In our recent project, there was a requirement for uploading the media files and controlling their access. In the context of access control, we wanted our files to stay private by default, and add public access to them at run-time.

AWS provides a very robust storage service named Amazon Simple Storage Service (Amazon S3), and it also supports access control. It allows you to store and retrieve any amount of data at any time, from anywhere on the web.

Amazon Simple Storage Service (Amazon S3) is storage for the Internet. You can use Amazon S3 to store and retrieve any amount of data at any time, from anywhere on the web.

We created our project using Django Web Framework. So we needed a Python-based library through which we can access our AWS bucket and also configure the access control. The Django framework allows uploading media files and accessing through S3. We will learn how to upload the Django REST framework file to S3.

Read Also: Pros and Cons of Django Web Framework

We used boto3 to upload and access our media files over AWS S3.

Boto is the Amazon Web Services (AWS) SDK for Python, which allows Python developers to write software that makes use of Amazon services like S3 and EC2. Boto provides an easy to use, object-oriented API as well as low-level direct service access.

When we create a bucket on AWS S3, by default bucket content permissions are set to be private.

You can install boto3 by using the following code:

$ pip install boto3

Here is how to upload a Django rest framework file to S3

In the above piece of code, I am getting access to these files as per their extension.

For all PDF files we set public access, the remaining will be private by default.

For changing access permissions of the existing files stored over AWS S3, we can do the following:

You can create a signed URL to allow access to private content or make your bucket accessible only to the specific domain. This will help with Django secure file upload. Read our blog here to get the idea about this.

Consulting is free – let us help you grow!