无法从Amazon S3

时间:2017-04-27 11:29:23

标签: amazon-web-services 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);

        }
    });
});

我收到以下表格的网址:

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]

但是,当我尝试在浏览器中打开此链接时,我收到以下错误:

<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>

我无法理解为什么会出现此错误,因为返回的url具有所有必需的查询参数。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:1)

  

注意:如果同步调用此方法(没有回调),则必须确保拥有静态或先前已解析的凭据,否则可能无法正确签署请求。如果您不能保证这一点(您使用的是异步凭据提供程序,即EC2 IAM角色),则应始终使用异步回调调用此方法。

尝试在方法回调函数中检索签名的URL。

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