使用 django-storages 上传静态文件时有选择地应用 AWS Headers
Selectively apply AWS Headers while uploading static files using django-storages
我想在将它们上传到 S3 源时根据我的文件类型和文件名模式有选择地应用 AWS headers。我正在使用 django-storages 和 django 1.8.12
我可以在 django-storages documentation 中看到设置 AWS_HEADERS,但我似乎无法找到仅在某些文件上应用此设置的方法。
如果有人可以指导我,我将不胜感激
最简单的是subclass 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)
不要忘记编辑设置和更改文件存储定义。
DEFAULT_FILE_STORAGE = 'myapp.MyS3Storage'
以上代码主要来自S3BotoStorage class,我们的代码只是检查内容类型和名称来添加自定义headers.
# handlers.py
import mimetypes
from storages.backends.s3boto import S3BotoStorage
from django.conf import settings
class ManagedS3BotoStorage(S3BotoStorage):
def _save(self, name, content):
cleaned_name = self._clean_name(name)
_type, encoding = mimetypes.guess_type(name)
content_type = getattr(content, 'content_type',
_type or self.key_class.DefaultContentType)
self.headers.update(self.get_headers(cleaned_name, content_type))
return super(ManagedS3Storage, self)._save(name, content)
def get_headers(cleaned_name, content_type):
// if possible, load AWS_HEADERS_LIST from settings
headers_list = settings.AWS_HEADERS_LIST
// logic for updating headers & return headers
settings.py 文件中的 # 个更改
DEFAULT_FILE_STORAGE = 'handlers.ManagedS3BotoStorage'
其他答案是关于S3BotoStorage
,这个答案是关于S3Boto3Storage
(注意Boto
后面的3
)。你需要覆盖 _save_content
,像这样:
class CustomBoto3Storage(S3Boto3Storage):
def _save_content(self, obj, content, parameters):
"""We want .mp3 files to have the Content-Disposition header set to
attachement in this example."""
new_parameters = parameters.copy() if parameters else {}
if new_parameters.get('ContentType') in ['audio/mpeg3', 'audio/x-mpeg-3', 'audio/mpeg']:
new_parameters['ContentDisposition'] = 'attachment'
return super()._save_content(obj, content, new_parameters)
我想在将它们上传到 S3 源时根据我的文件类型和文件名模式有选择地应用 AWS headers。我正在使用 django-storages 和 django 1.8.12
我可以在 django-storages documentation 中看到设置 AWS_HEADERS,但我似乎无法找到仅在某些文件上应用此设置的方法。 如果有人可以指导我,我将不胜感激
最简单的是subclass 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)
不要忘记编辑设置和更改文件存储定义。
DEFAULT_FILE_STORAGE = 'myapp.MyS3Storage'
以上代码主要来自S3BotoStorage class,我们的代码只是检查内容类型和名称来添加自定义headers.
# handlers.py
import mimetypes
from storages.backends.s3boto import S3BotoStorage
from django.conf import settings
class ManagedS3BotoStorage(S3BotoStorage):
def _save(self, name, content):
cleaned_name = self._clean_name(name)
_type, encoding = mimetypes.guess_type(name)
content_type = getattr(content, 'content_type',
_type or self.key_class.DefaultContentType)
self.headers.update(self.get_headers(cleaned_name, content_type))
return super(ManagedS3Storage, self)._save(name, content)
def get_headers(cleaned_name, content_type):
// if possible, load AWS_HEADERS_LIST from settings
headers_list = settings.AWS_HEADERS_LIST
// logic for updating headers & return headers
settings.py 文件中的 # 个更改
DEFAULT_FILE_STORAGE = 'handlers.ManagedS3BotoStorage'
其他答案是关于S3BotoStorage
,这个答案是关于S3Boto3Storage
(注意Boto
后面的3
)。你需要覆盖 _save_content
,像这样:
class CustomBoto3Storage(S3Boto3Storage):
def _save_content(self, obj, content, parameters):
"""We want .mp3 files to have the Content-Disposition header set to
attachement in this example."""
new_parameters = parameters.copy() if parameters else {}
if new_parameters.get('ContentType') in ['audio/mpeg3', 'audio/x-mpeg-3', 'audio/mpeg']:
new_parameters['ContentDisposition'] = 'attachment'
return super()._save_content(obj, content, new_parameters)