(403) 调用 HeadObject 操作时:在 python 中从 AWS Batch 访问 S3 时被禁止

(403) when calling the HeadObject operation: Forbidden when accessing S3 from AWS Batch in python

我创建了一个从 amazonlinux 生成的 docker 图像。它在我手动安装了 python3、pip 和 awscli。我还配置了 AWSCLI 以使用我的密钥和秘密密钥。当我从图像创建容器时,我可以毫无问题地与 S3 交互。

然后我使用 Dockerfile 从上面的自定义图像生成一个新图像。在其中,我安装了此任务所需的模块(boto3、numpy、pandas、scipy 和 spacy)以及自定义 python 代码。此图像是我用于 AWS Batch 的容器。

我的自定义 python 代码尝试使用以下方式从 S3 下载文件:

jsonText = None
s3 = boto3.client('s3', region_name='us-east-1')
with io.BytesIO() as file_stream:
    s3.download_fileobj(args.input_transcript_s3_bucket, args.input_transcript_s3_key, file_stream)
    wrapper = io.TextIOWrapper(file_stream, encoding='utf-8')
    wrapper.seek(0)
    jsonText = wrapper.read()
    wrapper.flush()
    file_stream.flush()

当通过 AWS Batch 触发 python 代码时,我收到以下错误:

botocore.exceptions.ClientError: An error occurred (403) when calling the HeadObject operation: Forbidden

我认为这是一个政策问题。我检查了 VPC 端点策略,发现它足够了:

{
    "Statement": [
        {
            "Action": "*",
            "Effect": "Allow",
            "Resource": "*",
            "Principal": "*"
        }
    ]
}

我已经生成了一个自定义批处理服务角色。我向其中添加了 AWSBatchServiceRole 以及 AmazonS3FullAccess 策略。我将这个新服务角色分配给了一个全新的计算环境,但运气不佳。

我不确定下一步该做什么或如何获取更多信息。有什么想法吗?

谢谢。

docker 容器正在以 'nobody' 用户身份启动。 AWS 配置(特别是 .aws 目录)只能作为根用户访问。

我更改了 docker 容器以将 .aws 目录从 /root 复制到 HD 的根目录,然后使其可供 'nobody' 访问:

cp -r /root/.aws /
chown nobody /.aws
chgrp nobody /.aws
cd /.aws
chown nobody *
chgrp nobody *

然后我进行了测试以确保 'nobody' 可以访问凭据:

su -s /bin/bash nobody
aws s3 ls

这解决了我的问题。

I am not very happy with this solution since it exposes my AWS key and secret key to the 'nobody' user, but find myself in a bit of a catch 22. I created this post to see if there are other options.