使用Node.js从EC2实例下载AWS S3文件
我正试图从Node.js托管的应用程序中从Amazon S3 bucket下载文件使用Node.js从EC2实例下载AWS S3文件,node.js,amazon-s3,amazon-ec2,Node.js,Amazon S3,Amazon Ec2,我正试图从Node.js托管的应用程序中从Amazon S3 bucket下载文件 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", secretAccessK
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”
请建议我如何将文件下载到用户的机器上?如何从ec2实例获取用户主目录的路径
谢谢。您可以使用express创建http服务器和API。您可以在get started with 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(框架)下载文件。您可以使用express创建http服务器和API。您可以在get started with 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下载文件。只有谷歌,如何使用XYZ(框架)下载文件。作为旁注:将secretAccessKey保存在代码中是个坏主意。您应该创建IAM角色并将其分配给EC2实例。好的,我会的。谢谢@Sergey Kovalevw你说的“不起作用”是什么意思?你有错误吗folderpath
是Linux机器上的普通目录路径(它是ec2用户的主目录)。您是否确保/home/ec2 user/Downloads/
目录存在,并且用户对其具有写入权限?否,我没有收到任何错误,但我需要用户的下载文件夹路径。为什么我需要在/home/ec2 user/中写入的权限?其他用户如何在其计算机上拥有ec2用户文件夹?如果您希望从客户端下载该文件,那么您只需要从nodejs流式传输该文件。您不能在nodejs中编写代码以在客户端下载文件。您必须通过某个端点(api)流式传输文件,然后在客户端使用该api。假设您的客户端是浏览器,您将使用前端Javascript编写代码以在用户的机器上下载文件。您应该创建IAM角色并将其分配给EC2实例。好的,我会的。谢谢@Sergey Kovalevw你说的“不起作用”是什么意思?你有错误吗folderpath
是Linux机器上的普通目录路径(它是ec2用户的主目录)。您是否确保/home/ec2 user/Downloads/
目录存在,并且用户对其具有写入权限?否,我没有收到任何错误,但我需要用户的下载文件夹路径。为什么我需要在/home/ec2 user/中写入的权限?其他用户如何在其计算机上拥有ec2用户文件夹?如果您希望从客户端下载该文件,那么您只需要从nodejs流式传输该文件。您不能在nodejs中编写代码以在客户端下载文件。您必须通过某个端点(api)流式传输文件,然后在客户端使用该api。假设您的客户端是浏览器,您将使用前端Javascript编写代码在用户的机器上下载文件。它返回文件数据意味着我仍然在angular.js中编写文件,这可能导致查找路径并提供给createWriteStream()非常感谢您的时间和努力,但我认为签名url将易于使用。它返回文件的数据意味着我仍在angular.js中编写文件,这可能导致查找路径并提供给createWriteStream()非常感谢您的时间和努力,但我认为签名url将易于使用。