Skip to content

Commit a3fc3e9

Browse files
committed
Allow bucket validation to be disabled in s3fs
1 parent 17b34b1 commit a3fc3e9

File tree

2 files changed

+35
-5
lines changed

2 files changed

+35
-5
lines changed

python/lsst/resources/s3.py

+7
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
from ._resourcePath import ResourcePath
3333
from .s3utils import (
3434
_get_s3_connection_parameters,
35+
_s3_disable_bucket_validation,
36+
_s3_should_validate_bucket,
3537
all_retryable_errors,
3638
backoff,
3739
bucketExists,
@@ -335,6 +337,11 @@ def to_fsspec(self) -> tuple[AbstractFileSystem, str]:
335337
key=endpoint_config.access_key_id,
336338
secret=endpoint_config.secret_access_key,
337339
)
340+
if not _s3_should_validate_bucket():
341+
# Accessing the s3 property forces the boto client to be
342+
# constructed and cached and allows the validation to be removed.
343+
_s3_disable_bucket_validation(s3.s3)
344+
338345
return s3, f"{self._bucket}/{self.relativeToPathRoot}"
339346

340347
def _as_local(self) -> tuple[str, bool]:

python/lsst/resources/s3utils.py

+28-5
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,21 @@ def getS3Client(profile: str | None = None) -> boto3.client:
221221
if botocore is None:
222222
raise ModuleNotFoundError("Could not find botocore. Are you sure it is installed?")
223223

224-
disable_value = os.environ.get("LSST_DISABLE_BUCKET_VALIDATION", "0")
225-
skip_validation = not re.search(r"^(0|f|n|false)?$", disable_value, re.I)
226-
227224
endpoint_config = _get_s3_connection_parameters(profile)
228225

229-
return _get_s3_client(endpoint_config, skip_validation)
226+
return _get_s3_client(endpoint_config, not _s3_should_validate_bucket())
227+
228+
229+
def _s3_should_validate_bucket() -> bool:
230+
"""Indicate whether bucket validation should be enabled.
231+
232+
Returns
233+
-------
234+
validate : `bool`
235+
If `True` bucket names should be validated.
236+
"""
237+
disable_value = os.environ.get("LSST_DISABLE_BUCKET_VALIDATION", "0")
238+
return bool(re.search(r"^(0|f|n|false)?$", disable_value, re.I))
230239

231240

232241
def _get_s3_connection_parameters(profile: str | None = None) -> _EndpointConfig:
@@ -254,6 +263,20 @@ def _get_s3_connection_parameters(profile: str | None = None) -> _EndpointConfig
254263
return _parse_endpoint_config(endpoint, profile)
255264

256265

266+
def _s3_disable_bucket_validation(client: boto3.client) -> None:
267+
"""Disable the bucket name validation in the client.
268+
269+
This removes the ``validate_bucket_name`` handler from the handlers
270+
registered for this client.
271+
272+
Parameters
273+
----------
274+
client : `boto3.client`
275+
The client to modify.
276+
"""
277+
client.meta.events.unregister("before-parameter-build.s3", validate_bucket_name)
278+
279+
257280
@functools.lru_cache
258281
def _get_s3_client(endpoint_config: _EndpointConfig, skip_validation: bool) -> boto3.client:
259282
# Helper function to cache the client for this endpoint
@@ -269,7 +292,7 @@ def _get_s3_client(endpoint_config: _EndpointConfig, skip_validation: bool) -> b
269292
config=config,
270293
)
271294
if skip_validation:
272-
client.meta.events.unregister("before-parameter-build.s3", validate_bucket_name)
295+
_s3_disable_bucket_validation(client)
273296
return client
274297

275298

0 commit comments

Comments
 (0)