【发布时间】:2013-06-05 20:44:27
【问题描述】:
我使用django-storages 和s3boto 作为后端。
我有一个带有两个文件夹的存储桶——一个用于static,一个用于media。我使用django-s3-folder-storage 实现了这一点。
除了使用模型保存到 S3 之外,我还想实现一个图像调整大小和缓存功能来将文件保存到 S3。为此,我直接与我的 S3 存储桶进行交互。该代码有效,但未在 S3 上设置 Content-Type。
在 iPython 中:
In [2]: from s3_folder_storage.s3 import DefaultStorage
In [3]: s3media = DefaultStorage()
In [4]: s3media
Out[4]: <s3_folder_storage.s3.DefaultStorage at 0x4788780>
测试我们正在访问正确的存储桶 - storage_test 是我之前创建的:
In [5]: s3media.exists('storage_test')
Out[5]: True
In [6]: s3media.open("test.txt", "w")
Out[6]: <S3BotoStorageFile: test.txt>
In [7]: test = s3media.open("test.txt", "w")
In [8]: test
Out[8]: <S3BotoStorageFile: test.txt>
In [9]: test.key.content_type = "text/plain"
In [10]: test.write("...")
In [11]: test.close()
In [12]: test = s3media.open("test.txt", "w")
In [13]: test.key.content_type
Out[13]: 'binary/octet-stream'
我也尝试过使用test.key.metadata 和test.key.set_metadata 来代替In [9]。他们都没有这样做。
如何设置正确的 Content-Type?
【问题讨论】:
标签: django amazon-s3 boto django-storage