Skip to content

Commit

Permalink
Fix AWS_S3_* environment variables lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
iurisilvio committed Nov 8, 2023
1 parent d0dce62 commit 5a12535
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
19 changes: 16 additions & 3 deletions storages/backends/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from storages.utils import clean_name
from storages.utils import get_available_overwrite_name
from storages.utils import is_seekable
from storages.utils import lookup_env
from storages.utils import safe_join
from storages.utils import setting
from storages.utils import to_bytes
Expand Down Expand Up @@ -352,14 +353,26 @@ def get_cloudfront_signer(self, key_id, key):

def get_default_settings(self):
return {
"access_key": setting("AWS_S3_ACCESS_KEY_ID", setting("AWS_ACCESS_KEY_ID")),
"access_key": setting(
"AWS_S3_ACCESS_KEY_ID",
setting(
"AWS_ACCESS_KEY_ID",
lookup_env(["AWS_S3_ACCESS_KEY_ID", "AWS_ACCESS_KEY_ID"])
)
),
"secret_key": setting(
"AWS_S3_SECRET_ACCESS_KEY", setting("AWS_SECRET_ACCESS_KEY")
"AWS_S3_SECRET_ACCESS_KEY",
setting(
"AWS_SECRET_ACCESS_KEY",
lookup_env(["AWS_S3_SECRET_ACCESS_KEY", "AWS_SECRET_ACCESS_KEY"])
)
),
"security_token": setting(
"AWS_SESSION_TOKEN", setting("AWS_SECURITY_TOKEN")
),
"session_profile": setting("AWS_S3_SESSION_PROFILE"),
"session_profile": setting(
"AWS_S3_SESSION_PROFILE", lookup_env(["AWS_S3_SESSION_PROFILE"])
),
"file_overwrite": setting("AWS_S3_FILE_OVERWRITE", True),
"object_parameters": setting("AWS_S3_OBJECT_PARAMETERS", {}),
"bucket_name": setting("AWS_STORAGE_BUCKET_NAME"),
Expand Down
11 changes: 11 additions & 0 deletions storages/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,17 @@ def check_location(storage):
)


def lookup_env(names):
"""
Look up for names in environment. Returns the first element
found.
"""
for name in names:
value = os.environ.get(name)
if value:
return value


def get_available_overwrite_name(name, max_length):
if max_length is None or len(name) <= max_length:
return name
Expand Down
21 changes: 21 additions & 0 deletions tests/test_s3.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import datetime
import gzip
import io
import os
import pickle
import threading
from textwrap import dedent
Expand Down Expand Up @@ -938,6 +939,26 @@ def test_auth_config(self):
self.assertEqual(storage.access_key, "foo")
self.assertEqual(storage.secret_key, "boo")

with mock.patch.dict(
os.environ, {
"AWS_S3_ACCESS_KEY_ID": "foo",
"AWS_S3_SECRET_ACCESS_KEY": "boo"
}
):
storage = s3.S3Storage()
self.assertEqual(storage.access_key, "foo")
self.assertEqual(storage.secret_key, "boo")

with mock.patch.dict(
os.environ, {
"AWS_ACCESS_KEY_ID": "foo",
"AWS_SECRET_ACCESS_KEY": "boo"
}
):
storage = s3.S3Storage()
self.assertEqual(storage.access_key, "foo")
self.assertEqual(storage.secret_key, "boo")

storage = s3.S3Storage(access_key="foo", secret_key="boo")
self.assertEqual(storage.access_key, "foo")
self.assertEqual(storage.secret_key, "boo")
Expand Down

0 comments on commit 5a12535

Please sign in to comment.