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

[s3] Add ability to set endpoint_url from AWS_ENDPOINT_URL env variable #773

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 7 additions & 2 deletions smart_open/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import time
import warnings

from os import getenv

try:
import boto3
import botocore.client
Expand Down Expand Up @@ -89,8 +91,11 @@ def parse_uri(uri_as_string):
split_uri = smart_open.utils.safe_urlsplit(uri_as_string)
assert split_uri.scheme in SCHEMES

port = DEFAULT_PORT
host = DEFAULT_HOST
custom_endpoint_url = getenv('AWS_ENDPOINT_URL', '')
split_custom_endpoint_url = smart_open.utils.safe_urlsplit(custom_endpoint_url)

port = split_custom_endpoint_url.port or DEFAULT_PORT
host = split_custom_endpoint_url.hostname or DEFAULT_HOST
ordinary_calling_format = False
#
# These defaults tell boto3 to look for credentials elsewhere
Expand Down
9 changes: 9 additions & 0 deletions smart_open/tests/test_smart_open.py
Original file line number Diff line number Diff line change
Expand Up @@ -1206,6 +1206,15 @@ def test_s3_mode_mock(self, mock_client):
smart_open.open("s3://mybucket/mykey", "w", transport_params=transport_params)
mock_client.assert_called_with('s3', endpoint_url='http://s3.amazonaws.com')

@mock.patch("boto3.client")
@mock.patch.dict(os.environ, {"AWS_ENDPOINT_URL": "https://my-endpoit.com:443", }, clear=True,)
def test_s3_endpoint_from_env_var(self, mock_client):
"""Are s3:// open modes passed correctly with endpoint from environment variables?"""

smart_open.open("s3://mybucket/mykey", "w")

mock_client.assert_called_with("s3", endpoint_url="https://my-endpoit.com:443")

@mock.patch('smart_open.hdfs.subprocess')
def test_hdfs(self, mock_subprocess):
"""Is HDFS write called correctly"""
Expand Down