Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't upload images in nodejs using aws-sdk

I've tried using aws-sdk and knox and I get status code 301 trying to upload images. I get status code 301 and message - 'The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint. This works in php.

AWS.config.loadFromPath(__dirname + '/config/config.json');
fs.readFile(source, function (err, data) {
var s3 = new AWS.S3();
    s3.client.createBucket({Bucket: 'mystuff'}, function() {
       var d = {
            Bucket: 'mystuff',
            Key: 'img/test.jpg',
            Body: data,
            ACL: 'public-read'
            };
       s3.client.putObject(d, function(err, res) {
            if (err) {
                console.log("Error uploading data: ", err);
                callback(err); 
            } else {
                console.log("Successfully uploaded data to myBucket/myKey");
                callback(res);   
            }
        });
    });
}); 
like image 335
rigaman Avatar asked Jan 24 '13 13:01

rigaman


1 Answers

I actually solved this problem. In your config you have to have a region, since my bucket was "US Standard", I left my region blank and it worked. config.json - { "accessKeyId": "secretKey", "secretAccessKey": "secretAccessKey", "region": ""}

go to s3 management console select one of your files and click on proporties - > look at the file link. US standard https://s3.amazonaws.com/yourbucket/ host in your console window yourbucket.s3.amazonaws.com/

us-west-1 https://s3-us-west-1.amazonaws.com/yourbucket/ host in your console window yourbucket.s3-us-west-1.amazonaws.com/

like image 190
rigaman Avatar answered Nov 15 '22 07:11

rigaman