Hassan Baig
Hassan Baig

Reputation: 15824

Error uploading to a non-US Standard AWS S3 Bucket in Django application; US Standard bucket works perfectly

I set up an S3 bucket in a non-US region (Singapore). When I try to upload images to it, I get a 301 (Permanently moved) error from S3. Researching has led me to understand that for a non-standard bucket, one must specify the particular endpoint and region.

I've tried a variety of configuration variables in my settings.py, but it never works. Note that boto and django-storages are installed, and I've gotten it to work for a US standard bucket before.

The following is how it's currently set up (plus all the different variations I've tried):

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_S3_FORCE_HTTP_URL = True
AWS_S3_SECURE_URLS = False
AWS_S3_URL_PROTOCOL = 'http'
AWS_QUERYSTRING_AUTH = False
AWS_SECRET_ACCESS_KEY = os.environ.get('awssecretkey')
AWS_ACCESS_KEY_ID = os.environ.get('awsaccesskeyid')
AWS_S3_CALLING_FORMAT='boto.s3.connection.OrdinaryCallingFormat'
AWS_STORAGE_BUCKET_NAME = 'bucket.my'
#HOST ='s3-ap-southeast-1.amazonaws.com'
#AWS_ENDPOINT = 's3-ap-southeast-1.amazonaws.com'
#AWS_S3_REGION = 's3-ap-southeast-1'
#REGION = 's3-ap-southeast-1'
#AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
#BOTO_S3_HOST = 'bucket.my.s3-ap-southeast-1.amazonaws.com'
#S3_HOST = 'http://bucket.my.ap-southeast-1.amazonaws.com'
#'bucket.my.s3-website-ap-southeast-1.amazonaws.com'
#'s3-ap-southeast-1.amazonaws.com'
#BOTO_BUCKET_LOCATION = 'Singapore'

#S3Connection.DefaultHost = 's3-ap-northeast-1.amazonaws.com'
#HOST = 'http://bucket.my.ap-southeast-1.amazonaws.com'

Can anyone help me with the correct configuration? I'll provide more details in case you need them.

Upvotes: 2

Views: 1013

Answers (3)

Léo Chaz Maltrait
Léo Chaz Maltrait

Reputation: 669

AWS_AUTO_CREATE_BUCKET = True
AWS_S3_HOST = 's3-eu-west-1.amazonaws.com'

This works also for django-storages. Thanks @gmcc051

Upvotes: 0

gmcc051
gmcc051

Reputation: 410

I'm using the current version of django-storages-redux and had to use the following in settings.py in order to use the Sydney S3 region.

import boto
from boto.s3.connection import OrdinaryCallingFormat, Location

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_ACCESS_KEY_ID = 'XXX'
AWS_SECRET_ACCESS_KEY = 'XXX'
AWS_STORAGE_BUCKET_NAME = 'my.bucket.name'
AWS_AUTO_CREATE_BUCKET = False
AWS_S3_HOST = 's3-ap-southeast-2.amazonaws.com'
AWS_S3_CALLING_FORMAT = 'boto.s3.connection.OrdinaryCallingFormat'

Note the AWS_S3_HOST value; that is the parameter that I needed to solve the "301 (Permanently moved)" error.

Upvotes: 3

Michael - sqlbot
Michael - sqlbot

Reputation: 179064

"Singapore" is not a valid "region" in the sense that you need, here:

AWS_S3_REGION = 'ap-southeast-1'

http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region

This may not be the only issue, but it should be one of the issues, at least.

Upvotes: 2

Related Questions