【发布时间】:2016-09-03 17:17:11
【问题描述】:
在localhost 上工作正常,但在服务器上却不行。返回的所有 URL 都已过期。
<Error>
<Code>AccessDenied</Code>
<Message>Request has expired</Message>
<Expires>2016-04-26T09:44:22Z</Expires><ServerTime>2016-04-26T11:34:36Z</ServerTime><RequestId>33AF4E321F37ADA6</RequestId><HostId>+AXA3itXG9aKlt+EQkYxTHJCsxkEkymj+o2COPYo4+v26Vaxx17j/agh+hCq5NoHNzvJp2GI8Y=</HostId>
</Error>
在localhost 上,为同一对象生成的 URL 在expires 参数中不同,但在服务器上没有。服务器上的相同对象返回相同的 URL(expires 参数每次都相同)。
服务器是 Amazon EC2。凭据保存在localhost 和服务器上的/.aws/credentials 文件中
模型代码
var AWS = require('aws-sdk');
var s3 = new AWS.S3();
exports.download = function (req, res) {
var fileName = req.params.name;
var key = req.user._id + '/' + fileName;
var params = { Bucket: 'myBucket', Key: key };
s3.getSignedUrl('getObject', params, function (err, url) {
if (err) {
console.log('Getting Signed URL', err);
res.send(err);
} else {
console.log('Getting Signed URL', url);
res.send(url);
}
});
};
在 S3 上编辑了 CORS 配置
<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>GET</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
</CORSConfiguration>
【问题讨论】:
-
文档说它会默认为15分钟的有效期,所以结果很奇怪。您是否尝试过传递
Expires值?见:getSignedUrl() documentation -
我传递了
Expires值。结果相同。 -
你能从生成的 URL 中提取过期时间来检查它的值吗? (它将是 UTC。)
-
文件刚刚上传。浏览器中 URL 的结果。
<Error><Code>AccessDenied</Code><Message>Request has expired</Message><Expires>2016-05-09T09:27:53Z</Expires><ServerTime>2016-05-09T09:28:58Z</ServerTime><RequestId>35CA4C50C6F689E3</RequestId><HostId>/OswGs5Ixdpx5+ngNyBLwgnm1PWGqm4MhcfSNHHGLWEDLDg1I+FVcOHPfcvGEwvVTt1RIIK870M=</HostId></Error> -
实际的签名 URL 怎么样?您能在发送到 S3 的 URL 中看到到期时间吗?
标签: javascript node.js amazon-s3 amazon-ec2 mean-stack