@@ -548,16 +548,19 @@ def default_bucket(self):
548
548
default_bucket = generate_default_sagemaker_bucket_name (self .boto_session )
549
549
_default_bucket_set_by_sdk = True
550
550
551
- self ._create_s3_bucket_if_it_does_not_exist (bucket_name = default_bucket , region = region )
552
- if _default_bucket_set_by_sdk :
553
- # make sure the s3 bucket is configured in users account.
554
- self ._bucket_owner_check (default_bucket )
551
+ self ._create_s3_bucket_if_it_does_not_exist (
552
+ bucket_name = default_bucket ,
553
+ region = region ,
554
+ default_bucket_set_by_sdk = _default_bucket_set_by_sdk ,
555
+ )
555
556
556
557
self ._default_bucket = default_bucket
557
558
558
559
return self ._default_bucket
559
560
560
- def _create_s3_bucket_if_it_does_not_exist (self , bucket_name , region ):
561
+ def _create_s3_bucket_if_it_does_not_exist (
562
+ self , bucket_name , region , default_bucket_set_by_sdk
563
+ ):
561
564
"""Creates an S3 Bucket if it does not exist.
562
565
563
566
Also swallows a few common exceptions that indicate that the bucket already exists or
@@ -625,44 +628,26 @@ def _create_s3_bucket_if_it_does_not_exist(self, bucket_name, region):
625
628
else :
626
629
raise
627
630
628
- def _bucket_owner_check (self , bucket_name ):
629
- """Checks if the S3 Bucket exists in AWS account configured in Session.
630
-
631
- More info: https://docs.aws.amazon.com/AmazonS3/latest/userguide/bucket-owner-condition.html
632
-
633
- Args:
634
- bucket_name (str): Name of the S3 bucket.
635
-
636
- Raises:
637
- botocore.exceptions.ClientError: If S3 throws an unexpected exception during
638
- head_bucket call. Only if the exception is due to the bucket exists but not in
639
- AWS account configured in Session
640
-
641
- """
642
- if self .s3_resource is None :
643
- s3 = self .boto_session .resource ("s3" , region_name = self .boto_session .region_name )
644
- else :
645
- s3 = self .s3_resource
646
-
647
- expected_bucket_owner_id = self .account_id ()
648
- try :
649
- s3 .meta .client .head_bucket (
650
- Bucket = bucket_name , ExpectedBucketOwner = expected_bucket_owner_id
651
- )
652
- except ClientError as e :
653
- error_code = e .response ["Error" ]["Code" ]
654
- message = e .response ["Error" ]["Message" ]
655
- if error_code == "403" and message == "Forbidden" :
656
- LOGGER .error (
657
- "Since default_bucket param was not set, SageMaker Python SDK tried to use "
658
- "%s bucket. The bucket exists, but not in the AWS account configured in "
659
- "SageMaker Session."
660
- "Please reach out to AWS Security to verify on bucket ownership."
661
- "To unblock it's recommended to use custom default_bucket "
662
- "param in sagemaker.Session" ,
663
- bucket_name ,
631
+ if default_bucket_set_by_sdk :
632
+ # make sure the s3 bucket is configured in users account.
633
+ expected_bucket_owner_id = self .account_id ()
634
+ try :
635
+ s3 .meta .client .head_bucket (
636
+ Bucket = bucket_name , ExpectedBucketOwner = expected_bucket_owner_id
664
637
)
665
- raise
638
+ except ClientError as e :
639
+ error_code = e .response ["Error" ]["Code" ]
640
+ message = e .response ["Error" ]["Message" ]
641
+ if error_code == "403" and message == "Forbidden" :
642
+ LOGGER .error (
643
+ "Since default_bucket param was not set, SageMaker Python SDK tried to use "
644
+ "%s bucket. This bucket cannot be configured to use as it is not owned by Account %s. "
645
+ "To unblock it's recommended to use custom default_bucket "
646
+ "parameter in sagemaker.Session" ,
647
+ bucket_name ,
648
+ expected_bucket_owner_id ,
649
+ )
650
+ raise
666
651
667
652
def _append_sagemaker_config_tags (self , tags : list , config_path_to_tags : str ):
668
653
"""Appends tags specified in the sagemaker_config to the given list of tags.
0 commit comments