AWS CloudFront Signed URLs: A Guide to Enable Content Disposition Using Django and django-storages

Nic Lasdoce
30 May 20235 minutes read

Learn about the ten essential best practices for constructing scalable software architectures. From modularization and microservices to horizontal scaling, load balancing, caching, and more, this article explores key considerations and patterns that empower developers to build robust and scalable software systems. Ensure your applications can handle increased user demands, maintain optimal performance, and seamlessly accommodate future growth.

AWS CloudFront Signed URLs: A Guide to Enable Content Disposition Using Django and django-storages

Introduction

In the world of software architecture, AWS presents us with a myriad of tools that, when used effectively, can drastically streamline our processes. Among these is the CloudFront Signed URL. As proficient Django developers, we find ourselves needing to enable Content Disposition within these URLs. Our goal? Transforming original file URLs into downloadable links.

The Power of URLs and Content Disposition: A Brief Overview

AWS CloudFront service’s cornerstone is the Signed URL, a tool that opens up a realm of possibilities for online resources. But how do we demystify 'enabling Content Disposition' within these CloudFront Signed URLs?

Content Disposition, in essence, is a response header. It directs the desired action for a requested file. By enabling Content Disposition, we convert the original file URL into a downloadable link. The process involves passing a query string labeled 'Content Disposition Attachment'. While this might sound complex, it's a straightforward process once we break it down.

The Django Approach: Crafting Downloadable Links

Creating a downloadable link requires a nuanced understanding of how to manipulate CloudFront Signed URLs for optimal user experience. With Django and django-storages, this is made simpler. The django-storages package conveniently auto-signs URLs, and we can utilize this functionality to our advantage.

Detailed Steps to Enable Content Disposition on Cloudfront Signed URL Using Django

NOTE: This tutorial uses Django + django-storages and assumes you already have a working project that utilizes both.

1. Modify CloudFront Behaviour: Start by navigating to 'Cache key and origin requests' under CloudFront behaviour. Here, you need to create a new policy named 'AllowQueryStrings' and fill in the necessary values.

2. Create the AttachmentMediaStorage class: this will auto convert the file fields into downloadable links instead of simple signed links.

  • 2.1. In your project root directory create a file named "storage_backends.py"
  • 2.2 Add the script below to the new file
from abc import ABC
from storages.backends.s3boto3 import S3Boto3Storage
import os
class AttachmentMediaStorage(S3Boto3Storage, ABC):
def url(self, name, parameters=None, expire=None, http_method=None):
f_name = os.path.basename(name)
extra = {
'response-content-disposition': f'attachment; filename={f_name};'}
if not parameters:
parameters = extra
else:
parameters = {**parameters, **extra}
url = super().url(name, parameters=parameters, expire=expire, http_method=http_method)
return url

3. (Optional)Update Django Settings: Django's django-storages package automatically signs URLs. If you wish to make all file links to add the content disposition then go to your settings.py file and change the

DEFAULT_FILE_STORAGE=
to
DEFAULT_FILE_STORAGE = 'storage_backends.AttachmentMediaStorage'
. If not, then use it individually to your target file fields by going to step 4

4. Implement AttachmentMediaStorage Individually: Now,

AttachmentMediaStorage
can be used in model fields to override
DEFAULT_FILE_STORAGE
, allowing us to leverage Content Disposition functionality effectively on individual file fields. Here's an example of how you can do this:

invoice = models.FileField(upload_to=file_upload_to, storage=AttachmentMediaStorage)

Further Reading

For a deeper dive into CloudFront URL signing, you can refer to this comprehensive guide: S3 CloudFront URL Signer.

Conclusion

AWS equips us with a wide range of tools that, with the right knowledge and application, can significantly enhance our workflow. By understanding how to enable Content Disposition in CloudFront Signed URLs using Django and django-storages, we can build efficient and user-friendly solutions. Stay tuned for more tips and tricks on mastering AWS.

Bonus

If you are a founder needing help in your Software Architecture or Cloud Infrastructure, we do free assessment and we will tell you if we can do it or not! Feel free to contact us at any of the following:
Social
Contact

Email: nic@triglon.tech

Drop a Message

Tags:
Software Development
TechStack
AWS
NodeJS

Nic Lasdoce

Software Architect

Unmasking Challenges, Architecting Solutions, Deploying Results

Member since Mar 15, 2021

Tech Hub

Unleash Your Tech Potential: Explore Our Cutting-Edge Guides!

Stay ahead of the curve with our cutting-edge tech guides, providing expert insights and knowledge to empower your tech journey.

View All
Struggling with Database Performance? Discover These 5 Aurora Patterns for Better Operations
30 Jun 20242 minutes read
Monolith: From Zero to Millions
14 May 20244 minutes read
View All

Get The Right Job For You

Subscribe to get updated on latest and relevant career opportunities