Escher
Escher

Reputation: 5795

How to make AWS credentials accessible to boto in django environment?

I'm unable to create an S3 connection in my django environment via django-s3-storages middleware (I'm getting a 403 response from S3).

Boto doesn't seem to be able to pick up the environment settings, and I suspect this is the cause (the traceback isn't helping much). As a diagnosis in manage.py shell:

import boto
boto.connect_s3()
>>> boto.exception.NoAuthHandlerFound: No handler was ready to authenticate. 1 handlers were checked. ['HmacAuthV1Handler'] Check your credentials

from django.conf import settings
boto.connect_s3(settings.AWS_ACCESS_KEY_ID, settings.AWS_SECRET_ACCESS_KEY)
>>> S3Connection:s3.amazonaws.com

The docs (and other posts) indicate that these settings should work:

MEDIAFILES_LOCATION = 'media'
AWS_S3_CUSTOM_DOMAIN = 'my-bucket.s3-website-eu-west-1.amazonaws.com'
AWS_S3_HOST = 's3-website-eu-west-1.amazonaws.com'
MEDIA_URL = "https://%s/%s/" % (AWS_S3_CUSTOM_DOMAIN, MEDIAFILES_LOCATION)
DEFAULT_FILE_STORAGE = 'django_s3_storage.storage.StaticS3Storage'

#S3 settings from https://github.com/etianen/django-s3-storage
AWS_ACCESS_KEY_ID = "xxx"
AWS_SECRET_ACCESS_KEY = "yyy"
AWS_S3_BUCKET_NAME = "my-bucket"
AWS_S3_CALLING_FORMAT = "boto.s3.connection.OrdinaryCallingFormat"

# Make user uploaded files public
AWS_S3_BUCKET_AUTH = False
AWS_S3_MAX_AGE_SECONDS = 60*60*24*365 # 1 year
AWS_S3_GZIP = True

Why isn't boto able to connect?

Upvotes: 0

Views: 3750

Answers (1)

wkl
wkl

Reputation: 80031

The django storage middleware uses its own S3Storage class. That class has knowledge of django's settings.py and will use settings.AWS_ACCESS_KEY_ID and settings.AWS_SECRET_ACCESS_KEY if they're configured.

boto3 by itself has no knowledge of the django settings file, so it doesn't use anything you configure in that file. That's why you have to specify the key and secret to boto3 when trying to establish an S3 connection.

Upvotes: 2

Related Questions