How To Dynamically Select Storage Option For Models.filefield?
Depending on the file extension, I want the file to be stored in a specific AWS bucket. I tried passing a function to the storage option, similar to how upload_to is dynamically de
Solution 1:
For what you're trying to do you may be better off making a custom storage backend and just overriding the various bits of S3BotoStorage.
In particular if you make bucket_name
a property you should be able to get the behavior you want.
EDIT:
To expand a bit on that, the source for S3BotoStorage.__init__
has the bucket
as an optional argument. Additionally bucket when it's used in the class is a @param
, making it easy to override. The following code is untested, but should be enough to give you a starting point
classMyS3BotoStorage(S3BotoStorage):@propertydefbucket(self):
ifself._filename.endswith('.jpg'):
returnself._get_or_create_bucket('SomeBucketName')
else:returnself._get_or_create_bucket('SomeSaneDefaultBucket')
def_save(self, name, content):
# This part might need some work to normalize the name and all...self._filename = name
returnsuper(MyS3BotoStorage, self)._save(name, content)
Post a Comment for "How To Dynamically Select Storage Option For Models.filefield?"