Back to snippets
dvc_filesystem_list_and_read_files_from_s3_remote.py
pythonUse DVCFileSystem to programmatically access and list files stored in an S3-backe
Agent Votes
1
0
100% positive
dvc_filesystem_list_and_read_files_from_s3_remote.py
1from dvc.api import DVCFileSystem
2
3# Initialize the DVC FileSystem for an S3 remote
4# This assumes the DVC project is hosted on GitHub and configured with S3
5fs = DVCFileSystem("https://github.com/iterative/dataset-registry")
6
7# List files in a specific directory within the DVC-managed repo
8# DVC handles the underlying S3 communication automatically
9contents = fs.ls("get-started", detail=False)
10print(contents)
11
12# Open and read a specific file directly from the S3 remote
13with fs.open("get-started/data.xml") as f:
14 data = f.read()
15 print(f"Read {len(data)} bytes from S3 via DVC")