【问题标题】:Django - how to upload to a specific S3 bucket?Django - 如何上传到特定的 S3 存储桶?
【发布时间】:2015-12-17 02:30:46
【问题描述】:

我试过ImageField(upload_to='', storage=S3MediaStorage),当调用save() 时,它会报错TypeError: get_valid_name() missing positional argument: 'name'。但是,如果我省略 storage= 并让它使用 DEFAULT_FILE_STORAGE 存储桶,它就可以完美地工作。

S3MediaStorage:

class S3MediaStorage(S3BotoStorage):
    def __init__(self, *args, **kwargs):
        kwargs['bucket'] = getattr(settings, 'AWS_BUCKET_MEDIA')
        super(S3MediaStorage, self).__init__(*args, **kwargs)

models.py:

class Image(models.Model):
    created = models.DateTimeField(auto_now_add=True)
    updated = models.DateTimeField(auto_now=True)
    visible = models.BooleanField(default=False)
    author = models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE)
    caption = models.CharField(max_length=254, blank=True)
    image = models.ImageField(upload_to='', storage=S3MediaStorage)

    def __str__(self):
        return str(self.created)

我做错了什么?我应该使用完全不同的方法将文件上传到非默认存储桶吗?

【问题讨论】:

    标签: django django-models django-storage


    【解决方案1】:

    尝试返回super

    return super(S3MediaStorage, self).__init__(*args, **kwargs)
    

    【讨论】:

      猜你喜欢
      • 2017-04-19
      • 1970-01-01
      • 2020-10-17
      • 2018-03-11
      • 2015-03-12
      • 2018-08-26
      • 1970-01-01
      • 1970-01-01
      • 2017-12-23
      相关资源
      最近更新 更多