3

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?

4

1 回答 1

3

查看 django-storages 的 amazon s3 后端。然后,您可以将 csv 文件直接发送到 s3,而无需在本地保存。

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

从文档:

>>> 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
于 2013-06-27T00:30:06.907 回答