How to upload file using django-storages to S3 without giving AWS credentials

445 Views Asked by At

I want to upload a file from my django-admin page to S3. I read the document of django-storages but my concern is that it requires AWS Credentials to be added in the settings.py. Which i don't think is a good idea when deploying a project on production level. I looked through the internet but wasn't satisfied with the solution. Can someone help me on this.

Currently i am keeping a local directory to save the file their first and then from there upload to S3 and then deleting the file from directory.

I want to use django-storages if it can be set-up without providing AWS credentials.

Or if there is any better way to do this file upload to s3 please let me know.

1

There are 1 best solutions below

0
On

You can use python-decouple

pip install python-decouple

Create a .env file in your root directory and add below key-pair

AWS_S3_ACCESS_KEY="ACCESS KEY"
AWS_S3_SECRET_KEY="SECRET KEY"

Then in your settings.py file

from decouple import config

....

AWS_ACCESS_KEY_ID = config("AWS_S3_ACCESS_KEY", default="NO_KEY")
AWS_SECRET_ACCESS_KEY = config("AWS_S3_SECRET_KEY", default="NO_KEY")

...