【问题标题】:How can I make folder in AWS S3 bucket with django-storages?如何使用 django-storages 在 AWS S3 存储桶中创建文件夹?
【发布时间】:2020-01-14 16:43:06
【问题描述】:

我尝试了以下方法:

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

但它返回了这个错误:

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

【问题讨论】:

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


    【解决方案1】:

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

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

    Add folder in Amazon s3 bucket

    【讨论】:

      【解决方案2】:

      对于 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()
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-22
        • 1970-01-01
        • 1970-01-01
        • 2016-08-23
        • 2020-10-28
        • 2020-05-04
        • 2016-09-26
        • 2017-12-25
        相关资源
        最近更新 更多