简体   繁体   English

如何将django-storages用于媒体和静态文件?

[英]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_STORAGESTATICFILES_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_AUTHAWS_S3_CUSTOM_DOMAINAWS_PRELOAD_METADATA等,则也可以在此子类中更改它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在dropbox上使用django-storage进行媒体存储? - How to use django-storages for media storage on dropbox? Django存储没有检测到更改的静态文件 - Django-storages not detecting changed static files 如何使用 django-storages 在 AWS S3 存储桶中创建文件夹? - How can I make folder in AWS S3 bucket with django-storages? 一起使用django-storages和django-compressor时尝试压缩静态文件时出错 - Error when trying to compress static files when using django-storages and django-compressor together 如何使用django-storages将图像上传到Amazon S3? 在管理员作品中上传,但在网站表单中却没有 - How can I upload images to amazon S3 with django-storages? Uploading in admin works but in the website form does not 使用django-storages / boto在本地收集静态作品,但不在Heroku上收集 - Collect static works locally but no on Heroku using django-storages/boto 如何同时使用静态文件和媒体 - How to use both static files and media together 使用s3boto和django-storages的collecstatic修改文件 - collecstatic modified files using s3boto and django-storages django-storages和boto配置不正确 - django-storages and boto ImproperlyConfigured 如何在静态媒体文件中使用Django templatetags? - How to use Django templatetags in static media files?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM