3

Good afternoon,

I'm trying to set up a connection to my aws product api, however I keep getting a 301 Permanent Redirect Error as follows:

{ [PermanentRedirect: The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.]
  message: 'The bucket you are attempting to access must be addressed using the specified endpoint. Please send all future requests to this endpoint.',
  code: 'PermanentRedirect',
  name: 'PermanentRedirect',
  statusCode: 301,
  retryable: false }

The code I am using to connect to the API is as follows:

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

//Setting up the AWS API
aws.config.update({
    accessKeyId: 'KEY',
    secretAccessKey: 'SECRET',
    region: 'eu-west-1'
})

var s3 = new aws.S3();

s3.createBucket({Bucket: 'myBucket'}, function() {
    var params = {Bucket: 'myBucket', Key: 'myKey', Body: 'Hello!'};
    s3.putObject(params, function(err, data) {
        if (err)
            console.log(err)
        else
            console.log("Successfully uploaded data to myBucket/myKey");
    });
});

If I try using different regions, like us-west-1 I just get the same error.

What am I doing wrong?

Thank you very much in advance!

Cœur
  • 37,241
  • 25
  • 195
  • 267
axsauze
  • 343
  • 4
  • 14
  • Oh ok, how would I make it work then - or what's wrong? – axsauze Apr 20 '13 at 13:57
  • Might be stating the obvious here: follow the redirect. However it could be a deficiency with the API you are using which does not follow the redirects for you. – Steve-o Apr 20 '13 at 13:57
  • [This answer is related](http://stackoverflow.com/a/14511996/175849). – Steve-o Apr 20 '13 at 14:01
  • Woo! Fixed it! The problem was that I didn't create any Bucket in my S3 Console! Thank you anyways bro! – axsauze Apr 20 '13 at 14:04

3 Answers3

5

I have fixed this issue:

You have to make sure that you already have created a bucket with the same name; in this case, the name of the bucket would be 'myBucket'.

s3.createBucket({Bucket: 'myBucket'}, function() {
var params = {Bucket: 'myBucket', Key: 'myKey', Body: 'Hello!'}; 

Once you created the bucket, go to properties and see what region it is using - add this into:

aws.config.update({
    accessKeyId: 'KEY',
    secretAccessKey: 'SECRET',
    region: 'eu-west-1'
})

Now it should work! Best wishes

axsauze
  • 343
  • 4
  • 14
2

I've just come across this issue and I believe the example at http://aws.amazon.com/sdkfornodejs/ is incorrect.

The issue with the demo code is that Bucket names should be in lowercase - http://docs.aws.amazon.com/AmazonS3/latest/dev/BucketRestrictions.html

Had the demo actually output the err argument on the callback in s3.createBucket this would have been immediately obvious.

Also bucket names need to be unique in the region your creating them.

s3.createBucket({Bucket: 'mylowercaseuniquelynamedtestbucket'}, function(err) {

    if(err){
        console.info(err);
    }else{
        var params = {Bucket: 'mylowercaseuniquelynamedtestbucket', Key: 'testKey', Body: 'Hello!'};
        s3.putObject(params, function(err, data) {
            if (err)
                console.log(err)
            else
                console.log("Successfully uploaded data to testBucket/testKey");
        });
    }

 })
Grant Trevor
  • 1,052
  • 9
  • 23
2

Was stuck on this for a while...

Neither setting the region name or leaving it blank in my configuration file fixed the issue. I came across the gist talking about the solution and it was quite simple:

Given:

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

Prior to instantiating your S3 object do the following:

AWS.config.update({"region": "us-west-2"}) //replacing your region there

I was then able to call the following with no problem:

var storage = new AWS.S3();
var params = {
                Bucket: config.aws.s3_bucket,
                Key: name,
                ACL:'authenticated-read',
                Body: data
            }
storage.putObject(params, function(storage_err, data){...})
Airswoop1
  • 421
  • 4
  • 7