简体   繁体   English

无法从Amazon S3下载文件

[英]Unable to download file from Amazon S3

I am using following code to download an image from Amazon S3: 我正在使用以下代码从Amazon S3下载图像:

 router.post('/image-upload', function (req, res, next) {

    if (!req.files)
        return res.status(400).send('No files were uploaded.');

    var file = req.files['image_' + req.session.sessID];

    AWS.config.loadFromPath(<credentials_path>);

    var s3 = new AWS.S3();

    var params = {Bucket: credentials.aws_s3.bucket_name, Key: req.session.email, Body: file.data};

    s3.putObject(params, function(err, data) {

        if (err) {
            console.log(err)

        } else {

            var options = {
                Bucket: credentials.aws_s3.bucket_name,
                Key: req.session.email
            };

            var url = s3.getSignedUrl('getObject', options);
            console.log(url);

        }
    });
});

I am getting url in following form: 我以以下形式获取网址:

https://[S3 BUCKET].s3.ap-south-1.amazonaws.com/[KEY]?X-Amz-Algorithm=[VALUE]&amp;X-Amz-Credential=[VALUE]&amp;X-Amz-Date=20170427T111724Z&amp;X-Amz-Expires=60&amp;X-Amz-Signature=[VALUE]&amp;X-Amz-SignedHeaders=[VALUE]

However, when I try to open this link in browser, I am getting following error: 但是,当我尝试在浏览器中打开此链接时,出现以下错误:

<Error>
<Code>AuthorizationQueryParametersError</Code>
<Message>
Query-string authentication version 4 requires the X-Amz-Algorithm, X-Amz-Credential, X-Amz-Signature, X-Amz-Date, X-Amz-SignedHeaders, and X-Amz-Expires parameters.
</Message>
<RequestId>29819210D89C8877</RequestId>
<HostId>
aPpmRMYB7QCog4UDqs1j2rCdY3cy5H8u3kGE8nv2qXF6Y2iATPNquz+MQNdvr3zZ1ceRydRplq0=
</HostId>
</Error>

I am unable to understand why this error, as the returned url has all the requisite query parameters. 我无法理解为什么出现此错误,因为返回的url具有所有必需的查询参数。 Can anyone please help?? 谁能帮忙吗?

Note: You must ensure that you have static or previously resolved credentials if you call this method synchronously (with no callback), otherwise it may not properly sign the request. 注意:如果同步调用此方法(没有回调),则必须确保您具有静态或先前解析的凭据,否则它可能无法正确签名请求。 If you cannot guarantee this (you are using an asynchronous credential provider, ie, EC2 IAM roles), you should always call this method with an asynchronous callback. 如果不能保证这一点(您正在使用异步凭据提供程序,即EC2 IAM角色),则应始终使用异步回调调用此方法。

Try instead to retrieve the signed url inside the methods callback function. 而是尝试在方法回调函数中检索签名的url。

s3.getSignedUrl('getObject', options, function(err, url) {
  console.log(url);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM