aws-sdk: NoSuchKey: The specified key does not exist?

Shubham Verma picture Shubham Verma · Jun 18, 2017 · Viewed 12k times · Source

In my nodejs project, I am using aws-sdk to download all the images from my s3 bucket, But I got this error- NoSuchKey: The specified key does not exist. But keys are correct and I can upload images with these keys.

My code is:

var AWS = require('aws-sdk');
  s3 = new AWS.S3();
  var params = {
        Bucket:  config.get("aws.s3.bucket"),
        Key: config.get("aws.credentials.secretAccessKey")
    };
    s3.getObject(params, function (err, data) {
        console.log("data");
        if (err) console.log(err, err.stack); // an error occurred
        else console.log(data);
    });
}

Can anyone please tell me where I am doing wrong?

Answer

Ninja picture Ninja · Jun 18, 2017

There are problems related to how to use aws-sdk and it should be as following example:

var aws = require('aws-sdk');
aws.config.update({
  accessKeyId: {{AWS_ACCESS_KEY}},
  secretAccessKey: {{AWS_SECRET_KEY}}
});
var s3 = new aws.S3();
var s3Params = {
    Bucket:  {{bucket name}},
    Key: {{path to dedicated S3 Object (folder name + file/object 
    name)}}
};
s3.getObject(s3Params, function (err, data) {
  //Continue handling the returned results.
});

replace the strings inside {{}} with correct data and it should work well.