[英]How can I use django-storages for both media and static files?
I'm attempting to use http://django-storages.readthedocs.org/en/latest/backends/amazon-S3.html for serving both static files and uploaded media, but I'm not certain it's possible. 我试图用http://django-storages.readthedocs.org/en/latest/backends/amazon-S3.html为服务于静态文件和上传的媒体,但我不能肯定这是可能的。 Is there a documented way that I'm missing? 有没有我遗失的文件记录方式? Also, I would assume (hope) that you could configure a separate bucket for each, but I can't find any info on that. 另外,我假设(希望)您可以为每个配置单独的存储桶,但是我找不到任何信息。
Yes this is possible by configuring both DEFAULT_FILE_STORAGE
and STATICFILES_STORAGE
to use the S3 storage. 是的,可以通过将DEFAULT_FILE_STORAGE
和STATICFILES_STORAGE
配置为使用S3存储来实现。 However if you set 但是,如果您设置
DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
STATICFILES_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
then these will both use the default settings for the S3 storage, meaning they will both use the AWS_STORAGE_BUCKET_NAME
bucket. 那么它们都将使用S3存储的默认设置,这意味着它们都将使用AWS_STORAGE_BUCKET_NAME
存储桶。 The way to work around this is to create a small subclass of S3BotoStorage
in your project which uses a different setting for the bucket name. 解决此问题的方法是在项目中创建一个S3BotoStorage
小型子类,该子类对存储区名称使用不同的设置。
from django.conf import settings
from storages.backends.s3boto import S3BotoStorage
class S3StaticStorage(S3BotoStorage):
def __init__(self, *args, **kwargs):
kwargs['bucket'] = settings.AWS_STATIC_BUCKET_NAME
super(S3StaticStorage, self).__init__(*args, **kwargs)
You would then define the AWS_STATIC_BUCKET_NAME
setting to be whatever you want for your static bucket and change AWS_STATIC_BUCKET_NAME
to the path for this custom storage class. 然后,您可以将AWS_STATIC_BUCKET_NAME
设置定义为您的静态存储桶AWS_STATIC_BUCKET_NAME
设置,并将AWS_STATIC_BUCKET_NAME
更改为此自定义存储类的路径。
STATICFILES_STORAGE = 'dotted.path.to.storage.S3StaticStorage'
If you wanted to change other settings such as AWS_QUERYSTRING_AUTH
, AWS_S3_CUSTOM_DOMAIN
, AWS_PRELOAD_METADATA
, etc then you would change them in this subclass as well. 如果您想更改其他设置,例如AWS_QUERYSTRING_AUTH
, AWS_S3_CUSTOM_DOMAIN
, AWS_PRELOAD_METADATA
等,则也可以在此子类中更改它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.