Faisal Nazik
Faisal Nazik

Reputation: 2863

Amazon S3 Bucket Configuration Problem with Django settings. <Code>AccessDenied</Code>

I'm Unable to set up an Amazon S3 Bucket Correctly. I followed the following steps to configure.

Installed boto3 django-storages

Created S3 Bucket with IAM User, And Block all public access is off

Settings.py has the following related code.

INSTALLED_APPS = [
    #...
    'storages',
]

AWS_ACCESS_KEY_ID = env('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = env('AWS_SECRET_ACCESS_KEY')
AWS_STORAGE_BUCKET_NAME = env('AWS_STORAGE_BUCKET_NAME')
AWS_S3_CUSTOM_DOMAIN = '%s.s3.amazonaws.com' % AWS_STORAGE_BUCKET_NAME
AWS_S3_OBJECT_PARAMETERS = {
    'CacheControl': 'max-age=86400',
}
AWS_LOCATION = 'static'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR / 'static'),
    os.path.join(BASE_DIR / 'build/static')
]
STATIC_URL = 'https://%s/%s/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
MEDIA_URL = 'https://%s/%s/images/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION)
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

Now the problem is When I go to the URL of the Uploaded file which is MEDIA_URL = 'https://%s/%s/images/' % (AWS_S3_CUSTOM_DOMAIN, AWS_LOCATION) I'm getting this error on accessing the Image file https://remote-hospital-v4.s3.us-east-2.amazonaws.com/static/images/IMG_18.jpg

enter image description here

And also files are still getting stored locally and are not getting uploaded to S3 Bucket

enter image description here

Help required to fix this, Or where the problem could be.Thanks!

Upvotes: 0

Views: 233

Answers (1)

Alain Bianchini
Alain Bianchini

Reputation: 4171

You need to add your Amazon Web Services access key in your settings (you only insert the secret key):

AWS_ACCESS_KEY_ID = env('AWS_ACCESS_KEY_ID')

Upvotes: 1

Related Questions