【问题标题】:Heroku NodeJS: Error SignatureDoesNotMatch with aws-sdkHeroku NodeJS:错误 SignatureDoesNotMatch 与 aws-sdk
【发布时间】:2016-10-12 22:05:11
【问题描述】:

节点:5.6.0 角度:1.4.5 帆:0.12.1

我正在使用 SailsJS 和 Angular 构建应用程序,我需要将图像上传到 S3,但在运行 PUT http 请求时我不断收到 SignatureDoesNotMatch 错误。

我关注了这个heroku guide,但显然我必须使用 angular 和sails 而不是vanilla js 和express。

我搜索了很多,但找不到任何真正有用的东西。 示例:https://github.com/aws/aws-sdk-js/issues/86

我不知道自己做错了什么。

代码如下: AwsController:

var aws = require('aws-sdk');

module.exports = {
  "sign-s3": function(req, res) {
    const s3 = new aws.S3({region: 'eu-west-1'});
    const fileName = req.param('file-name');
    const fileType = req.param('file-type');
    const s3Params = {
      Bucket: process.env.S3_BUCKET,
      Key: fileName,
      Expires: 60,
      ContentType: fileType,
      ACL: 'public-read'
    };

    s3.getSignedUrl('putObject', s3Params, function (err, data) {
      if(err){
        console.log(err);
        return res.end();
      }

      const returnData = {
        signedRequest: data,
        url: 'https://' + process.env.S3_BUCKET + '.s3.amazonaws.com/' + fileName
      };

      return res.json(returnData);
    });
  }
};

app.factory.aws-manager.js:

function getSignedRequest(file) {
  return $http.get('/aws/sign-s3?file-name=' + file.name + '&file-type=' + file.type)
    .then(successCallback)
    .catch(errorCallback);

  function successCallback(response) {
    return response.data;
  }

  function errorCallback(response) {
    console.log(response);
    return response;
  }
}

function sendFile(file, signedRequest, url){
  var data = {file: file};

  return $http.put(signedRequest, data)
    .then(successCallback)
    .catch(errorCallback);

  function successCallback(response) {
    return { data: response.data, url: url};
  }

  function errorCallback(response) {
    console.log(response);
    return response;
  }
}

function uploadFile(file) {
  return getSignedRequest(file)
    .then(successCallback);

  function successCallback(data) {
    console.log(data);
    return sendFile(file, data.signedRequest, data.url)
      .then(successCallback);

    function successCallback(response) {
      return response.url;
    }
  }
}

电话:

awsManager.uploadFile(file);

错误:

<?xml version="1.0" encoding="UTF-8"?>
<Error>
    <Code>SignatureDoesNotMatch</Code>
    <Message>The request signature we calculated does not match the signature you provided. Check your key and signing method.</Message>
    <AWSAccessKeyId>AKIAJFI5FDWJK7TTGIKQ</AWSAccessKeyId>
    <StringToSign>PUT application/json;charset=UTF-8 1465728343 x-amz-acl:public-read /aemporium-assets/einstein.jpg</StringToSign>
    <SignatureProvided>mtVl69xyqhAbYhQt1oZk0F1w1WE=</SignatureProvided>
    <StringToSignBytes>50 55 54 0a 0a 61 70 70 6c 69 63 61 74 69 6f 6e 2f 6a 73 6f 6e 3b 63 68 61 72 73 65 74 3d 55 54 46 2d 38 0a 31 34 36 35 37 32 38 33 34 33 0a 78 2d 61 6d 7a 2d 61 63 6c 3a 70 75 62 6c 69 63 2d 72 65 61 64 0a 2f 61 65 6d 70 6f 72 69 75 6d 2d 61 73 73 65 74 73 2f 65 69 6e 73 74 65 69 6e 2e 6a 70 67</StringToSignBytes>
    <RequestId>900A623A516D2DDD</RequestId>
<HostId>j1t+w1N6MLeWA2UAirQbTXIa2+U5vVtMTSYiRo72SzOnzDf0TyDZxlL4VoXnCLqXu+hv/rxN9Z8=</HostId>
</Error>

(我确定所有 process.env.* 变量都是正确的)

【问题讨论】:

  • 你能把“.”去掉吗来自 AWS S3 存储桶名称,并使用不带“.”的存储桶名称。然后尝试
  • 你的意思是这样的: url: 'https://' + process.env.S3_BUCKET + 's3.amazonaws.com/' + fileName

标签: angularjs node.js amazon-web-services heroku amazon-s3


【解决方案1】:

我发现了问题,我没有注意到在 PUT 调用的请求标头中我有 2 个错误的参数:

  1. 接受:是 json/application,它必须是 */*

  2. Content-Type:它是 json/application,它必须是“file.type”

更正后的代码:

function sendFile(file, signedRequest, url){
  var data = {file: file};

  return $http.put(signedRequest, file, {"headers": {"Accept":"*/*", "Content-Type":file.type}})
    .then(successCallback)
    .catch(errorCallback);

  function successCallback(response) {
    return { data: response.data, url: url};
  }

  function errorCallback(response) {
    console.log(response);
    return response;
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-05-02
    • 1970-01-01
    • 2015-07-06
    • 2014-12-31
    • 1970-01-01
    • 2021-12-04
    • 2019-01-29
    • 2016-11-02
    相关资源
    最近更新 更多