diff --git a/fastenv/cloud/object_storage.py b/fastenv/cloud/object_storage.py index 5199867..d557c2b 100644 --- a/fastenv/cloud/object_storage.py +++ b/fastenv/cloud/object_storage.py @@ -77,10 +77,18 @@ def __init__( "`.s3..backblazeb2.com` for Backblaze B2." ) elif bucket_host and not bucket_name: - if ( - bucket_host.endswith(".amazonaws.com") - or bucket_host.endswith(".backblazeb2.com") - ) and bucket_name is None: + scheme = ( + "http://" + if bucket_host.startswith("http://") + else "https://" + if bucket_host.startswith("https://") + else None + ) + if scheme: + bucket_host = bucket_host.split(scheme, maxsplit=1)[1] + if bucket_host.endswith(".amazonaws.com") or bucket_host.endswith( + ".backblazeb2.com" + ): self.bucket_name = bucket_host.split(".s3.")[0] else: self.bucket_name = None diff --git a/tests/cloud/test_object_storage.py b/tests/cloud/test_object_storage.py index 5fc86fb..6aac73d 100644 --- a/tests/cloud/test_object_storage.py +++ b/tests/cloud/test_object_storage.py @@ -243,10 +243,10 @@ def test_config_if_not_bucket_name( bucket_name=bucket_name, bucket_region=bucket_region, ) - if "backblazeb2" in bucket_host and bucket_name is None: - assert config.bucket_name == "mybucket" - else: + if "digitaloceanspaces.com" in bucket_host: assert config.bucket_name is None + else: + assert config.bucket_name == self.example_bucket_name def test_config_if_bucket_name_not_in_bucket_host( self, mocker: MockerFixture @@ -295,6 +295,25 @@ def test_config_if_bucket_region_not_in_bucket_host( ) assert str(e.value) == expected_exception_value + @pytest.mark.parametrize("scheme", ("http", "https")) + def test_config_if_scheme_in_bucket_host( + self, scheme: str, mocker: MockerFixture + ) -> None: + """Assert that bucket host scheme ("http" or "https") is removed if present. + Scheme is added automatically when generating instances of `httpx.URL()`. + """ + mocker.patch.dict(os.environ, clear=True) + bucket_host = f"{scheme}://{self.example_bucket_host}" + expected_bucket_host = self.example_bucket_host + config = fastenv.cloud.object_storage.ObjectStorageConfig( + access_key=self.example_access_key, + secret_key=self.example_secret_key, + bucket_host=bucket_host, + bucket_region=self.example_bucket_region, + ) + assert self.config_is_correct(config, expected_bucket_host=expected_bucket_host) + assert scheme not in config.bucket_host + class TestObjectStorageClientUnit: """Test `class ObjectStorageClient` and its methods.