s3://trd-data-lake-landing-zone/fetched_projects/project_65e34c4352faff00017fc8a2/locations/location_65e34c4352faff00017fc835/design_65e34c4352faff00017fc832/analysis_65e34c4352faff00017fc 8a3/
看看这个文件结构。在 fetched_projects 下,我看到一个project_文件夹,其中有一些文件和一个名为locations的文件夹。在locations文件夹下,我有另一个文件夹,其位置内有几个文件夹作为设计,其中有几个文件夹作为分析,其中有一些json文件。如果您发现这些都不相同。
s3://trd-data-lake-landing-zone/
└── fetched_projects
└── project_<id>
├── files...
└── locations
└── location_<id>
└── design_<id>
└── analysis_<id>
└── json files...
在分析桶的末尾,我得到一些名为 result_.json 的 json 文件。我只想使用它们来运行另一个转换管道,以展平 json 结构。我如何动态地解决这个问题?我正在本地机器上尝试这个。
我尝试获取所有 id 的列表,然后创建一个格式键,例如 f'{projects_path}project_{project_id}/result_{project_id}.json',但这不起作用
您能说得更具体一点吗?如果您想动态获取给定某个键的文件,或者将您的键与存储桶中存在的键进行匹配,您可以列出存储桶中的项目,然后只需使用正则表达式过滤字符串列表。这是最简单的方法,可以根据您的任务实施更好的解决方案。
您可以在某个字符处分割每个文件的密钥(例如
/
)。在伪代码中(假设您在执行 python 代码的环境中设置了 boto3):
import boto3
###
# Necessary boto3 setup and auth would normally be here
###
s3_client = boto3.client("s3")
objects = s3_client.list_objects_v2(Bucket=bucket_name)["Contents"]
for file in objects:
# file key is the URI string
# if the names do not match check with a debugger if the URI
# looks like you would expect it to look
file_key: str = file["Key"]
key_split = file_key.split('/') # ensure the file_key is unquoted
如果您已经在大括号内定义了 exact 键,那么要从 s3 下载 json 文件,您将
file_content = s3_client.get_object(Bucket=bucket_name, Key=file_key)
## process the data accordingly
但同样,这里缺少您问题的意图。您能否尝试提供一个最小(至少理论上)的工作示例来说明您想要完成的任务?