Amazon S3 Specify Upload to Folder Django

File upload and download are some of the most performed deportment on the web. With the rise of cloud-based solutions, companies are moving from an on-bounds solution to cloud storage. A couple reasons beingness its cheaper cost and convenience.

This article will demonstrate how to upload user-generated files to Amazon S3 using the Django Rest Framework.

Tabular array of contents

  1. Prerequisites

  2. What is AWS S3?

  3. Building a simple Django Rest API application

  4. Integrating AWS S3 into the Django Rest API awarding

  5. Summary

Prerequisites

To follow this article forth it would be helpful if the reader is comfy working with Django and Django Rest Framework.

What is AWS S3?

AWS S3 is an acronym for Amazon Spider web Services Simple Storage Service (AWS S3). It is a deject-based service by Amazon for object storage.

Object storage is a type of storage where items are processed as a data object. Contrary to the traditional method of storing files in the file arrangement bureaucracy.

In the traditional file system, the bones unit of storage is a "file". In AWS S3, the bones unit of storage is called a "bucket".

The AWS console and available SDKs from AWS are used to access buckets. These SDKs come up in supported popular languages such as Python and PHP.

There are several advantages of using AWS S3.

These includes:

  • Scalability
  • High operation
  • Audit adequacy
  • Security
  • Cost-effective
  • Virtually 99.999% availability (uptime)

AWS S3 can be used equally a fill-in and disaster recovery tool equally well equally in information analytics.

In this guide, we will upload user-generated files using the Django Rest Framework.

Building a unproblematic Django Remainder API application

Nosotros are going to create a new Django project named Dropboxer. Dropboxer is a simple file storage awarding. We can find the complete source lawmaking for this project in this repository.

Execute the commands below to gear up the project.

            pip install django pip install djangorestframework django-admin startproject dropboxer cd dropboxer python manage.py startapp uploader                      

The folder structure looks like the construction below:

            - dropboxer - uploader - manage.py                      

Add together the new app to INSTALLED_APPS in settings.py:

                                        # Application definition              INSTALLED_APPS              =              [              'django.contrib.admin',              'django.contrib.auth',              'django.contrib.contenttypes',              'django.contrib.sessions',              'django.contrib.letters',              'django.contrib.staticfiles',              # 3rd party apps              'rest_framework',              # local apps              'uploader',              # new              ]                            # Static files (CSS, JavaScript, Images)              # https://docs.djangoproject.com/en/ii.2/howto/static-files/              STATIC_URL              =              '/static/'              STATIC_ROOT              =              os.path.bring together(BASE_DIR,              'static') MEDIA_URL              =              '/media/'              MEDIA_ROOT              =              bone.path.bring together(BASE_DIR,              'media')                      

Add the code snippet below to urls.py file in the dropboxer project directory.

                          from              django.conf              import              settings              # new              from              django.conf.urls.static              import              static              # new              from              django.contrib              import              admin              from              django.urls              import              path urlpatterns              =              [     path('admin/', admin.site.urls), ]              if              settings.DEBUG:     urlpatterns              +=              static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)     urlpatterns              +=              static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)                      

Create serializers.py and urls.py files in the uploader app.

In models.py file, we create a simple model that represents a single file.

                          from              django.db              import              models              class              DropBox(models.Model):     title              =              models.CharField(max_length=              xxx)     certificate              =              models.FileField(max_length=              30)     created_at              =              models.DateTimeField(auto_now_add=True)     updated_at              =              models.DateTimeField(auto_now=Truthful)              class              Meta:         verbose_name_plural              =              'Drop Boxes'                      

Add together the code snippet below in serializers.py file:

                          from              rest_framework              import              serializers              from              .models              import              DropBox              grade              DropBoxSerializer(serializers.ModelSerializer):              class              Meta:         model              =              DropBox         fields              =              '__all__'                      

In uploader/views.py:

                          from              rest_framework              import              viewsets, parsers              from              .models              import              DropBox              from              .serializers              import              DropBoxSerializer              class              DropBoxViewset(viewsets.ModelViewSet):       queryset              =              DropBox.objects.all()     serializer_class              =              DropBoxSerializer     parser_classes              =              [parsers.MultiPartParser, parsers.FormParser]     http_method_names              =              ['get',              'post',              'patch',              'delete']                      

In uploader/urls.py:

                          from              rest_framework.routers              import              SimpleRouter              from              .views              import              DropBoxViewset router              =              SimpleRouter() router.register('accounts', DropBoxViewset) urlpatterns              =              router.urls                      

In uploader/admin:

                          from              django.contrib              import              admin              from              .models              import              DropBox admin.site.annals(DropBox)                      

In dropboxer/urls.py:

                          ...              from              django.urls              import              path, include              # new              urlpatterns              =              [     path('admin/', admin.site.urls),     path('api/', include('rest_framework.urls')),              # new              path('', include('uploader.urls')),              # new              ]                      

