gpt4 book ai didi

jquery - 预签名的 AWS S3 PUT url 无法使用 jquery 从客户端上传

转载 作者:搜寻专家 更新时间:2023-10-31 23:09:15 24 4
gpt4 key购买 nike

刚开始使用 node.js aws 客户端生成预签名 url 并将其发送到浏览器供用户上传文件,但我收到以下消息:

SignatureDoesNotMatch The request signature we calculated does not match the signature you provided. Check your key and signing method.

我引用了很多链接,看起来很基础,但我似乎失败了

https://github.com/aws/aws-sdk-js/issues/251

Direct Browser Upload to S3 with Meteor, jQuery and the AWS SDK

https://forums.aws.amazon.com/thread.jspa?messageID=556839

要么,我完全傻了,要么sdk真的不好用

Node :

var putParams = {
Bucket: config.aws.s3UploadBucket,
Key: filename,
ACL: 'public-read',
Expires: 120,
Body: '',
ContentMD5: 'false'
};
s3.getSignedUrl('putObject', putParams, function (err, url) {
if (!!err) {
console.error(err);
res.json({error: ''});
return;
}

res.json({
'awsAccessKeyId': config.aws.accessKeyId,
's3bucket': config.aws.s3UploadBucket,
's3key': filename,
's3policy': s3policy.policy,
's3signature': s3policy.signature,
'url': url
});
});

客户:

  var fd = new FormData();
fd.append('file', file);
return new RSVP.Promise(function(resolve, reject) {
$.ajax({
url: uploadObj.url,
data: fd,
processData: false,
contentType: false,
crossDomain: true,
type: 'PUT',
success: function(json, textStatus, jqXhr){
console.log(json);
resolve(json);
},
error: function(jqXhr, textStatus, errorThrown){
reject({ jqXhr: jqXhr, textStatus: textStatus, errorThrown: errorThrown});
}
});
});

更新:为了回应一些评论,我确实为存储桶添加了一个有效的 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>PUT</AllowedMethod>
<AllowedMethod>POST</AllowedMethod>
<AllowedMethod>DELETE</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Content-*</AllowedHeader>
<AllowedHeader>Authorization</AllowedHeader>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>

最佳答案

我也一直在抗争。这对我有用,我遇到了与您完全相同的错误。

在服务器端,我使用 AWS-SDK for nodejs

var params = {    Bucket: "bucketname",    Key: "filename",     ContentType: "multipart/form-data"}var url = s3.getSignedUrl('putObject', params, function(err, url) {     console.log(url);}

客户端

$.ajax({    method: "PUT",    headers: {"Content-Type": "multipart/form-data"},    processData: false,    url: "http://AWSURL?AWSAccessKeyId..."})

我觉得你的 cors 很合适,关键是确保 Content-Type 的 header 完全匹配

关于jquery - 预签名的 AWS S3 PUT url 无法使用 jquery 从客户端上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28899005/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com