简体   繁体   English

使用签名的上传URL从浏览器上传到S3失败

[英]Uploading to S3 from browser using signed upload URL fails

I'm trying to upload a file to S3 directly from browser. 我正在尝试直接从浏览器将文件上传到S3。 For this, I'm generating a signed upload URL on my server. 为此,我正在服务器上生成一个签名的上传URL。

When I try to upload a file to the generated url from the browser using 当我尝试使用浏览器将文件上传到生成的url时

var xhr = new XMLHttpRequest();
xhr.open('PUT', signedUrl);     
xhr.send(file);

I get the following error 我收到以下错误

<Error>
  <Code>
    SignatureDoesNotMatch
  </Code>
  <Message>
    The request signature we calculated does not match the signature you provided. Check your key and signing method.
  </Message>
  ...
</Error>

I can upload to the signed URL using curl with the following command 我可以使用curl通过以下命令上传到签名的URL

curl -T <file> -X PUT '<signed-url>'

The browser is sending my request as multipart-form data which is causing the problem. 浏览器将我的请求作为多部分形式的数据发送,这导致了问题。 How do I fix the issue? 我该如何解决该问题?

I'm generating the signed URL using the following code 我正在使用以下代码生成签名的URL

var params = {
    Bucket: bucket, 
    Key: s3Key,
    ACL: "authenticated-read",
    Expires: 600
};

S3.getSignedUrl('putObject', params, (err, data) => {
    const returnData = {
        url: data
    };
    reply(returnData);
});

Edit: Just to be clear, I have allowed all origins to send PUT requests in bucket CORS configuration. 编辑:为了清楚起见,我已允许所有来源在存储桶CORS配置中发送PUT请求。

Okay, the problem was that Content-Type was being sent by the browser in the request header but the signed url wasn't being generated for the content type. 好的,问题是浏览器在请求标头中发送了Content-Type,但没有为内容类型生成签名的url。 Instead of this 代替这个

var params = {
    Bucket: bucket, 
    Key: s3Key,
    ACL: "authenticated-read",
    Expires: 600
};
S3.getSignedUrl('putObject', params, ...);

I did this 我做了这个

var params = {
    Bucket: bucket, 
    Key: s3Key,
    ACL: "authenticated-read",
    Expires: 600,
    ContentType: require('mime').lookup(filename)
};
S3.getSignedUrl('putObject', params, ...);

and it worked! 而且有效!

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

相关问题 S3使用预先签名的URL从浏览器上传图片 - S3 Upload image with pre-signed url from browser 如何使用预签名的 URL 而不是凭证直接从浏览器上传到 AWS S3? - How to upload to AWS S3 directly from browser using a pre-signed URL instead of credentials? 使用签名URL上传到Amazon S3可以使用curl但不能在javascript中使用 - Uploading to Amazon S3 using a signed URL works with curl but not in javascript 使用签名 URL 上传到 S3 时获取 403(禁止访问) - Getting 403 (Forbidden) when uploading to S3 with a signed URL 如何使用 angular 或 javascript 中的预签名 url 将文件上传到 S3 存储桶 - how to upload file to S3 bucket using pre-signed url in angular or javascript cordova-plugin-file-transfer:如何使用签名的URL将文件上传到S3? - cordova-plugin-file-transfer: How do you upload a file to S3 using a signed URL? 使用 lamda 上传 S3 图像的签名 URL 上的“PUT”请求上出现 403 - 403 on `PUT` request on signed url for S3 image upload using lamda 使用 POST 直接从浏览器上传文件到 S3 - Upload file to S3 direct from the browser using POST 通过预签名URL将PUT上传到S3时拒绝签名 - Signature Denied in PUT upload to S3 with Pre-Signed URL 从 s3 url 将文件上传到 s3 存储桶 - uploading file to s3 bucket from s3 url
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM