Django的存储器与多个S3桶 [英] django-storages with multiple S3 Buckets

查看:171
本文介绍了Django的存储器与多个S3桶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用AWS和我将在我的应用程序不同的桶。我也使用Django-储量我。有没有一种方法来指定我要上传文件到哪个桶(例如,如Save()函数或任何一个参数?)

I am using AWS and I will have different buckets in my application. I am also using Django-Storages. Is there a way to specify which bucket I want to upload the file to (for example, as a parameter in the Save() function or whatever?)

我看到这个 Django的 - 错误导入storages.backends 的,但我不'牛逼了解应该如何使用呢?!

I saw this Django - Error importing storages.backends, but I don't understand how it should be used?!

谢谢!

推荐答案

S3BotoStorage 取水桶名作为参数。如果没有给它会使用 AWS_STORAG​​E_BUCKET_NAME 设置。这意味着,如果你想使 S3BotoStorage 默认存储后端与 DEFAULT_FILE_STORAG​​E 则必须使用默认斗。

S3BotoStorage takes the bucket name as a parameter. If not given it will use the AWS_STORAGE_BUCKET_NAME setting. That means if you want to make S3BotoStorage the default storage backend with DEFAULT_FILE_STORAGE then it must use the default bucket.

不过,您也可以在现场级分配存储:

However you can also assign a storage on a field level:

from django.db import models
from storages.backends.s3boto import S3BotoStorage

class MyModel(models.Model):
    file_1 = models.FileField() # Uses default storage
    file_2 = models.FileField(storage=S3BotoStorage(bucket='other-bucket'))

修改

注释失控,​​所以我会更新我的答案。更改对实例的基础存储后端的参数是不是说Django的存储API的目的是要去做。存储后端不具有模型实例的知识,因为该存储器可在模型的上下文之外可以使用,例如与静态文件。并非完全没有道理,但它不是一个使用情况的Django Django的或-存储器的目的是要解决的问题。我不指望你AREN找到存储后端的下降,将处理这个给你。

Comments are getting out of hand so I'll update my answer. Changing the parameters of the storage backend on an instance basis is not something that the Django storage API was designed to do. The storage backend does not have knowledge of the model instance because the storages can be used outside the context of a model such as with static files. Not completely unreasonable but it's not a usage that Django or django-storages was intended to solve. I don't expect you aren to find a drop in storage backend that will handle this for you.

该文档描述了如何可以手动管理文件:<一href="https://docs.djangoproject.com/en/1.4/topics/files/#storage-objects">https://docs.djangoproject.com/en/1.4/topics/files/#storage-objects至少,你需要保存在您保存该文件的水桶的地方,以便以后能找到它,当你查询模型。

The docs describe how you can manage files manually: https://docs.djangoproject.com/en/1.4/topics/files/#storage-objects At a minimum you would need store the bucket where you saved the file somewhere so that you can find it later when you query the model.

这篇关于Django的存储器与多个S3桶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