我使用 Django的货仓
与 s3boto
作为后端。
我有一个桶两个文件夹 - 一个是静态
,一个是传媒
。我做到这一点使用 Django的S3文件夹存储
。
I have one bucket with two folders - one for static
and one for media
. I achieve this using django-s3-folder-storage
.
除了保存到S3采用的模式,我也想实现的图像调整大小并缓存功能,将文件保存到S3。要做到这一点,我直接交流与我的S3桶。在code工作,但内容类型
未在S3中设定。
As well as saving to S3 using a model, I also want to implement an image-resize-and-cache function to save the files to S3. To do this I interact directly with my S3 bucket. The code works, but the Content-Type
isn't set on S3.
在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
是一个我前面创建的:
Test we're accessing the right bucket - storage_test
is one I created earlier:
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'
我也试过来代替 [9]
使用 test.key.metadata 和
test.key.set_metadata 。他们没有这样做。
I've also tried instead of In [9]
using test.key.metadata
and test.key.set_metadata
. None of them do it.
我如何设置正确的内容类型?
How do I set the correct Content-Type?
如果您通过源$ C $ C类 S3BotoStorageFile
及功能写
,标题是从只有2个名额更新,
If you go through the source code in class S3BotoStorageFile
and function write
, the header is updated from only 2 places,
upload_headers.update(self._storage.headers)
,其中 self._storage.headers
摘自 AWS_HEADERS
self._storage.default_acl
upload_headers.update(self._storage.headers)
where self._storage.headers
is taken from AWS_HEADERS
self._storage.default_acl
和函数 _flush_write_buffer
仅 self._storage.headers
被认为是。检查行标题= self._storage.headers.copy()
And in function _flush_write_buffer
only self._storage.headers
is considered. Check for the line headers = self._storage.headers.copy()
所以更新 test.key.content_type
将无法正常工作。
So updating test.key.content_type
will not work.
而不是 test.key.content_type =text / plain的
在 [9]:
尝试使用 test._storage.headers ['的Content-Type] ='text / plain的
,它应该工作。
Instead of test.key.content_type = "text/plain"
at In [9]:
try using test._storage.headers['Content-Type'] = 'text/plain'
, it should work.