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

endpoint url #24

Merged
merged 3 commits into from
Mar 7, 2024
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Added

- boto3utils.s3 object now supports specifying the `endpoint_url` parameter
for the underlying boto3 client

## [v0.4.1] - 2022-11-19

### Fixed
Expand Down
13 changes: 9 additions & 4 deletions boto3utils/s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import logging
import os
import os.path as op
from typing import Tuple
from typing import Tuple, Optional

from botocore.exceptions import ClientError
from copy import deepcopy
Expand All @@ -21,12 +21,17 @@


class s3(object):
def __init__(self, session=None, requester_pays=False):
def __init__(
self,
session: boto3.Session = None,
requester_pays: bool = False,
endpoint_url: Optional[str] = None,
):
self.requester_pays = requester_pays
if session is None:
self.s3 = boto3.client("s3")
self.s3 = boto3.client("s3", endpoint_url=endpoint_url)
else:
self.s3 = session.client("s3")
self.s3 = session.client("s3", endpoint_url=endpoint_url)

@classmethod
def urlparse(cls, url):
Expand Down
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ def s3_west(aws_credentials):
yield boto3.client("s3", region_name="us-west-2")


@pytest.fixture
def s3_custom_endpoint(aws_credentials):
os.environ["MOTO_S3_CUSTOM_ENDPOINTS"] = "http://my-s3"
with moto.mock_s3():
yield boto3.client("s3", endpoint_url="http://my-s3")


@pytest.fixture
def secretsmanager(aws_credentials):
with moto.mock_secretsmanager():
Expand Down
7 changes: 7 additions & 0 deletions tests/test_s3.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,3 +188,10 @@ def test_latest_inventory():
# hours = (datetime.today() - dt).seconds // 3600
# assert(hours < 24)
# break


def test_upload_download_with_custom_endpoint(s3_custom_endpoint):
create_test_bucket(s3_custom_endpoint, BUCKET)
url = "s3://%s/mytestfile" % BUCKET
s3(endpoint_url="http://my-s3").upload(__file__, url, public=True)
assert s3(endpoint_url="http://my-s3").exists(url)
Loading