Skip to content

Commit c02591a

Browse files
James WoodJames Wood
James Wood
authored and
James Wood
committed
added s3 method to list all s3 keys in a s3 path
1 parent fd187a3 commit c02591a

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

podaac/hitide_backfill_tool/s3_reader.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,20 @@ def read_file_from_s3(self, s3_path):
4545
return response["Body"].read().decode("ISO-8859-1")
4646
except ClientError as exc:
4747
raise Exception(f"S3Reader could not read file at {s3_path}.") from exc
48+
49+
def list_s3_keys(self, s3_path):
50+
"""
51+
List all keys in a s3 bucket from input s3_path
52+
53+
:return: List of file keys in the specified directory.
54+
"""
55+
56+
bucket_name, prefix = self.extract_bucket_and_file(s3_path)
57+
58+
s3_client = boto3.client("s3")
59+
response = s3_client.list_objects_v2(Bucket=bucket_name, Prefix=prefix)
60+
61+
if "Contents" in response:
62+
return [obj["Key"] for obj in response["Contents"]]
63+
else:
64+
return []

0 commit comments

Comments
 (0)