AWS Lambda S3 GET/POST - SignatureDoesNotMatch error

▼魔方 西西 提交于 2020-01-01 16:57:07

问题


I have had a Lambda node.js function up and running for about 6 months without any issue. The function simply takes a object and copies it from one bucket to another.

Today, I have started getting:

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

The code I am using is pretty simple, any suggestions on how I could fix this?

var aws = require('aws-sdk');
var s3 = new aws.S3({apiVersion: '2006-03-01'});

exports.handler = function(event, context) {

  var to_bucket = 'my_to_bucket/test';
  var from_bucket = event.Records[0].s3.bucket.name;
  var key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));
  var size = Math.floor(event.Records[0].s3.object.size / 1024);

  s3.getObject({Bucket: from_bucket, Key: key}, function(err, data) {
    if (err) {
      // send a webhook
    } 
    else {
      s3.putObject({Bucket: to_bucket, Key: key, Body: data.Body, ContentType: data.ContentType}, 
      function(err, data) {
        if (err) {
          // send a webhook
        }
        else {
          // send a webhook
        }
      });
    } // end else
  }); // end getobject
};

UPDATE: I have discovered that if sending to a bucket, it works fine. If I sent to any subfolder of the same bucket, it fails. I do send to a subfolder and simplified the code above initially, but I've updated it to show a subfolder in the to_bucket.


回答1:


I found a fix for this. After realising it was due to the folder inside the bucket, and not just sending to a bucket root, I searched and found the following post: https://github.com/aws/aws-sdk-go/issues/562

It looks like the bucket should not include the subfolder, instead the key should. Why this has worked until now is a mystery. Here is the replacement code for above:

var aws = require('aws-sdk');
var s3 = new aws.S3({apiVersion: '2006-03-01'});

exports.handler = function(event, context) {

  var to_bucket = 'my_to_bucket';
  var from_bucket = event.Records[0].s3.bucket.name;
  var key = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));
  var size = Math.floor(event.Records[0].s3.object.size / 1024);

  s3.getObject({Bucket: from_bucket, Key: key}, function(err, data) {
    if (err) {
      // send a webhook
    } 
    else {
      key = 'subfolder/' + key;
      s3.putObject({Bucket: to_bucket, Key: key, Body: data.Body, ContentType: data.ContentType}, 
      function(err, data) {
        if (err) {
          // send a webhook
        }
        else {
          // send a webhook
        }
      });
    } // end else
  }); // end getobject
};


来源:https://stackoverflow.com/questions/38127283/aws-lambda-s3-get-post-signaturedoesnotmatch-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!