我正在编写一个 Python 脚本,该脚本通过 Athena 运行查询,将其输出到 S3 并将其下载到我的计算机中。我能够通过 Athena 运行我的查询并将结果输出到 S3。所以我似乎无法弄清楚的下一步是如何在不知道密钥名称的情况下将其下载到我的计算机上?
有没有办法在输出到 Athena 后在我的 python 脚本中查找对象键?
我已经完成的:
# Output location and DB
s3_output = ‘s3_output_here’
database = ‘database_here’
# Function to run Athena query
def run_query(query, database, s3_output):
while True:
try:
response = client.start_query_execution(
QueryString=query,
QueryExecutionContext={
'Database': database
},
ResultConfiguration={
'OutputLocation': s3_output,
}
)
return response
break
except client.exceptions.TooManyRequestsException as e:
print('Too many requests, trying again after sleep')
time.sleep(100)
# Our SQL Query
query = """
SELECT *
FROM test
”””
print("Running query to Athena...")
res = run_query(query, database, s3_output)
我了解如何使用此代码下载文件:
try:
s3.Bucket(BUCKET_NAME).download_file(KEY, ‘KEY_HERE’)
except botocore.exceptions.ClientError as e:
if e.response['Error']['Code'] == "404":
print("The object does not exist.")
else:
raise
那么如何在运行我的第一个完整代码后读取密钥名称?
富国沪深
相关分类