How to Collect Static Files in a Django Application Deployed on Digital Ocean and Store Them in an Amazon S3 Bucket

What will you learn?

In this tutorial, you will discover how to configure your Django application to collect static files and securely store them in an Amazon S3 bucket while being deployed on Digital Ocean.

Introduction to the Problem and Solution

When deploying a Django application, it is essential to correctly configure the project settings to collect static files and then store them in an Amazon S3 bucket. This process involves setting up the Django project for static file collection and configuring Amazon S3 as the storage backend for these files.

The solution revolves around making necessary changes primarily in the Django project settings related to static file handling. Additionally, you will set up an Amazon S3 bucket with proper permissions to securely store these static files.

Code

# settings.py

# Install boto3 using pip if not already installed: pip install boto3

AWS_STORAGE_BUCKET_NAME = 'your-s3-bucket-name'
AWS_ACCESS_KEY_ID = 'your-access-key-id'
AWS_SECRET_ACCESS_KEY = 'your-secret-access-key'
AWS_S3_CUSTOM_DOMAIN = f'{AWS_STORAGE_BUCKET_NAME}.s3.amazonaws.com'

STATIC_URL = f'https://{AWS_S3_CUSTOM_DOMAIN}/static/'
STATICFILES_STORAGE = 'storages.backends.s3boto3.S3Boto3Storage'

# Copyright PHD

Remember to replace ‘your-s3-bucket-name’, ‘your-access-key-id’, and ‘your-secret-access-key’ with your actual values.

Code Explanation:

  • AWS_STORAGE_BUCKET_NAME: Represents the name of your Amazon S3 bucket.
  • AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY: Your AWS IAM credentials.
  • STATIC_URL: Indicates the URL from which static assets will be served.
  • STATICFILES_STORAGE: Specifies that static files should be stored using S3 storage.

Note: Ensure your AWS credentials are properly configured.

Explanation

  1. Setting up Boto: Boto is a Python library used for interacting with AWS services like Amazon S#. Install it using pip.
  2. Configuring Settings: Update your Django project’s settings.py with the provided code snippet. This configures Django’s staticfiles app to use Amazon S# as its storage backend for collecting and serving static file content during deployment.
    How do I install boto?

    To install boto, use pip: pip install boto.

    What role does STATICFILES_STORAGE play?

    STATICFILES_STORAGE specifies which storage system should be used by Django�s collectstatic management command.

    Where do I find my AWS access key ID?

    You can find your AWS access key ID in your AWS account under the Security Credentials section after logging into the IAM console.

    Can I use environment variables for sensitive information like keys?

    Yes, it is recommended practice for security reasons rather than hardcoding them directly into the codebase.

    Do I need CORS configuration on my S# Bucket?

    Depending on requirements for accessing resources from different origins or domains, you may need to configure Cross-Origin Resource Sharing (CORS) headers on your Amazon S# bucket.

    Conclusion

    In conclusion, this guide has demonstrated how you can seamlessly configure your Django project deployed on Digital Ocean to automatically collect static files during deployment by storing them in an Amazon S# bucket. By following these steps, you can ensure efficient handling of your application’s static assets without manual intervention.

    Leave a Comment