Skip to content

Commit

Permalink
Add environment variable to override endpoint_url (boto#2099)
Browse files Browse the repository at this point in the history
  • Loading branch information
rwillmer committed Jan 30, 2021
1 parent 52dfabe commit 27c7827
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
6 changes: 6 additions & 0 deletions boto3/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ def client(self, service_name, region_name=None, api_version=None,
:return: Service client instance
"""
if endpoint_url is None:
try:
endpoint_url = os.environ["AWS_ENDPOINT_URL"]
except KeyError:
pass

return self._session.create_client(
service_name, region_name=region_name, api_version=api_version,
use_ssl=use_ssl, verify=verify, endpoint_url=endpoint_url,
Expand Down
3 changes: 3 additions & 0 deletions docs/source/guide/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ Configurations can be set through the use of system-wide environment variables.
Specifies the types of retries the SDK will use. For more information,
see the ``retry_mode`` configuration file section.

``AWS_ENDPOINT_URL``
Overrides the default AWS endpoint_url. Useful when testing with localstack or minio, for example.

Using a configuration file
-------------------

Expand Down
24 changes: 24 additions & 0 deletions tests/unit/test_endpoint_url.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import os
import unittest

import boto3

class TestEndpointUrl(unittest.TestCase):
def test_default_session_does_not_require_endpoint_url_env_var(self):
boto3.setup_default_session(
aws_access_key_id='key',
aws_secret_access_key='secret')
client = boto3.DEFAULT_SESSION.client('s3')
self.assertTrue(client._endpoint.host.endswith("amazonaws.com"),
"AWS_ENDPOINT_URL env var should not be required")

def test_default_session_uses_endpoint_url_env_var(self):
ENDPOINT_URL = "http://endpoint.url"
os.environ["AWS_ENDPOINT_URL"] = ENDPOINT_URL
boto3.setup_default_session(
aws_access_key_id='key',
aws_secret_access_key='secret')
client = boto3.DEFAULT_SESSION.client('s3')
self.assertTrue(client._endpoint.host == ENDPOINT_URL, 'AWS_ENDPOINT_URL env var not used when set')

# eof

0 comments on commit 27c7827

Please sign in to comment.