2727from six .moves .urllib .parse import urlparse
2828
2929import sagemaker
30- from sagemaker import git_utils , image_uris , vpc_utils
30+ from sagemaker import git_utils , image_uris , vpc_utils , s3
3131from sagemaker .analytics import TrainingJobAnalytics
3232from sagemaker .config import (
3333 TRAINING_JOB_VOLUME_KMS_KEY_ID_PATH ,
@@ -672,6 +672,9 @@ def __init__(
672672 enable_network_isolation = self ._enable_network_isolation ,
673673 )
674674
675+ # Internal flag
676+ self ._is_output_path_set_from_default_bucket_and_prefix = False
677+
675678 @abstractmethod
676679 def training_image_uri (self ):
677680 """Return the Docker image to use for training.
@@ -772,7 +775,12 @@ def _prepare_for_training(self, job_name=None):
772775 if self .sagemaker_session .local_mode and local_code :
773776 self .output_path = ""
774777 else :
775- self .output_path = "s3://{}/" .format (self .sagemaker_session .default_bucket ())
778+ self .output_path = s3 .s3_path_join (
779+ "s3://" ,
780+ self .sagemaker_session .default_bucket (),
781+ self .sagemaker_session .default_bucket_prefix ,
782+ )
783+ self ._is_output_path_set_from_default_bucket_and_prefix = True
776784
777785 if self .git_config :
778786 updated_paths = git_utils .git_clone_repo (
@@ -847,7 +855,8 @@ def _stage_user_code_in_s3(self) -> str:
847855 if is_pipeline_variable (self .output_path ):
848856 if self .code_location is None :
849857 code_bucket = self .sagemaker_session .default_bucket ()
850- code_s3_prefix = self ._assign_s3_prefix ()
858+ key_prefix = self .sagemaker_session .default_bucket_prefix
859+ code_s3_prefix = self ._assign_s3_prefix (key_prefix )
851860 kms_key = None
852861 else :
853862 code_bucket , key_prefix = parse_s3_url (self .code_location )
@@ -860,16 +869,33 @@ def _stage_user_code_in_s3(self) -> str:
860869 if local_mode :
861870 if self .code_location is None :
862871 code_bucket = self .sagemaker_session .default_bucket ()
863- code_s3_prefix = self ._assign_s3_prefix ()
872+ key_prefix = self .sagemaker_session .default_bucket_prefix
873+ code_s3_prefix = self ._assign_s3_prefix (key_prefix )
864874 kms_key = None
865875 else :
866876 code_bucket , key_prefix = parse_s3_url (self .code_location )
867877 code_s3_prefix = self ._assign_s3_prefix (key_prefix )
868878 kms_key = None
869879 else :
870880 if self .code_location is None :
871- code_bucket , _ = parse_s3_url (self .output_path )
872- code_s3_prefix = self ._assign_s3_prefix ()
881+ # TODO: if output_path was set from Session, include prefix. If it was set by
882+ # the user, do not set the prefix (which would change behavior of existing
883+ # notebooks)
884+ code_bucket , possible_key_prefix = parse_s3_url (self .output_path )
885+
886+ if self ._is_output_path_set_from_default_bucket_and_prefix :
887+ # Only include possible_key_prefix if the output_path was created from the
888+ # Session's default bucket and prefix. In that scenario, possible_key_prefix
889+ # will either be "" or Session.default_bucket_prefix.
890+ # Note: We cannot do `if (code_bucket == session.default_bucket() and
891+ # key_prefix == session.default_bucket_prefix)` instead because the user
892+ # could have passed in equivalent values themselves to output_path. And
893+ # including the prefix in that case could result in a potentially backwards
894+ # incompatible behavior change for the end user.
895+ code_s3_prefix = self ._assign_s3_prefix (possible_key_prefix )
896+ else :
897+ code_s3_prefix = self ._assign_s3_prefix ()
898+
873899 kms_key = self .output_kms_key
874900 else :
875901 code_bucket , key_prefix = parse_s3_url (self .code_location )
@@ -1060,8 +1086,12 @@ def _set_source_s3_uri(self, rule):
10601086 if "source_s3_uri" in (rule .rule_parameters or {}):
10611087 parse_result = urlparse (rule .rule_parameters ["source_s3_uri" ])
10621088 if parse_result .scheme != "s3" :
1063- desired_s3_uri = os .path .join (
1064- "s3://" , self .sagemaker_session .default_bucket (), rule .name , str (uuid .uuid4 ())
1089+ desired_s3_uri = s3 .s3_path_join (
1090+ "s3://" ,
1091+ self .sagemaker_session .default_bucket (),
1092+ self .sagemaker_session .default_bucket_prefix ,
1093+ rule .name ,
1094+ str (uuid .uuid4 ()),
10651095 )
10661096 s3_uri = S3Uploader .upload (
10671097 local_path = rule .rule_parameters ["source_s3_uri" ],
0 commit comments