Bhavya Chugh
Bhavya Chugh

Reputation: 9

How to connect s3 amazon to your django project?

I have uploaded the images as well as static files in the s3 bucket but now the error is when we go to the path images does not appear:

and when we inspect it, it shows the correct path where the image should be placed:

and when we access this from s3 we get to see every image here

Do we need to change the URLs? And if yes how to do it?

And the access is also public

Upvotes: 0

Views: 954

Answers (2)

Aalok kumar
Aalok kumar

Reputation: 319

First see your image path doesn't include media but S3 image path have that string to fully render the image so Include MEDIA_URL in project settings.py file.

MEDIA_URL = "/media/"

Include after urlpatterns/paths and don't forget to import

from django.conf.urls.static import static

urlpatterns =  [ ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Upvotes: 0

Theodore
Theodore

Reputation: 303

Dependencies

You will need to install two Python libraries:

  1. boto3
  2. django-storages

The boto3 library is a public API client to access the Amazon Web Services (AWS) resources, such as the Amazon S3. It’s an official distribution maintained by Amazon.

Amazon S3 Setup

The django-storages is an open-source library to manage storage backends like Dropbox, OneDrive and Amazon S3. It’s very convenient, as it plugs in the built-in Django storage backend API. In other words, it will make you life easier, as it won’t drastically change how you interact with the static/media assets. We will only need to add a few configuration parameters and it will do all the hard work for us.

Before we get to the Django part, let’s set up the S3 part. We will need to create a user that have access to manage our S3 resources.

Logged in the AWS web page, find the IAM in the list of services, it’s listed under Security, Identity & Compliance:

Proper

Go to the Users tab and click in the Add user button:

Proper2

Give a user name and select the programmatic access option:

enter image description here

Click next to proceed to permissions. At this point we will need to create a new group with the right S3 permissions, and add our new user to it. Follow the wizard and click in the Create group button:

enter image description here

Define a name for the group and search for the built-in policy AmazonS3FullAccess:

enter image description here

Click in the Create group to finalize the group creation process, in the next screen, the recently created group will show up selected, keep it that way and finally click in the button Next: Review:

enter image description here

Review the information, if everything is correct proceed to create the new user. Next, you should see this information:

Take note of all the information: User, Access key ID and the Secret access key. Save them for later.

Click in the Close button and let’s proceed. Now, it’s time to create our very first bucket.

Bucket is what we call a storage container in S3. We can work with several buckets within the same Django project. But, for the most part you will only need one bucket per website.

Click in the Services menu and search for S3. It’s located under Storage. If you see the screen below, you are in the right place.

enter image description here

Click in the + Create bucket to start the flow. Set a DNS-compliant name for your bucket. It will be used to identify your assets. In my case, I choose sibtc-static. So the path to my assets will be something like this: https://sibtc-static.s3.amazonaws.com/static/.

enter image description here

Leave the remaining of the settings as it is, proceed to the next steps just using the defaults and finally hit the Create bucket button. Next you should see the screen below:

enter image description here

Read more here to learn about installing..

Upvotes: 1

Related Questions