简体   繁体   English

CORS将文件上传到S3,出现SignatureDoesNotMatch错误

[英]CORS upload file to S3, got SignatureDoesNotMatch error

I want to upload video files from browser to S3. 我想将视频文件从浏览器上传到S3。 Here is the my steps: 这是我的步骤:

  1. Send GET request to API Gateway, invoke Lambda to get a signed URL. GET请求发送到API网关,调用Lambda以获取签名的URL。
  2. Use the signed URL directly to upload files to S3 using PUT . 直接使用签名的URL通过PUT将文件上传到S3。

Step 1 works fine, I can use the signed URL to upload file through curl : 第1步工作正常,我可以使用签名的URL通过curl上传文件:

curl -X PUT https://bucket-name.s3.amazonaws.com/testupload/videofile.mp4?AWSAccessKeyId=MY_AWS_ACCESS_KEY_ID&Expires=1523329853&Signature=XXXXX -T videophile.mp4

But upload the file to S3 from browser, I get 403 response with SignatureDoesNotMatch error: 但是将文件从浏览器上传到S3,我收到403响应,并带有SignatureDoesNotMatch错误:

<Error> <错误>

<Code>SignatureDoesNotMatch</Code> <Code> SignatureDoesNotMatch </ Code>

<Message>The request signature we calculated does not match the signature you provided. <Message>我们计算出的请求签名与您提供的签名不匹配。 Check your key and signing method.</Message> 检查您的密钥和签名方法。</ Message>

<AWSAccessKeyId>MY_AWS_ACCESS_KEY_ID</AWSAccessKeyId> <AWSAccessKeyId> MY_AWS_ACCESS_KEY_ID </ AWSAccessKeyId>

... ...

... ...

</Error> </ Error>

I checked the access key is the one I used in lambda to create signed url 我检查了访问密钥是我在lambda中用来创建签名URL的密钥


Client side Javascript: 客户端Javascript:

$.ajax({
    url : signedurl,
    type : "PUT",
    data : uploadfile,
    dataType : "text",
    cache : false,
    contentType : file.type,
    processData : false
})
.done(function() {

})
.fail(function(e) {
    console.error(e.status, e.statusText)
    console.error(e.responseText)
});

Bucket CORS config: 存储桶CORS配置:

<?xml version="1.0" encoding="UTF-8"?>
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">    
<CORSRule>
    <AllowedOrigin>*</AllowedOrigin>
    <AllowedMethod>GET</AllowedMethod>
    <AllowedMethod>POST</AllowedMethod>
    <AllowedMethod>PUT</AllowedMethod>
    <AllowedMethod>HEAD</AllowedMethod>
    <AllowedMethod>DELETE</AllowedMethod>
    <MaxAgeSeconds>3000</MaxAgeSeconds>
    <AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

I found the solution. 我找到了解决方案。 Content Type is needed when creating the signed URL and in the http request header (even when you can upload file by curl ) : 创建签名的URL时和在HTTP请求标头中(即使您可以通过curl上传文件),也需要Content Type

Lambda code (node): Lambda代码(节点):

s3.getSignedUrl('putObject', {
    Bucket: "bucket-name",
    Key: `key/filename.ext`,
    ContentType: event.contentType,  // e.g.: "video/mp4"
    Expires: 300
})

Client side JavaScript 客户端JavaScript

$.ajax({
    url : signedurl,
    type : "PUT",
    data : uploadfile,
    cache : false,
    headers:{
        'Content-Type': file.type  // e.g.: "video/mp4"
    },
    processData : false
})

Useful links: 有用的链接:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM