4

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?

Rufi
  • 2,529
  • 1
  • 20
  • 41
Shubham Verma
  • 8,783
  • 6
  • 58
  • 79

2 Answers2

3

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.

Ninja
  • 486
  • 4
  • 12
  • It would appear OP's misunderstanding is what a ["Key" is in S3](https://docs.aws.amazon.com/AmazonS3/latest/dev/UsingObjects.html). Your code example is correct. Though ideally, one should use [roles](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles.html) as opposed to hard-coding access keys/secrets. [Configuring local environ](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html). Though all that is beyond the scope of OP's question :) – cameck Jun 18 '17 at 18:40
  • It is beyond the scope of this question but you are right and what you explained i actually used to do in my codes :) – Ninja Jun 18 '17 at 18:45
  • Correct, I did notice that. I added the link for OP's benefit, not yours :) – cameck Jun 18 '17 at 18:47
0

This is because that img url doesnot exist with same user.Means that u put a img alone.jpeg in your postman when you upload image in aws.But in getobject you are posting image.jpg with same user.conclusion is that image should be same which you are uploading and then getting with same user. [when you are getting a getObject,you post this image with same user]

when you are uploading image to aws [1]: https://i.stack.imgur.com/WLh5v.png

but when you use getObject with another image with same user(which user's token you are using ),it will give the same error. When you user image.jpg instead [1]: https://i.stack.imgur.com/WLh5v.png

So use same image key. use image key which is coming from aws's response instead of url.

deep Kaur
  • 1
  • 2