s3.getSignedUrl ResponseContentDisposition 参数无效

s3.getSignedUrl ResponseContentDisposition parameter not working

我成功生成了签名的 url,然后我可以在有限的时间内使用它从我的 s3 存储桶下载资源。但是,我正在尝试在此处记录的参数中使用 ResponseContentDisposition 属性:

  1. http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getSignedUrl-property
  2. http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#getObject-property

我不确定我是否做错了,但出于某种原因 headers 没有被设置。例如,如果我使用 url 我从 s3.getSignedUrl:

返回

curl -i "https://foo-dev.s3.amazonaws.com/images/foo.jpg?AWSAccessKeyId=AKIAICBHUC26S6B446PQ&Expires=1468359314&Signature=EeBqx1G83oeusarBl2KUbbCCBgA%3D&response-content-disposition=attachment%3B%20filename%3Ddata.jpg"

headers 是:

x-amz-id-2: SG9rjYQCcuqgKfjBmMbDQC2CNLcnqBAFzP7zINa99VYUwNijPOm5Ea/5fllZ6cnt/Qti7e26hbE=
x-amz-request-id: 2670068008525B1D
Date: Tue, 12 Jul 2016 21:26:16 GMT
Content-Disposition: inline; filename=foo.jpg
Last-Modified: Tue, 12 Jul 2016 00:47:23 GMT
ETag: "2a8e36651b24769170f4faa429f40f54"
Accept-Ranges: bytes
Content-Type: image/jpeg
Content-Length: 43373
Server: AmazonS3

我正在使用 javascript s3 sdk 进行设置,如下所示:

function tempRedirect(req, res) {
    var filename = req.params[0];
    var contentDisposition = 'attachment; filename=data.jpg';
    var params = {
        Bucket: S3_BUCKET,
        ResponseContentDisposition: contentDisposition,
        Key: checkTrailingSlash(getFileKeyDir(req)) + filename
    };
    var s3 = new aws.S3(s3Options);
    s3.getSignedUrl('getObject', params, function(err, url) {
        res.redirect(url);
    });
};

文档非常简单,我只能找到 PHP 个示例,但看起来我确实正确设置了内容处置。

有人知道这里出了什么问题吗??

根据 RFC- 2616,您的值格式错误。

预期格式为 attachment; filename="funny-cat.jpg"。文件名是带引号的字符串。

而且,我最初的假设是 S3 将其视为无效而拒绝并默默地拒绝替换该值。

后续测试揭示了意外行为:如果 Content-Disposition 未与 object 一起存储,则 &response-content-disposition=... 会按预期工作,设置响应 header。但是,如果 header 与 object 一起存储,则此查询字符串参数不具有 "overriding" 该值的记录效果。

相反,&response-content-type=... 会覆盖 object 的已存储 Content-Type:

这就是一些快速测试为我揭示的结果。

但这似乎是 S3 中的错误——或者更准确地说,是某种回归。根据一个支持论坛 post,行为实际上是不一致的,有时有效,有时无效。

S3 is aware of this issue and we are working to resolve it. (2016-07-12)

https://forums.aws.amazon.com/thread.jspa?threadID=235006