如何使用django-storage在AWS S3存储桶中创建文件夹?

时间:2013-04-25 08:29:15

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

我尝试了以下内容:

f = default_storage.open('test/', 'w')
f.write('')
f.close()

但是它返回了这个错误:

  

您提供的XML格式不正确或未针对我们发布的架构进行验证

2 个答案:

答案 0 :(得分:1)

S3中没有文件夹这样的东西。如果使用路径保存文件,则会伪造目录结构。

https://stackoverflow.com/a/2141499/682968

Add folder in Amazon s3 bucket

答案 1 :(得分:0)

对于Google Cloud Storage,我最终通过覆盖save方法来创建目录,该方法适用于写空文件:


class Attachment(models.Model):
    file = models.FileField(...)

    def save(self, *args, **kwargs):
        super(Attachment, self).save(*args, **kwargs)

        cloud_storage = GoogleCloudStorage(...)
        directory_path = "<your_path>"
        if not cloud_storage.exists(directory_path):
            directory = cloud_storage.open(directory_path, "w")
            directory.write("")
            directory.close()
相关问题