【问题标题】:using s3 with django-storages and uploading images使用 s3 和 django-storages 并上传图片
【发布时间】:2014-07-01 05:30:11
【问题描述】:

我正在尝试在 Amazon 的 S3 服务上上传 MEDIA_FILES。我想先将它们上传到存储桶的根目录,然后在保存文件之前创建子文件夹。我的测试模型很简单

from django.db import models

# Create your models here.


class TestModel(models.Model):
    name = models.CharField(max_length=10)
    logo = models.ImageField(upload_to='pictures/')

    class Meta:
        verbose_name = ('TestModel')
        verbose_name_plural = ('TestModels')

    def __unicode__(self):
        pass

我已经创建了一个名为图片的子文件夹。

settigns.py

DEFAULT_FILE_STORAGE = 'storages.backends.s3boto.S3BotoStorage'
AWS_ACCESS_KEY_ID = 'id'
AWS_SECRET_ACCESS_KEY = 'secret_key'
AWS_STORAGE_BUCKET_NAME = 'mybucket'

from S3 import CallingFormat
AWS_CALLING_FORMAT = CallingFormat.SUBDOMAIN

但是当我尝试从管理面板添加 TestModel 时,我收到以下错误

Request URL:    http://127.0.0.1:8000/admin/testmodel/testmodel/add/
Django Version: 1.6.4
Exception Type: TypeError
Exception Value:    
'NoneType' object has no attribute '__getitem__'

s3 和 django-storages 是否像以前一样工作?使用 s3 时要设置的 MEDIA_URL 的值是多少?使用 S3 和 django-storages 时 MEDIA_ROOT 的值是多少?

我可以使用this way 将文件上传到 S3 服务吗?这是我第一次使用云存储,所以我有点困惑。

【问题讨论】:

    标签: python django file-upload amazon-web-services amazon-s3


    【解决方案1】:

    如果您想使用 S3,请检查“boto”,python 模块的 aws,http://boto.readthedocs.org/en/latest/ 还有这里关于 MEDIA_ROOT 和 MEDIA_URL https://docs.djangoproject.com/en/dev/ref/settings/#media-root

    【讨论】:

    • 我知道媒体根是什么,我问的是如何设置它以将它与 django-storages 和 s3 一起使用。由于 django-storages 使用它,我也知道这两者。
    • 我的错,没注意你使用 django-storage,我在我的项目中使用了 tinyS3。
    • 没问题我想我可以用这个stackoverflow.com/a/10825691/2349589
    猜你喜欢
    • 1970-01-01
    • 2019-12-02
    • 2012-07-14
    • 2011-07-13
    • 2016-12-28
    • 1970-01-01
    • 1970-01-01
    • 2012-03-28
    • 2010-11-26
    相关资源
    最近更新 更多