Skip to content

Commit e267be1

Browse files
authored
Improve bucket name configuration handling
Refactor bucket name retrieval logic for clarity and maintainability.
1 parent 6144051 commit e267be1

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

smart_storages/s3_backend.py

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,25 @@ class BaseSpecialS3Storage(S3Boto3Storage):
1414
def __init__(self, **kwargs):
1515
# Start with sane defaults
1616
options = {"querystring_auth": True}
17-
18-
# Load from STORAGES if configured
17+
bucket_name = None
18+
19+
# 1. Try STORAGES dict
1920
if hasattr(settings, "STORAGES") and self.storage_key:
2021
storage_conf = settings.STORAGES.get(self.storage_key, {})
2122
options.update(storage_conf.get("OPTIONS", {}))
23+
bucket_name = options.get("bucket_name", None)
2224

23-
# Fallback: if bucket not set, look for setting variable
24-
if "bucket_name" not in options:
25-
# Allow storage_key to be either "import_export"
25+
# 2. Fallback to storage_key setting if bucket_name is None
26+
if bucket_name is None and self.storage_key:
2627
bucket_name = getattr(settings, self.storage_key, None)
27-
if not bucket_name:
28-
bucket_name = getattr(settings, "AWS_STORAGE_BUCKET_NAME", None)
29-
options["bucket_name"] = bucket_name
3028

31-
# Finally, user kwargs override everything
29+
# 3. Final fallback to AWS_STORAGE_BUCKET_NAME if still None
30+
if bucket_name is None:
31+
bucket_name = getattr(settings, "AWS_STORAGE_BUCKET_NAME", None)
32+
33+
options["bucket_name"] = bucket_name
34+
35+
# 4. Override with any kwargs
3236
options.update(kwargs)
3337

3438
super().__init__(**options)

0 commit comments

Comments
 (0)