Skip to content

Commit 5666439

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

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

python/lsst/resources/s3.py

+6
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@
2525
from typing import IO, TYPE_CHECKING, cast
2626

2727
from botocore.exceptions import ClientError
28+
from botocore.handlers import validate_bucket_name
2829
from lsst.utils.timer import time_this
2930

3031
from ._resourceHandles._baseResourceHandle import ResourceHandleProtocol
3132
from ._resourceHandles._s3ResourceHandle import S3ResourceHandle
3233
from ._resourcePath import ResourcePath
3334
from .s3utils import (
3435
_get_s3_connection_parameters,
36+
_s3_validate_bucket,
3537
all_retryable_errors,
3638
backoff,
3739
bucketExists,
@@ -335,6 +337,10 @@ 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_validate_bucket():
341+
# This uses a private property for now.
342+
s3._s3.meta.events.unregister("before-parameter-build.s3", validate_bucket_name)
343+
338344
return s3, f"{self._bucket}/{self.relativeToPathRoot}"
339345

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

python/lsst/resources/s3utils.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -221,14 +221,24 @@ 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-
224+
skip_validation = not _s3_validate_bucket()
227225
endpoint_config = _get_s3_connection_parameters(profile)
228226

229227
return _get_s3_client(endpoint_config, skip_validation)
230228

231229

230+
def _s3_validate_bucket() -> bool:
231+
"""Indicate whether bucket validation should be enabled.
232+
233+
Returns
234+
-------
235+
validate : `bool`
236+
If `True` bucket names should be validated.
237+
"""
238+
disable_value = os.environ.get("LSST_DISABLE_BUCKET_VALIDATION", "0")
239+
return bool(re.search(r"^(0|f|n|false)?$", disable_value, re.I))
240+
241+
232242
def _get_s3_connection_parameters(profile: str | None = None) -> _EndpointConfig:
233243
"""Calculate the connection details.
234244

0 commit comments

Comments
 (0)