【发布时间】:2020-05-29 03:02:48
【问题描述】:
我正在尝试从我的根目录之外下载一个文件,但是每次我尝试时,它都会尝试从根目录中获取它。我需要我网站的用户才能下载这些文件。
该文件最初已上传到 Amazon S3,我已使用 getObject 函数访问它。
这是我的代码:
app.get('/test_script_api', function(req, res){
var fileName = req.query.File;
s3.getObject(
{ Bucket: "bucket-name", Key: fileName },
function(error, s3data){
if(error != null){
console.log("Failed to retrieve an object: " + error);
}else{
//I have tried passing the S3 data but it asks for a string
res.download(s3data.Body);
//So I have tried just passing the file name & an absolute path
res.download(fileName);
}
}
);
});
这将返回以下错误: 错误:ENOENT:没有这样的文件或目录,stat '/home/ec2-user/environment/test2.txt'
当我输入绝对路径时,它只是将它附加到 /home/ec2-user/environment/ 的末尾
如何更改 res.download 尝试下载的目录?
有没有更简单的方法从 Amazon S3 下载文件?
任何帮助将不胜感激!
【问题讨论】:
标签: javascript node.js amazon-web-services file express