我使用以下代码从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]&X-Amz-Credential=[VALUE]&X-Amz-Date=20170427T111724Z&X-Amz-Expires=60&X-Amz-Signature=[VALUE]&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具有所有必需的查询参数。有人可以帮忙吗?
答案 0 :(得分:1)
注意:如果同步调用此方法(没有回调),则必须确保拥有静态或先前已解析的凭据,否则可能无法正确签署请求。如果您不能保证这一点(您使用的是异步凭据提供程序,即EC2 IAM角色),则应始终使用异步回调调用此方法。
尝试在方法回调函数中检索签名的URL。
s3.getSignedUrl('getObject', options, function(err, url) {
console.log(url);
});