简体   繁体   English

使用JQuery使用预先签名的URL将文件上载到S3时出错

[英]Error uploading file to S3 with presigned url using JQuery

We are trying to upload a file to S3 using jQuery ajax & a presigned url. 我们正在尝试使用jQuery ajax和预先签名的URL将文件上传到S3。 We generate the presigned url on the server. 我们在服务器上生成预先指定的URL。 Currently we are trying to use FormData to upload the file. 目前我们正在尝试使用FormData上传文件。

          var uploadData = new FormData(),
            files = $(this.input).prop('files'),
            file = files[0];

          uploadData.append('file', file);

          $.ajax({
            url: '{presigned url string}',
            type: 'PUT',
            data: uploadData,
            cache: false,
            processData: false,
            contentType: false,
            success: function(response) {
              console.log('S3 upload success!');
            },
            error: function(response) {
              console.log('Error with S3 upload: ' + response.statusText);
            }
          });

This returns a SignatureDoesNotMatch error from AWS: 这将从AWS返回SignatureDoesNotMatch错误:

<Error><Code>SignatureDoesNotMatch</Code><Message>The request signature we 
calculated does not match the signature you provided. Check your key and 
signing method.</Message><StringToSignBytes>50 55 54 0a 0a 6d 75 6c 74 69 70 61 72 74 2f 66 6f 72 6d 2d 64 61 74 61 3b 20 62 6f 75 6e 64 61 72 79 3d 2d 2d 2d 2d 57 65 62 4b 69 74 46 6f 72 6d 42 6f 75 6e 64 61 72 79 67 57 6f 4a 73 42 56 79 41 4c 57 71 51 6b 73 69 0a 31 34 30 39 37 37 37 32 39 33 0a 2f 64 69 2d 6b 79 72 73 74 65 6e 2d 64 65 61 6c 73 2f 76 4e 79 4b 4e 55 4c 37 51 68 4f 30 45 4b 38 52 58 44 70 32 59 77 25 32 46 63 35 37 65 64 37 62 39 2d 64 63 61 62 2d 34 63 30 62 2d 62 36 63 30 2d 36 31 66 30 36 62 32 30 37 34 66 31 2d 74 65 73 74 2e 74 78 74</StringToSignBytes>
<RequestId>ED7C581570F547DB</RequestId><HostId>ZT6LsFYCbo1L0gYNcUwtdCWF6SNnyuUyKiL60ntJEZugx3cnDN/yH5KBjgEiBv5c</HostId><SignatureProvided>N2d7oNMVHvI6yxAXujNy8O5cF24=</SignatureProvided>
<StringToSign>PUT

multipart/form-data; boundary=----WebKitFormBoundarygWoJsBVyALWqQksi
1409777293
/test-bucket/vNyKNUL7QhO0EK8RXDp2Yw%2Fc57ed7b9-dcab-4c0b-b6c0-61f06b2074f1-test.txt</StringToSign><AWSAccessKeyId>FAKEACCESSKEY</AWSAccessKeyId></Error>  

I know that the presigned url works because I can upload the file correctly via CURL: 我知道预签名网址有效,因为我可以通过CURL正确上传文件:

curl -v --upload-file {filename} {presigned url}

We found the issue ourselves. 我们自己发现了这个问题。 Here is the code we were using to generate the pre-signed url: 以下是我们用于生成预签名网址的代码:

val dt: DateTime = new DateTime()
val expiration = DateTime.now.plusMillis(timeout.toInt)
val presignedUrlRequest: GeneratePresignedUrlRequest =
  new GeneratePresignedUrlRequest(awsBucket, key, HttpMethod.PUT)
presignedUrlRequest.setExpiration(expiration.toDate())
// WE ADDED THIS LINE
presignedUrlRequest.setContentType("multipart/form-data")

s3client.generatePresignedUrl(presignedUrlRequest).toString()

We had to set the content type in the presignedUrlRequest as well as add 我们必须在presignedUrlRequest中设置内容类型以及添加
headers: {'Content-Type': 'multipart/form-data'} to our ajax call. headers: {'Content-Type': 'multipart/form-data'}到我们的ajax调用。

The way we were generating the url at first worked with CURL since we weren't setting the content-type in the presignedUrlRequest and curl wasn't setting a content-type. 我们最初生成url的方式是使用CURL,因为我们没有在presignedUrlRequest中设置content-type而curl没有设置内容类型。

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

相关问题 使用预签名 URL 将文件上传到 S3 - Uploading file to S3 using a pre-signed URL 使用预签名 URL 将文件从 javascript 前端上传到 S3 存储桶返回错误 400 - Uploading a file from javascript frontend to S3 bucket using presigned URL return error 400 使用预签名 URL 上传后,AWS S3 上的文件在文件中包含额外信息 - File on AWS S3 having extra information in file after uploading using presigned URL 使用预签名的 url 将文件发送到 s3 存储桶会出现 403 错误(SignatureDoesNotMatch) - Sending file to s3 bucket using presigned url gives 403 error (SignatureDoesNotMatch) Javascript-通过预先签名的URL将CSV上传到S3-文件包含边界数据 - Javascript - Uploading CSV to S3 by presigned URL - file contains boundary data 使用预先签名的URL将图像上传到S3 - Upload image to S3 using presigned URL 如何使用带有 Nuxt.js 和 Axios 的预签名 url 将文件上传到 S3 存储桶? - How to upload a file into a S3 bucket using a presigned url with Nuxt.js and Axios? S3预签名url文件上传——nodeJS+客户端 - S3 presigned url file upload – nodeJS + client S3 presigned url PUT 和 POST 返回 200 但在大多数情况下文件未上传 - S3 presigned url PUT and POST returning 200 but files not uploading in most cases 使用预签名链接上传到 AWS S3 时出现 403 Cors 问题 - 403 Cors issue when uploading to AWS S3 using a presigned link
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM