@@ -221,12 +221,21 @@ 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
-
227
224
endpoint_config = _get_s3_connection_parameters (profile )
228
225
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 ))
230
239
231
240
232
241
def _get_s3_connection_parameters (profile : str | None = None ) -> _EndpointConfig :
@@ -254,6 +263,20 @@ def _get_s3_connection_parameters(profile: str | None = None) -> _EndpointConfig
254
263
return _parse_endpoint_config (endpoint , profile )
255
264
256
265
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
+
257
280
@functools .lru_cache
258
281
def _get_s3_client (endpoint_config : _EndpointConfig , skip_validation : bool ) -> boto3 .client :
259
282
# Helper function to cache the client for this endpoint
@@ -269,7 +292,7 @@ def _get_s3_client(endpoint_config: _EndpointConfig, skip_validation: bool) -> b
269
292
config = config ,
270
293
)
271
294
if skip_validation :
272
- client . meta . events . unregister ( "before-parameter-build.s3" , validate_bucket_name )
295
+ _s3_disable_bucket_validation ( client )
273
296
return client
274
297
275
298
0 commit comments