我需要上传Gziped格式的内容到S3通过一个签名的网址。
下面是我如何使用JS后端生成签名的URL:
s3.createPresignedPost({
Bucket: 'name',
Fields: {
key: 'key'
}
})我尝试将Content-Encoding头传递给signedURL POST请求,但不起作用。未在s3对象上正确设置标头。
我还尝试设置了一个post upload lambda来更新元数据。它失败,出现错误File is identical error
最后,我尝试使用cloudfront +一个lambda来强制使用头。这也失败了,错误声明Content-Enconding是一个受保护的错误。
发布于 2019-01-07 08:22:59
--更新开始--
对于通过Ajax或JS脚本上传到S3,我建议使用s3.getSignedUrl方法。s3.createPresignedPost仅用于直接上传浏览器。
下面是我使用this指南创建的Ajax jQuery上传示例。
s3.getSignedUrl('putObject', {
Bucket: 'bucketName',
Key: 'sample.jpg.gz',
// This must match with your ajax contentType parameter
ContentType: 'binary/octet-stream'
/* then add all the rest of your parameters to AWS puttObect here */
}, function (err, url) {
console.log('The URL is', url);
});Ajax PUT Script -从上面的函数调用中获取Url并在下面使用它。
$.ajax({
type: 'PUT',
url: "https://s3.amazonaws.com/bucketName/sample.jpg.gz?AWSAccessKeyId=AKIAIOSFODNN7EXAMPLE&Content-Type=binary%2Foctet-stream&Expires=1547056786&Signature=KyTXdr8so2C8WUmN0Owk%2FVLw6R0%3D",
//Even thought Content-Encoding header was not specified in signature, it uploads fine.
headers: {
'Content-Encoding': 'gzip'
},
// Content type must much with the parameter you signed your URL with
contentType: 'binary/octet-stream',
// this flag is important, if not set, it will try to send data as a form
processData: false,
// the actual file is sent raw
data: theFormFile
}).success(function () {
alert('File uploaded');
}).error(function () {
alert('File NOT uploaded');
console.log(arguments);
});在S3 object中,您应该在metadata下看到Content-Type、Content-Encoding。
重要提示当您尝试通过浏览器上运行的JS脚本上传时,浏览器通常会在调用PUT方法之前先发送OPTIONS方法预检(或CORS检查)。你会得到选项的403 Forbidden错误,因为S3存储桶上的CORS不允许这样做。我解决的一种方法是在存储桶级别上使用以下CORS配置。Reference
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedOrigin>*</AllowedOrigin>
<AllowedMethod>PUT</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>--更新结束--
你试过这样做吗?我刚刚使用亚马逊网络服务文档中给出的示例html测试了该策略。参考- https://docs.aws.amazon.com/AmazonS3/latest/API/sigv4-HTTPPOSTConstructPolicy.html
s3.createPresignedPost({
Bucket: 'name',
Conditions: [
{ "Content-Encoding": "gzip" }
],
Fields: {
key: 'key'
}
})更新-这是我到目前为止的观察结果。
我们真的需要检查您的客户端是否正在进行上传操作。如果你想在MetaData上设置Content-Encoding,那么你的预签名网址应该设置Content-Encoding属性。如果签名的Url没有,但是你的请求头有,那么它会给你Extra input fields: content-encoding。
我已经用Content-Encoding签署了一个网址,并上传了一个带有以下示例html的压缩文件。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<form action="http://bucket-name.s3.amazonaws.com" method="post" enctype="multipart/form-data">
Key to upload:
<input type="input" name="key" value="sample.jpg.gz" /><br />
Content-Encoding:
<input type="input" name="Content-Encoding" value="gzip" /><br />
<input type="text" name="X-Amz-Credential" value="AKIAIOSFODNN7EXAMPLE/20190108/us-east-1/s3/aws4_request" />
<input type="text" name="X-Amz-Algorithm" value="AWS4-HMAC-SHA256" />
<input type="text" name="X-Amz-Date" value="20190108T220828Z" />
Tags for File:
<input type="hidden" name="Policy" value='bigbase64String' />
<input type="hidden" name="X-Amz-Signature" value="xxxxxxxx" />
File:
<input type="file" name="file" /> <br />
<!-- The elements after this will be ignored -->
<input type="submit" name="submit" value="Upload to Amazon S3" />
</form>
</html>如果我不发送Content-Encoding标头,它会给出错误Policy Condition failed: ["eq", "$Content-Encoding", "gzip"]
注意-如果您在上传时使用https,请确保您在S3端点上具有正确的证书,否则将获得证书错误。
S3截图。

https://stackoverflow.com/questions/54064149
复制相似问题