Commit dbe7459 1 parent 17b34b1 commit dbe7459 Copy full SHA for dbe7459
File tree 2 files changed +21
-3
lines changed
2 files changed +21
-3
lines changed Original file line number Diff line number Diff line change 25
25
from typing import IO , TYPE_CHECKING , cast
26
26
27
27
from botocore .exceptions import ClientError
28
+ from botocore .handlers import validate_bucket_name
28
29
from lsst .utils .timer import time_this
29
30
30
31
from ._resourceHandles ._baseResourceHandle import ResourceHandleProtocol
31
32
from ._resourceHandles ._s3ResourceHandle import S3ResourceHandle
32
33
from ._resourcePath import ResourcePath
33
34
from .s3utils import (
34
35
_get_s3_connection_parameters ,
36
+ _s3_validate_bucket ,
35
37
all_retryable_errors ,
36
38
backoff ,
37
39
bucketExists ,
@@ -335,6 +337,12 @@ def to_fsspec(self) -> tuple[AbstractFileSystem, str]:
335
337
key = endpoint_config .access_key_id ,
336
338
secret = endpoint_config .secret_access_key ,
337
339
)
340
+ if not _s3_validate_bucket ():
341
+ # Force the boto client to be constructed so we can modify it.
342
+ with contextlib .suppress (FileNotFoundError ):
343
+ s3 .open ("bad_bucket/path" )
344
+ s3 .s3 .meta .events .unregister ("before-parameter-build.s3" , validate_bucket_name )
345
+
338
346
return s3 , f"{ self ._bucket } /{ self .relativeToPathRoot } "
339
347
340
348
def _as_local (self ) -> tuple [str , bool ]:
Original file line number Diff line number Diff line change @@ -221,14 +221,24 @@ def getS3Client(profile: str | None = None) -> boto3.client:
221
221
if botocore is None :
222
222
raise ModuleNotFoundError ("Could not find botocore. Are you sure it is installed?" )
223
223
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 ()
227
225
endpoint_config = _get_s3_connection_parameters (profile )
228
226
229
227
return _get_s3_client (endpoint_config , skip_validation )
230
228
231
229
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
+
232
242
def _get_s3_connection_parameters (profile : str | None = None ) -> _EndpointConfig :
233
243
"""Calculate the connection details.
234
244
You can’t perform that action at this time.
0 commit comments