使用Node.js从EC2实例下载AWS S3文件 [英] Download AWS S3 file from EC2 instance using Node.js
问题描述
我正在尝试从Node.js托管应用程序从Amazon S3存储桶下载文件.
var folderpath= process.env.HOME || process.env.USERPROFILE // tried using os.homedir() also
var filename = 'ABC.jpg';
var filepath = 'ABC';
AWS.config.update({
accessKeyId: "XXX",
secretAccessKey: "XXX",
region: 'ap-southeast-1'
});
var DOWNLOAD_DIR = path.join(folderpath, 'Downloads/');
var s3 = new AWS.S3();
var s3Params = {Bucket: filepath,Key: filename, };
var file = require('fs').createWriteStream(DOWNLOAD_DIR+ filename);
s3.getObject(s3Params).createReadStream().pipe(file);
此代码在localhost上正常工作,但不适用于实例,因为在实例上,folderpath返回"/home/ec2-user",而不是用户计算机的下载文件夹路径,即"C:\ Users \ name"./p>
请建议我如何将文件下载到用户的计算机?如何从ec2实例获取用户主目录的路径?
谢谢.
您可以使用express创建http服务器和API.您可以在Express.js入门上找到大量教程.完成express.js的初始设置后,您可以在node.js代码中执行以下操作:
AWS.config.update({
accessKeyId: "XXX",
secretAccessKey: "XXX",
region: 'ap-southeast-1'
});
var s3 = new AWS.S3();
app.get('/download', function(req, res){
var filename = 'ABC.jpg';
var filepath = 'ABC';
var s3Params = {Bucket: filepath, Key: filename};
var mimetype = 'video/quicktime'; // or whatever is the file type, you can use mime module to find type
res.setHeader('Content-disposition', 'attachment; filename=' + filename);
res.setHeader('Content-type', mimetype);
// Here we are reading the file from S3, creating the read stream and piping it to the response.
// I'm not sure if this would work or not, but that's what you need: Read from S3 as stream and pass as stream to response (using pipe(res)).
s3.getObject(s3Params).createReadStream().pipe(res);
});
完成此操作后,可以调用此API /download
,然后将文件下载到用户的计算机上.根据前端使用的框架或库(或纯JavaScript),您可以使用此/download
api下载文件.只是Google,如何使用XYZ(框架)下载文件.
I am trying to download file from Amazon S3 bucket from my Node.js hosted application.
var folderpath= process.env.HOME || process.env.USERPROFILE // tried using os.homedir() also
var filename = 'ABC.jpg';
var filepath = 'ABC';
AWS.config.update({
accessKeyId: "XXX",
secretAccessKey: "XXX",
region: 'ap-southeast-1'
});
var DOWNLOAD_DIR = path.join(folderpath, 'Downloads/');
var s3 = new AWS.S3();
var s3Params = {Bucket: filepath,Key: filename, };
var file = require('fs').createWriteStream(DOWNLOAD_DIR+ filename);
s3.getObject(s3Params).createReadStream().pipe(file);
This code works fine on localhost but does not work from instance because on instance folderpath returns "/home/ec2-user" instead of path of downloads folder of user machine i.e something like "C:\Users\name".
Please suggest me how can I download file to user's machine? How to get path of user's home directory from ec2 instance?
Thank you.
You can use express to create http server and APIs. You can find numerous tutorials on get started with Express.js. After the initial setup of express.js is done, you can do something like this in node.js code:
AWS.config.update({
accessKeyId: "XXX",
secretAccessKey: "XXX",
region: 'ap-southeast-1'
});
var s3 = new AWS.S3();
app.get('/download', function(req, res){
var filename = 'ABC.jpg';
var filepath = 'ABC';
var s3Params = {Bucket: filepath, Key: filename};
var mimetype = 'video/quicktime'; // or whatever is the file type, you can use mime module to find type
res.setHeader('Content-disposition', 'attachment; filename=' + filename);
res.setHeader('Content-type', mimetype);
// Here we are reading the file from S3, creating the read stream and piping it to the response.
// I'm not sure if this would work or not, but that's what you need: Read from S3 as stream and pass as stream to response (using pipe(res)).
s3.getObject(s3Params).createReadStream().pipe(res);
});
Once this is done, you can call this API /download
, and then download the file on user's machine. Depending on the framework or library (or plain javascript) which you are using in front end, you can download the file using this /download
api. Just google, how to download file using XYZ (framework).
这篇关于使用Node.js从EC2实例下载AWS S3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!