Athena查询失败,boto3(S3位置无效)

时间:2017-07-25 21:25:46

标签: python amazon-s3 boto3 amazon-athena

我正在尝试在Athena中执行查询,但它失败了。

代码:

client.start_query_execution(QueryString="CREATE DATABASE IF NOT EXISTS db;",
                           QueryExecutionContext={'Database': 'db'},
                           ResultConfiguration={
                                     'OutputLocation': "s3://my-bucket/",
                                     'EncryptionConfiguration': {
                                             'EncryptionOption': 'SSE-S3'
                                             }
                                     })

但它引发了以下异常:

botocore.errorfactory.InvalidRequestException: An error occurred (InvalidRequestException) 
when calling the StartQueryExecution operation: The S3 location provided to save your 
query results is invalid. Please check your S3 location is correct and is in the same 
region and try again. If you continue to see the issue, contact customer support 
for further assistance.

但是,如果我转到Athena控制台,请转到“设置”并输入相同的S3位置(例如):

athena settings

查询运行正常。

我的代码出了什么问题?我成功地使用了其他几个服务(例如S3)的API,但在这个中我相信我传递了一些不正确的参数。感谢。

Python:3.6.1。 Boto3:1.4.4

4 个答案:

答案 0 :(得分:3)

为保存查询结果而提供的S3位置无效。 请检查您的S3位置是否正确且位于同一区域,然后重试。

由于它在您使用控制台时起作用,因此存储桶可能与您在Boto3中使用的区域不同。在构建Boto3客户端时,请确保使用正确的区域(在控制台中工作的区域)。默认情况下,Boto3将使用凭证文件中配置的区域。

答案 1 :(得分:2)

我不得不添加一个' athena - '我的存储桶的前缀让它工作。例如,代替:

" S3://我的桶/"

尝试:

" S3://雅典娜-我的桶/"

答案 2 :(得分:1)

或者尝试boto3.client('athena', region_name = '<region>')

遇到同样的问题,需要在客户端指定S3存储桶。

答案 3 :(得分:0)

就我而言,IAM角色没有S3存储桶的所有权限。我为Athena结果存储区授予了IAM角色以下权限。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "s3:GetObject",
                "s3:ListBucket",
                "s3:PutObject",
                "s3:DeleteObject"
            ],
            "Resource": [
                "arn:aws:s3:::athena_results_bucket",
                "arn:aws:s3:::athena_results_bucket"
            ],
            "Effect": "Allow"
        }
    ]
}