Skip to content

Commit c46b025

Browse files
committed
fix leading slash case
1 parent cef26ba commit c46b025

File tree

3 files changed

+10
-22
lines changed

3 files changed

+10
-22
lines changed

src/sagemaker/local/utils.py

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
from __future__ import absolute_import
1515

1616
import os
17-
import pathlib
1817
import shutil
1918

2019
from distutils.dir_util import copy_tree
2120
from six.moves.urllib.parse import urlparse
2221

22+
from sagemaker import s3
23+
2324

2425
def copy_directory_structure(destination_directory, relative_path):
2526
"""Create all the intermediate directories required for relative_path to
@@ -63,8 +64,8 @@ def move_to_destination(source, destination, job_name, sagemaker_session):
6364
final_uri = destination
6465
elif parsed_uri.scheme == "s3":
6566
bucket = parsed_uri.netloc
66-
path = _create_s3_prefix(parsed_uri.path, job_name)
67-
final_uri = "s3://%s/%s" % (bucket, path)
67+
path = s3.s3_path_join(parsed_uri.path, job_name)
68+
final_uri = s3.s3_path_join(bucket, path)
6869
sagemaker_session.upload_data(source, bucket, path)
6970
else:
7071
raise ValueError("Invalid destination URI, must be s3:// or file://, got: %s" % destination)
@@ -73,21 +74,6 @@ def move_to_destination(source, destination, job_name, sagemaker_session):
7374
return final_uri
7475

7576

76-
def _create_s3_prefix(path, job_name):
77-
"""Constructs a path out of the given path and job name to be
78-
used as an S3 prefix.
79-
80-
Args:
81-
path (str): the original path. If the path is only ``"/"``,
82-
then it is ignored.
83-
job_name (str): the job name to be appended to the path.
84-
85-
Returns:
86-
str: an S3 prefix of the form ``"path/job_name"``
87-
"""
88-
return job_name if path == "" else str(pathlib.PurePosixPath(path, job_name))
89-
90-
9177
def recursive_copy(source, destination):
9278
"""A wrapper around distutils.dir_util.copy_tree but won't throw any
9379
exception when the source directory does not exist.

src/sagemaker/s3.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ def s3_path_join(*args):
5252
Returns:
5353
str: The joined string.
5454
"""
55-
path = str(pathlib.PurePosixPath(*args))
56-
if path.startswith("s3:/"):
57-
return path.replace("s3:/", "s3://")
55+
if args[0].startswith("s3://"):
56+
path = str(pathlib.PurePosixPath(*args[1:])).lstrip("/")
57+
return str(pathlib.PurePosixPath(args[0], path)).replace("s3:/", "s3://")
5858

59-
return path
59+
return str(pathlib.PurePosixPath(*args)).lstrip("/")
6060

6161

6262
class S3Uploader(object):

tests/unit/test_s3.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,9 @@ def test_path_join():
112112
test_cases = (
113113
("foo/bar", ("foo", "bar")),
114114
("foo/bar", ("foo/", "bar")),
115+
("foo/bar", ("/foo/", "bar")),
115116
("s3://foo/bar", ("s3://", "foo", "bar")),
117+
("s3://foo/bar", ("s3://", "/foo", "bar")),
116118
("s3://foo/bar", ("s3://foo", "bar")),
117119
)
118120

0 commit comments

Comments
 (0)