如何在django环境中使用boto访问AWS凭证?

时间:2016-12-30 15:03:48

标签: django amazon-web-services amazon-s3 boto

我无法通过django-s3-storages中间件在我的django环境中创建S3连接(我从S3获得了403响应)。

Boto似乎无法获取环境设置,我怀疑这是原因(回溯没有多大帮助)。作为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(和其他帖子)表明这些设置应该有效:

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

为什么不能连接?

1 个答案:

答案 0 :(得分:1)

django存储中间件使用自己的S3Storage class。该课程了解django的settings.py,如果已配置,则会使用settings.AWS_ACCESS_KEY_IDsettings.AWS_SECRET_ACCESS_KEY

boto3本身并不了解django设置文件,因此它不会使用您在该文件中配置的任何内容。这就是为什么在尝试建立S3连接时必须指定boto3的密钥和密钥。