Skip to content

Commit 2a4e221

Browse files
authored
Add ability to set endpoint_url from AWS_ENDPOINT_URL env variable (#193)
* Add ability to set endpoint_url from AWS_ENDPOINT_URL env variable. Very useful for localstack while waiting for upstream PR from boto3: boto/boto3#2746 * add test * rm print * lint
1 parent 3827cd8 commit 2a4e221

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

cloudpathlib/s3/s3client.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ def __init__(
6464
boto3_transfer_config (Optional[dict]): Instantiated TransferConfig for managing s3 transfers.
6565
(https://boto3.amazonaws.com/v1/documentation/api/latest/reference/customizations/s3.html#boto3.s3.transfer.TransferConfig)
6666
"""
67+
endpoint_url = endpoint_url or os.getenv("AWS_ENDPOINT_URL")
6768
if boto3_session is not None:
6869
self.sess = boto3_session
6970
else:

tests/test_s3_specific.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
from boto3.s3.transfer import TransferConfig
77
import botocore
8-
from cloudpathlib import S3Path
8+
from cloudpathlib import S3Client, S3Path
99
from cloudpathlib.local import LocalS3Path
1010
import psutil
1111

@@ -186,3 +186,19 @@ def test_no_sign_request(s3_rig, tmp_path):
186186
p = client.CloudPath(f"s3://{s3_rig.drive}/dir_0/file0_to_download.txt")
187187
with pytest.raises(botocore.exceptions.ClientError):
188188
p.exists()
189+
190+
191+
def test_aws_endpoint_url_env(monkeypatch):
192+
"""Allows setting endpoint_url from env variable
193+
until upstream boto3 PR is merged.
194+
https://github.com/boto/boto3/pull/2746
195+
"""
196+
s3_url = "https://s3.amazonaws.com"
197+
localstack_url = "http://localhost:4566"
198+
199+
s3_client = S3Client()
200+
assert s3_client.client.meta.endpoint_url == s3_url
201+
202+
monkeypatch.setenv("AWS_ENDPOINT_URL", localstack_url)
203+
s3_client_custom_endpoint = S3Client()
204+
assert s3_client_custom_endpoint.client.meta.endpoint_url == localstack_url

0 commit comments

Comments
 (0)