【问题标题】:StartQueryExecution operation: Unable to verify/create output bucketStartQueryExecution 操作:无法验证/创建输出存储桶
【发布时间】:2021-05-26 15:29:21
【问题描述】:

我正在尝试使用 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
        }
    )

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

  • 有一天它会抛出一个 InvalidRequestException 错误 错误
InvalidRequestException: An error occurred (InvalidRequestException) when calling the StartQueryExecution operation: Unable to verify/create output bucket <BUCKET NAME>
  • 根据 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": "*"
        }
    ]
}

【问题讨论】:

  • 遇到完全相同的问题。为什么 AWS 允许如此复杂?根本没有理由在控制台中而不是在 CLI 中允许这样做。
  • 对此有任何更新,你能解决这个问题吗,我面临着类似的问题。

标签: python amazon-web-services amazon-s3 amazon-athena pyathena


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我遇到了同样的问题 - 随机故障。问题原来是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>/*
    

    查看文档:https://docs.aws.amazon.com/AmazonS3/latest/userguide/using-with-s3-actions.html

    【讨论】:

    • 这对我也有用!
    猜你喜欢
    • 2021-04-16
    • 2019-05-31
    • 1970-01-01
    • 2018-02-18
    • 2021-07-03
    • 1970-01-01
    • 2020-03-02
    • 2020-02-15
    相关资源
    最近更新 更多