Cannot load images in nodejs using aws-sdk

I tried using aws-sdk and knox and I get status code 301 trying to load images. I get a status code 301 and a message - "The bucket you are trying to access should 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);   
            }
        });
    });
}); 
+5
source share
3 answers

Have you tried .send ()?

I can download the code below on S3.

http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/AWSRequest.html

var s3object = {Bucket: 'mystuff', Key: name, Body : data['data']};
    s3.client.putObject(s3object).done(function(resp){
      console.log("Successfully uploaded data");
    }).fail(function(resp){
      console.log(resp);
    }).send();
+4
source

. , "US Standard", , . config.json - { "accessKeyId": "secretKey", "secretAccessKey": "secretAccessKey", "region": ""}

s3, → . https://s3.amazonaws.com/yourbucket/ yourbucket.s3.amazonaws.com/

--1 https://s3-us-west-1.amazonaws.com/yourbucket/ yourbucket.s3-us-west-1.amazonaws.com/

+7

SDK, , .

Link: http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#constructor_details

Excerpt:

var AWS = require('aws-sdk'); 

var s3 = new AWS.S3({ endpoint :'https://s3-your-region-varies.amazonaws.com' }),
    myBucket = 'your-bucket-name';

var params = {Bucket: myBucket, Key: 'myUpload', Body: "Test"};

s3.putObject(params, function(err, data) {
    if (err)  {
        console.log(err) 
    } else {
        console.log("Successfully uploaded data to "+myBucket+"/testKeyUpload");
    }
});

Alternatively, you can solve this problem by setting the region in your configuration file, and you just have to be more specific on behalf of your region.

0
source

All Articles