python - 尝试使用执行 ID 检索对象时 AWS Athena python 连接 S3 错误

标签 python amazon-web-services boto3

当尝试读取 CSV 形式的 S3 对象时,响应是 AWS Athena 查询的执行 ID:

def run_query(query, database, s3_output):
    client = boto3.client('athena')
    response = client.start_query_execution(
        QueryString=query,
        QueryExecutionContext={
            'Database': database
            },
        ResultConfiguration={
            'OutputLocation': s3_output,
            }
        )
    print('Execution ID: ' + response['QueryExecutionId'])
    return response

response = run_query(query1, db, s3_output)
result = get_exec_status(response)
print(result)

s3_resource = boto3.resource('s3')
s3_client = boto3.client('s3')

def read_s3(path):
    path = path.replace("s3://", "")
    bucket, key = path.split('/', 1)
    s3_client.copy_object(Bucket=bucket, CopySource=path, Key=".csv")
    s3_client.delete_object(Bucket=bucket, Key=key)

read_s3("s3://"+ response + ".csv")

Error:
 File "athena_connect.py", line 67, in <module>
    read_s3("s3://"+ response + ".csv")
  File "athena_connect.py", line 64, in read_s3
    s3_client.copy_object(Bucket=bucket, CopySource=path, Key=".csv")
botocore.errorfactory.NoSuchKey: An error occurred (NoSuchKey) when calling the CopyObject operation: The specified key does not exist.

但是,当 响应='somekey' 这段代码工作正常。可能出了什么问题?

最佳答案

错误是:

The specified key does not exist

这意味着程序正在尝试读取 Amazon S3 中不存在的对象。

这一行:

read_s3("s3://"+ response + ".csv")

期望response是一个包含文件 key 的字符串。

但是,response 之前被用作字典:

print('Execution ID: ' + response['QueryExecutionId'])

因此,最好使用:

read_s3("s3://"+ response['QueryExecutionId'] + ".csv")

关于python - 尝试使用执行 ID 检索对象时 AWS Athena python 连接 S3 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54959342/

相关文章:

python - 如何确定我制作的随机 2D 地牢中正确的墙 block ?

python - aiosmtpd - python smtp 服务器

python - 在 python 中在另一个类中创建一个类的正确方法?

python-3.6 - 使用 boto3 删除 Cloudwatch 日志

Python boto3 从禁用备份的快照创建 rds 实例

python - 使用 boto3 从 EFS Amazon 下载文件

python - 如果列中的值在一组值列表中,则过滤数据框行

email - AWS SES 回复上一封电子邮件

amazon-web-services - 重命名 CloudFormation 模板中的资源

python - 解决 AWS Lambda 空间限制