Using the cloud

The objective of keepyourphoto is to investigate about a simple and cheaper solution to safely store and catalog photos. Developed with Django + Django Rest Framework on the backend, and after testing different providers, the media files of the application are saved on AWS S3.

Media files represent the core of the platform, as all the uploaded and cataloged photos are treated as such. In that sense, and because it is not economically sustainable to store them on a web server, it is essentially the use of a storage provider like S3. A quick search on the most common server providers is enough to evaluate the cost of safeguarding gigabytes of images. And although it is difficult to calculate the real costs to store the images on S3, it’s significantly cheaper.



How to integrate S3 with Django?


The easiest solution to integrate Django with S3, is using django-storeges, which is a collection of custom storage backends for Django. Compatible with the currently supported versions of Django, provides a variety of storage backends in a single library. In that sense, it supports AWS S3, but also Azure Storage, Dropbox, Apache Libcloud or Google Cloud Storage.


Each storage backend has its own and unique settings, that need to be added to the Django settings.py file.


STORAGES = {
    "default": {
        "BACKEND": "storages.backends.s3.S3Storage",
        "OPTIONS": {
          ...your_options_here
        },
    },
}

STATICFILES_STORAGE = "storages.backends.s3.S3Storage"


In addition, AWS S3 credentials will be provided. This involves a previous registry to AWS, the creation of a new S3 bucket, some configurations and the generation of access and secret keys. There are lots of tutorials about how to perform these configurations, but How to Setup Amazon S3 in a Django Project is maybe one of the most useful ones.


Once AWS S3 is configured for the project, all media files will automatically be uploaded to the AWS S3 bucket.



Image security


One of the main challenges of keepyourphoto is to maintain the integrity and security of all the images. But, how to prevent the unauthorized access?

Django and Django Rest Framework include several authentication solutions to restrict and control the access through user credentials and tokens.


Also, the access URL to each photo stored on AWS S3 can be configured to expire after a certain period of time.


AWS_QUERYSTRING_EXPIRE = 600


Generated on the fly at each request, using the S3 and Django tokens, and expiring after a certain period of time, it is not possible access to the media files outside the platform for none authenticated users.



A faster access


The latency time to access an object stored in AWS S3 can be relatively long. During the development of keeyourphoto and after some speed tests, the time required to recover the photos was not acceptable.

In order to solve that problem, it’s necessary to integrate Amazon Cloud Front, a Content Delivery Network (CDN) that speeds up the distribution of the static files.


Amazon Cloud Front requires, as S3, multiple configurations and some headaches. There are multiple options available at AWS web page, and it’s necessary to pay attention to configure the one that best suits the requirements of our project. Then, it’s also necessary to perform some implementations on Django.


There are multiple tutorials about how to integrate Cloud Front with Django, but this article is very useful.



Estimating costs


What about the cost to use and implement this cloud services? Maybe this is the most difficult question to answer.


The cost of S3 depends on multiple aspects, such as the storage size, requests, data transfer, object’s size, etc.

There are also multiple classes available: S3 Standard, S3 Intelligent-Tiering, S3 Standard-Infrequent Access, S3 One Zone-Infrequent Access, S3 Express One Zone, S3 Glacier Instant Retrieval, etc. It’s important to pay attention to the characteristics of each one, and choose the best for our project. It can save us a lot of costs.


Amazon Cloud Front charges for data transfers out from its edge locations, along with HTTP or HTTPS requests. Pricing varies by usage type, geographical region, and feature selection.



And now?


keepyourphoto is continuously under development. Now that the AWS services are running on production, new developments will focus on improving auto-registry and payment methods.


And remember, you can test the platform using the free plan and report errors and suggestions.