![](/img/trans.png)
[英]AWS S3 PUT request with presigned URL not working in javascript
[英]Presigned AWS S3 PUT url fails to upload from client using jquery
剛開始使用node.js aws客戶端來生成預先簽名的url並將其發送到瀏覽器以供用戶上傳文件,但是我收到以下消息:
SignatureDoesNotMatch我們計算的請求簽名與您提供的簽名不匹配。 檢查您的密鑰和簽名方法。
我引用了很多鏈接,它看起來很基本,但我似乎失敗了
https://github.com/aws/aws-sdk-js/issues/251
直接瀏覽器使用Meteor,jQuery和AWS SDK上傳到S3
https://forums.aws.amazon.com/thread.jspa?messageID=556839
要么,我完全是愚蠢的,或者sdk真的很難使用
節點:
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作為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的標題完全匹配
非常感謝jeremy和kungfoo! 雖然使用Python的boto庫時增加了皺紋,但我遇到了同樣的問題。 如果您從Python生成URL,則最小工作配置似乎是:
Boto3有效但不是Boto2 !
import boto3
conn = boto3.client('s3', ...)
url = conn.generate_presigned_url('put_object', {
'Bucket': my_bucket_name,
'Key': my_bucket_key,
'ContentType': 'my_content_type'
}, 300)
所需的最低規則似乎是:
<CORSRule>
<AllowedOrigin>*.mydomain.com</AllowedOrigin>
<AllowedMethod>PUT</AllowedMethod>
<MaxAgeSeconds>3000</MaxAgeSeconds>
<AllowedHeader>Content-*</AllowedHeader>
<AllowedHeader>Authorization</AllowedHeader>
</CORSRule>
使用jQuery,並確保保持與以前相同的內容類型:
$.ajax({
type: "PUT",
headers: {"Content-Type": "my_content_type"},
url: url_from_server,
data: my_data_as_string,
processData: false,
contentType: false,
crossDomain: true,
cache: false
});
我有同樣的問題,但我不記得究竟是什么問題,但這個答案可以幫助你上傳文件從angularjs-direct-to-amazon-s3-using-signed-url 。
服務器:
var AWS = require('aws-sdk');
AWS.config.update({accessKeyId: AWS_ACCESS_KEY, secretAccessKey: AWS_SECRET_KEY});
AWS.config.region = 'eu-west-1';
app.post('/s', function (req, res) {
var s3 = new AWS.S3();
var params = {Bucket: 'BUCKETNAME', Key: req.body.name, ContentType: req.body.type};
s3.getSignedUrl('putObject', params, function(err, url) {
if(err) console.log(err);
res.json({url: url});
});
});
客戶:
$.ajax({
url: '/s',
type: 'POST',
data: {name: file.name, size: file.size, type:file.type},
}).success(function(res){
$.ajax({
url: res.url,
type: 'PUT',
data: file,
processData: false,
contentType: file.type,
}).success(function(res){
console.log('Done');
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.