Paul
Paul

Reputation: 588

AWS Lambda S3 GET/POST - SignatureDoesNotMatch error

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.

Upvotes: 3

Views: 2753

Answers (1)

Paul
Paul

Reputation: 588

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

Upvotes: 4

Related Questions