StartQueryExecution 操作:无法验证/创建输出存储桶

use*_*ser 15 python amazon-s3 amazon-web-services amazon-athena pyathena

我正在尝试使用 python 在 Athena 上执行查询。

示例代码

   client = boto3.client(
        'athena', 
        region_name=region,
        aws_access_key_id=AWS_ACCESS_KEY_ID,
        aws_secret_access_key=AWS_SECRET_ACCESS_KEY
    )
    execution = client.start_query_execution(
        QueryString=query,
        QueryExecutionContext={
            'Database': database
        },
        WorkGroup=workgroup,
        ResultConfiguration={
            'OutputLocation': S3_OUTPUT_LOCATION
        }
    )

Run Code Online (Sandbox Code Playgroud)

这是工作代码,但我遇到了一个不寻常的情况。

  • 有一天它抛出一个 InvalidRequestException 错误 Error
InvalidRequestException: An error occurred (InvalidRequestException) when calling the StartQueryExecution operation: Unable to verify/create output bucket <BUCKET NAME>
Run Code Online (Sandbox Code Playgroud)
  • 由于 DevOps 应用程序拥有所有权限,它应该可以工作。
  • 我们尝试在 AWS Athena 控制台(查询编辑器)上执行相同的查询。在那里它正在工作。
  • 然后我们重新运行 python 脚本,它没有抛出任何错误。
  • 但第二天,Python 脚本开始抛出相同的 InvalidRequestException 错误。
  • 然后我们在 AWS Athena 控制台(查询编辑器)上执行相同的查询并重新运行 python 脚本,它开始工作。

我们观察这种情况几天了,每 24 小时 python 脚本都会抛出错误,然后我们在 Athena 控制台(查询编辑器)上执行查询并重新运行 python 脚本。我不明白为什么会发生这种情况,是否存在任何权限问题。

权限:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "s3:GetObject",
                "athena:GetWorkGroup",
                "athena:StartQueryExecution",
                "athena:ListDatabases",
                "athena:StopQueryExecution",
                "athena:GetQueryExecution",
                "athena:GetQueryResults",
                "athena:GetDatabase",
                "athena:GetDataCatalog",
                "athena:ListQueryExecutions",
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::<BUCKET NAME>",
                "arn:aws:s3:::<BUCKET NAME>/*",
            ]
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:ListBucket",
                "athena:UpdateWorkGroup",
            ],
            "Resource": [
                "arn:aws:s3:::<BUCKET NAME>/*",
                "arn:aws:s3:::<BUCKET NAME>",
                "arn:aws:athena:*:<BUCKET NAME>/<PATH>",
            ]
        },
        {
            "Sid": "VisualEditor2",
            "Effect": "Allow",
            "Action": [
                "athena:ListDataCatalogs",
                "s3:ListAllMyBuckets"
            ],
            "Resource": "*"
        }
    ]
}
Run Code Online (Sandbox Code Playgroud)

Han*_* R. 17

我遇到了同样的问题 - 随机失败。事实证明,问题是s3:GetBucketLocation策略配置错误。它与其他 s3 操作捆绑在同一集群中,其中资源指向 s3 存储桶,包括路径。这是行不通的。

我按如下方式修复了它,现在可以使用了。

- Effect: Allow
  Action:
    - s3:GetBucketLocation
  Resource:
    - arn:aws:s3:::*
- Effect: Allow
  Action:
    - s3:PutObject
    - s3:GetObject
  Resource:
    - arn:aws:s3:::<BUCKET NAME>/<PATH>/*
Run Code Online (Sandbox Code Playgroud)

请参阅文档:https://docs.aws.amazon.com/AmazonS3/latest/userguide/using-with-s3-actions.html


小智 9

我今天也遇到了同样的错误,发现执行角色也需要s3:GetBucketLocation权限,AWS 文档: https: //aws.amazon.com/premiumsupport/knowledge-center/athena-output-bucket-error/