jeudi 24 septembre 2015

Node JS aws-sdk getting 403 forbidden error

I am now developing Node JS module and one of its functionalities is to show the files that exist in Amazon s3 bucket. Now I know Amazon AccessKeyID and SecretAccessKey, bucket name also. So, I wrote my code as follows.

aws = require("aws-sdk");
fs = require("fs");

fs.readFile('config/amazon-s3.cfg', 'utf8', function(err, content) {
    if (err) {
        console.log("Cannot read Amazon Config file.");
    } else {
        config = JSON.parse(content);

        aws.config.update(
            {
                accessKeyId: config.accessKeyId,
                secretAccessKey: config.secretAccessKey,
                // region: config.region
            }
        );

        var s3 = new aws.S3();

        s3.listObjects({Bucket: config.bucket}, function(err, data) {
            if (err) console.log(err, err.stack);
            else {

                var contents = data.Contents;
                console.log(contents);
            }
        });
    }
});

But when I run this code, I get always forbidden 403 error.

I tried it with several amazon accounts including my friends' accounts, so the amazon accessKeyID and secretAccessKey and Bucket info is not doubtful, but keep fails.

Can you please tell me what's wrong in my code?

Please help me!!!




Aucun commentaire:

Enregistrer un commentaire