Amazon S3 Python and Django

Basic understanding of using Python and Django with S3

Eloy Zuniga Jr.
https://github.com/eloyz/

  • Amazon provides access control in the form of ACL (access control lists)
  • You can use the boto package to connection to S3
  • AWS S3 requires you to create buckets; they must have globally unique names
  • Buckets are not meant to be viewed as folders; they have keys, not directories and files
  • keys have a key, which is essentially it's path from which it will be served, however
  • keys are not written to S3 until you set their contents
  • you can also set metadata, like Content-Type

To use S3 with django:

$ pip install django-storages  

Then, in your project, make a storage.py:

from storages.backends.s3boto import S3BotoStorage

StaticRootS3BotoStorage = lambda: S3BotoStorage(locaiton='static')
MediaRootS3BotoStorage = lamdba: S3BotoStorage(location='media')

In settings.py:

# or whatever these Django settings are
STATICFILE_STORAGE = 'storage.StaticRootS3BotoStorage'
MEDIA_FILE_STORAGE = 'storage.MediaRootS3BotoStorage'

In a view:

from django.core.files.storage import default_storage

my_path = '/some/key/file.txt'
my_file = default_storage.open(path)