To create a table in the database from the model that we created above, execute the command beneath.

            python manage.py makemigrations python manage.py migrate                      

Start the development server past executing the control below:

            python manage.py runserver                      

On the browser navigate to accounts. We find the sample file uploaded in the media binder.

This sample file was created when nosotros uploaded information technology via the API on the browser as shown below:

media_folder_included_vs_code

sample_upload_via_api

Integrating AWS S3 into the Django API awarding

We accept a working awarding API endpoint. You lot need an AWS account to enable the integration of AWS S3 into your Django application.

Sign up if y'all do not already have an business relationship, sign in if yous take an existing AWS business relationship.

Search for S3:

search_for_s3

Click on "create bucket" button:

click_create_bucket

Provide a unique proper name for your S3 saucepan that is globally identified:

globally_named_bucket

Naming an AWS S3 saucepan may take some trial and fault before a name that does not already be is discovered.

Go along default option, click create bucket:

finish_bucket_creation

You will be redirected to the AWS S3 console which now shows the newly created bucket:

successful_create_bucket

We have successfully created an AWS S3 saucepan. Recall during bucket creation, public access to the S3 bucket was blocked.

To admission the created bucket from our application, we will demand to gain access using AWS IAM. AWS IAM is an acronym for Identity and Access Management.

It is used to provide access to rights and privileges on AWS resources. Currently, we can only access the S3 bucket through the console.

AWS allows access to its resource such equally AWS S3 through User and Roles. Yous can read more than about how AWS does this hither

Search for IAM using AWS search bar:

search_iam

Click User on the IAM side menu:

click_user

Click Add user on the IAM User dashboard:

add_user

Provide a user name and check the programmatic access box:

assign_username_and_access_type

In Set Permissions, choose "Attach existing policies directly" and check AWSS3FullAcess box:

set_permissions

Click through and review your selection before creating the user:

user_summary

On successful creation, information technology generated an AWS Access and Underground fundamental:

access_key_generated

Store the AWS Clandestine Central before finishing because the Secret Key won't exist shown over again.

Once washed, we tin can view the newly created AWS user on the IAM User dashboard:

new_user

In this commodity, nosotros will utilise Django-storages to connect to the AWS S3 saucepan.

Django-storages is a drove of custom storage backends for Django framework. We will use the AWS S3 integration from the collection in Django-storages package.

Install django-storages into the Django awarding via pip:

            pip install django-storages                      

In settings.py add the code snippet below:

                          ...              AWS_ACCESS_KEY_ID              =              <YOUR AWS Admission KEY>              AWS_SECRET_ACCESS_KEY              =              <YOUR AWS SECRET Fundamental>              AWS_STORAGE_BUCKET_NAME              =              <YOUR AWS S3 BUCKET NAME>              AWS_S3_SIGNATURE_VERSION              =              's3v4'              AWS_S3_REGION_NAME              =              <YOUR AWS S3 BUCKET LOCATION>              AWS_S3_FILE_OVERWRITE              =              False AWS_DEFAULT_ACL              =              None AWS_S3_VERIFY              =              True DEFAULT_FILE_STORAGE              =              'storages.backends.s3boto3.S3Boto3Storage'                      

AWS_ACCESS_KEY_ID: is the cardinal identifier of the IAM User. It starts with "AK****"

AWS_SECRET_ACCESS_KEY: is the generated 40 alphanumeric characters.

AWS_S3_REGION_NAME: refers to the AWS Region in the S3 console dashboard. For example: us-east-1, european union-west-two.

AWS_S3_SIGNATURE_VERSION: is the version of the signature used for generating pre-signed URLs. AWS S3 buckets need the signature to grant access.

AWS_S3_FILE_OVERWRITE: when set to Truthful, AWS S3 overwrites a file with the aforementioned name and format. If set to Simulated, AWS appends unique strings to the newly uploaded file proper noun. It does not override the existing file.

Restart the development server past executing the command below:

            python manage.py runserver                      

On the browser, navigate to localhost and retry uploading a sample file:

aws_successful_upload

Click the link in the document field. Find the link at present has "s3.amazon**". You will be able to access the file. Here, the uploaded file is titled "Big O Cheatsheet":

remote_file

Summary

In this article, nosotros created an AWS S3 bucket and assigned IAM User with full access to the AWS S3 bucket. We uploaded files to the AWS S3 bucket using Access Primal and AWS Underground Fundamental from our Django Rest API application.

Happy coding!

References

  • Django-storages documentation

Peer Review Contributions past: Odhiambo Paul

brownhicharach.blogspot.com

Source: https://www.section.io/engineering-education/how-to-upload-files-to-aws-s3-using-django-rest-framework/

0 Response to "Amazon S3 Specify Upload to Folder Django"

Enregistrer un commentaire

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel