Hassan Baig Hassan Baig - 4 months ago 21
Python Question

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.

Answer

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.

Comments