django-storages with multiple S3 Buckets

前端 未结 4 989
野趣味
野趣味 2020-12-07 23:09

I am using AWS and I will have different buckets in my application. I am also using Django-Storages. Is there a way to specify which bucket I want to upload the file to (for

4条回答
  •  攒了一身酷
    2020-12-07 23:57

    Another solution if you want to specify bucket on the runtime, you can do so before invoking the save() method on the model.

    Following the above example:

    from django.db import models
    from storages.backends.s3boto import S3BotoStorage
    
    class MyModel(models.Model):
        file_1 = models.FileField() # Uses default storage
        file_2 = models.FileField()
    

    In views when saving the model, you can specify the storage on that field.

    my_file_model = MyModel()
    my_file_model.file_2.storage = S3BotoStorage(bucket="your-bucket-name")
    my_file_model.save()
    

    In this way file_2 will be saved in the bucket you specify where file_1 will use your default bucket.

提交回复
热议问题