将内容编码标头添加到签名的URL上传文件中

时间:2019-01-06 17:31:25

标签: amazon-s3 pre-signed-url

我需要通过签名的URL将压缩的内容上传到S3。

这是我使用JS后端生成签名URL的方法:

s3.createPresignedPost({
  Bucket: 'name',
  Fields: {
    key: 'key'
  }
})

我尝试将Content-Encoding标头传递给signedURL POST请求,但这没有用。标头未在s3对象上正确设置。

我还尝试设置后上传lambda来更新元数据。它失败,并显示错误File is identical错误

最后,我尝试使用cloudfront + lambda强制执行标头。同样失败,并显示一条错误消息,指出Content-Enconding是受保护的错误。

1 个答案:

答案 0 :(得分:3)

-更新开始-

要通过Ajax或JS脚本上传到S3,我建议使用s3.getSignedUrl方法。 s3.createPresignedPost仅用于直接上传浏览器。

下面是我使用this指南创建的Ajax jQuery Upload示例。

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脚本-从上面的函数调用中获取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对象中,您应该在元数据下看到Content-Type和Content-Encoding。

重要提示 当您尝试通过运行在浏览器上的JS脚本进行上传时,通常浏览器通常会在调用OPTIONS方法之前先发送PUT方法预检(或CORS检查)。由于S3存储桶上的CORS不允许这样做,因此您会收到OPTIONS的403 Forbidden错误。我解决的一种方法是在存储桶级别使用以下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>

-更新结束-

您尝试过这样吗?我只是使用AWS文档中提供的示例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属性。如果Signed Url没有签名,但您的请求标头却包含Extra input fields: content-encoding

我已经用Content-Encoding签名了一个URL,并使用以下示例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截图。 enter image description here

相关问题