Anu*_*rag 3 django amazon-s3 django-storage
我想根据我的文件类型和文件名模式选择性地应用AWS标头,同时将它们上传到S3源.我正在使用django-storage与django 1.8.12
我可以在django- storages 文档中看到设置AWS_HEADERS ,但我似乎无法找到在某些文件上应用此设置的方法.如果有人可以指导我,我将不胜感激
最简单的是子类化storages.backends.s3boto.S3BotoStorage以引入所需的行为
from storages.backends.s3boto import S3BotoStorage
class MyS3Storage(S3BotoStorage):
def _save(self, name, content):
cleaned_name = self._clean_name(name)
name = self._normalize_name(cleaned_name)
_type, encoding = mimetypes.guess_type(name)
content_type = getattr(content, 'content_type',
_type or self.key_class.DefaultContentType)
# setting the content_type in the key object is not enough.
self.headers.update({'Content-Type': content_type})
if re.match('some pattern', cleaned_name) :
self.headers.update({'new custome header': 'value'})
if content_type == 'my/content_type':
self.headers.update({'new custome header': 'value'})
return super(MyS3Storage, self)._save(name, content)
Run Code Online (Sandbox Code Playgroud)
不要忘记编辑设置并更改文件存储定义.
DEFAULT_FILE_STORAGE = 'myapp.MyS3Storage'
Run Code Online (Sandbox Code Playgroud)
上面的代码主要来自S3BotoStorage类,我们的代码仅检查内容类型和名称以添加自定义标头.
| 归档时间: |
|
| 查看次数: |
482 次 |
| 最近记录: |