django-storages with multiple S3 Buckets

前端 未结 4 997
野趣味
野趣味 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:39

    Just mention another bucket name in settings.py with PRIVATE_BUCKET_NAME='bucket name'.

    Create a custom class which override S3BotoStorage and which can be serialize into migration files.

    Create a object for class s3_storage = S3MediaStorage() and give it to storage in file1 field in MyModel

    from storages.backends.s3boto import S3BotoStorage
    from django.conf import settings
    
    @deconstructible
    class S3MediaStorage(S3BotoStorage):
        def __init__(self, *args, **kwargs):
            kwargs['bucket'] = getattr(settings, 'PRIVATE_BUCKET_NAME')
            super(S3MediaStorage, self).__init__(*args, **kwargs)
    
    s3_storage = S3MediaStorage()
    
    class MyModel(models.Model):
        file = models.FileField()
        file1 = models.FileField(storage=s3_storage)
    

提交回复
热议问题