[英]Python process 1 file at a time using boto3
我有一个 Python 脚本需要读取和处理 100 个文件。 我的脚本如何在一次只处理 1 个文件的情况下运行? 我的print(len(session_results))
应该有 2 个不同的长度。
def fetch_endpoint(endpoint: str, date: str) -> str:
"""Read files from s3 bucket and aggregate them via generator function"""
client = aws_s3_client_auth()
resource = aws_s3_resource_auth()
print(f"{endpoint.upper()}/{date}/")
paginator = client.get_paginator('list_objects_v2')
operation_parameters: dict[str] = {
"Bucket": RAW_BUCKET,
"Prefix": f"{endpoint.upper()}/{date}/"
}
keys_to_process: list[str] = []
page_iterator = paginator.paginate(**operation_parameters, PaginationConfig={'MaxItems': 100})
for page in page_iterator:
if page.get("KeyCount") == 0:
return print(f'No data was found for {endpoint} on {date}')
else:
for content in page.get("Contents"):
# print(content)
keys_to_process.append(content.get("Key"))
print(keys_to_process)
index = 0
while index < len(keys_to_process):
print(keys_to_process[index])
client_events = resource.Object(RAW_BUCKET, key=keys_to_process[index])
file_content = client_events.get()['Body'].read().decode('utf-8')
for line in file_content.splitlines():
data = json.loads(line)
index += 1
yield data
def session(endpoint: str, date: str) -> None:
"""Make a request to s3 to read session in the RAW_BUCKET"""
session_results = [line for line in fetch_endpoint(endpoint, date)]
print(len(session_results))
return None
session_results
是一个列表,其中列表中的每个元素都是由fetch_endpoint()
产生的。
当您print(len(session_results))
时,您是在告诉 Python 告诉您session_results
中有多少元素。
我不太确定“应该有 2 个不同的长度”是什么意思。 它是一个列表,一次只能有 1 个长度。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.