Javaaaa
Javaaaa

Reputation: 3804

How to best save a file from Django on Heroku to S3

I have a Django application that makes a .csv file (using csv.writer) and then uploads it to Amazon S3.

How I do it now locally is to make the csv file, store it locally and then save that file to S3. However with heroku and it's "filesytem" I thought it might be better to skip that 'store locally' step.

Is there a way to output a csv file directly to S3 from within python?

Upvotes: 3

Views: 1293

Answers (1)

nickromano
nickromano

Reputation: 908

Check out the amazon s3 backend for django-storages. You can then send your csv file straight to s3 without having to save it locally.

http://django-storages.readthedocs.org/en/latest/backends/amazon-S3.html

From the docs:

>>> from django.core.files.storage import default_storage
>>> from django.core.files.base import ContentFile
>>> from django.core.cache import cache
>>> from models import MyStorage

>>> default_storage.exists('storage_test')
False
>>> file = default_storage.open('storage_test', 'w')
>>> file.write('storage contents')
>>> file.close()

>>> default_storage.exists('storage_test')
True
>>> file = default_storage.open('storage_test', 'r')
>>> file.read()
'storage contents'
>>> file.close()

>>> default_storage.delete('storage_test')
>>> default_storage.exists('storage_test')
False

Upvotes: 3

Related Questions