stuax
stuax

Reputation: 501

How can I make folder in AWS S3 bucket with django-storages?

I tried the following:

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

but it returned this error:

The XML you provided was not well-formed or did not validate against our published schema

Upvotes: 1

Views: 3865

Answers (2)

Igor Pejic
Igor Pejic

Reputation: 3698

For Google Cloud Storage I ended up creating the directory by overriding the save method for which the writing of an empty file works:


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

Upvotes: 0

rockingskier
rockingskier

Reputation: 9346

There is no such thing as a folder in S3. If you save a file with a path is will 'fake' the directory structure.

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

Add folder in Amazon s3 bucket

Upvotes: 3

Related Questions