|
13 | 13 | """This module contains Enums and helper methods related to S3."""
|
14 | 14 | from __future__ import print_function, absolute_import
|
15 | 15 |
|
| 16 | +import os |
| 17 | + |
16 | 18 | from six.moves.urllib.parse import urlparse
|
17 | 19 | from sagemaker.session import Session
|
18 | 20 |
|
@@ -42,7 +44,7 @@ def upload(local_path, desired_s3_uri, kms_key=None, session=None):
|
42 | 44 | Args:
|
43 | 45 | local_path (str): A local path to a file or directory.
|
44 | 46 | desired_s3_uri (str): The desired S3 uri to upload to.
|
45 |
| - kms_key (str): A KMS key to be provided as an extra argument. |
| 47 | + kms_key (str): The KMS key to use to encrypt the files. |
46 | 48 | session (sagemaker.session.Session): Session object which
|
47 | 49 | manages interactions with Amazon SageMaker APIs and any other
|
48 | 50 | AWS services needed. If not specified, the estimator creates one
|
@@ -70,7 +72,7 @@ def upload_string_as_file_body(body, desired_s3_uri=None, kms_key=None, session=
|
70 | 72 | Args:
|
71 | 73 | body (str): String representing the body of the file.
|
72 | 74 | desired_s3_uri (str): The desired S3 uri to upload to.
|
73 |
| - kms_key (str): A KMS key to be provided as an extra argument. |
| 75 | + kms_key (str): The KMS key to use to encrypt the files. |
74 | 76 | session (sagemaker.session.Session): AWS session to use. Automatically
|
75 | 77 | generates one if not provided.
|
76 | 78 |
|
@@ -98,7 +100,7 @@ def download(s3_uri, local_path, kms_key=None, session=None):
|
98 | 100 | Args:
|
99 | 101 | s3_uri (str): An S3 uri to download from.
|
100 | 102 | local_path (str): A local path to download the file(s) to.
|
101 |
| - kms_key (str): A KMS key to be provided as an extra argument. |
| 103 | + kms_key (str): The KMS key to use to decrypt the files. |
102 | 104 | session (sagemaker.session.Session): Session object which
|
103 | 105 | manages interactions with Amazon SageMaker APIs and any other
|
104 | 106 | AWS services needed. If not specified, the estimator creates one
|
@@ -133,3 +135,22 @@ def read_file(s3_uri, session=None):
|
133 | 135 | bucket, key_prefix = parse_s3_url(url=s3_uri)
|
134 | 136 |
|
135 | 137 | return sagemaker_session.read_s3_file(bucket=bucket, key_prefix=key_prefix)
|
| 138 | + |
| 139 | + @staticmethod |
| 140 | + def list(s3_uri, session=None): |
| 141 | + """Static method that lists the contents of an S3 uri. |
| 142 | +
|
| 143 | + Args: |
| 144 | + s3_uri (str): The S3 base uri to list objects in. |
| 145 | + session (sagemaker.session.Session): AWS session to use. Automatically |
| 146 | + generates one if not provided. |
| 147 | +
|
| 148 | + Returns: |
| 149 | + [str]: The list of S3 URIs in the given S3 base uri. |
| 150 | +
|
| 151 | + """ |
| 152 | + sagemaker_session = session or Session() |
| 153 | + bucket, key_prefix = parse_s3_url(url=s3_uri) |
| 154 | + |
| 155 | + file_keys = sagemaker_session.list_s3_files(bucket=bucket, key_prefix=key_prefix) |
| 156 | + return [os.path.join("s3://", bucket, file_key) for file_key in file_keys] |
0 commit comments