Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix AWS_S3_* environment variables lookup #1336

Merged
merged 1 commit into from
Nov 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 16 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,21 @@ 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