Presigned AWS S3 PUT url无法使用jquery从客户端上传

时间:2015-03-06 12:40:32

标签: jquery node.js amazon-web-services amazon-s3

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

  

SignatureDoesNotMatch我们计算的请求签名没有   匹配您提供的签名。检查您的密钥和签名方法。

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

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真的难以使用

节点:

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>

3 个答案:

答案 0 :(得分:4)

我也一直在打这个。这对我有用,我得到了与你完全相同的错误。

在服务器端,我正在使用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..."
})

你的角色看起来对我来说,关键是要确保Content-Type的标题完全匹配

答案 1 :(得分:1)

非常感谢杰里米和kungfoo!我遇到了同样的问题,但使用Python的boto库时增加了皱纹。如果您从Python生成URL,则最小工作配置似乎是:

的Python

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)

S3(CORS)

所需的最低规则似乎是:

<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
});

答案 2 :(得分:0)

我遇到了同样的问题,但我不记得问题究竟是什么,但这个答案可能对你有所帮助upload-file-from-angularjs-directly-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');
    });