在部署在 Sagemaker Endpoint 上的 Docker 容器中使用 boto3 下载文件
Download file using boto3 within Docker container deployed on Sagemaker Endpoint
我已经构建了自己的 Docker 容器,该容器提供推理代码以作为端点部署在 Amazon Sagemaker 上。但是,此容器需要能够访问 s3 中的某些文件。使用的 IAM 角色可以访问我尝试访问的所有 s3 存储桶。
使用 boto3 客户端下载文件的代码:
import boto3
model_bucket = 'my-bucket'
def download_file_from_s3(s3_path, local_path):
client = boto3.client('s3')
client.download_file(model_bucket, s3_path, local_path)
IAM 角色的策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::my-bucket/*"
]
}
]
}
在本地启动 docker 容器允许我像预期的那样从 s3 下载文件。
在 Sagemaker 上部署为端点,但是,请求超时:
botocore.vendored.requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='my-bucket.s3.eu-central-1.amazonaws.com', port=443): Max retries exceeded with url: /path/to/my-file (Caused by ConnectTimeoutError(<botocore.awsrequest.AWSHTTPSConnection object at 0x7f66244e69b0>, 'Connection to my-bucket.s3.eu-central-1.amazonaws.com timed out. (connect timeout=60)'))
感谢任何帮助!
出于安全原因,他们不允许它本地访问 s3,您需要将它连接到 VPC
https://docs.aws.amazon.com/sagemaker/latest/dg/host-vpc.html
对于遇到此问题的任何人,在创建模型时,'Enable Network Isolation' 属性 默认为 True。
来自 AWS 文档:
If you enable network isolation, the containers are not able to make any outbound network calls, even to other AWS services such as Amazon S3. Additionally, no AWS credentials are made available to the container runtime environment.
所以这个 属性 需要设置为 False 才能连接到任何其他 AWS 服务。
我已经构建了自己的 Docker 容器,该容器提供推理代码以作为端点部署在 Amazon Sagemaker 上。但是,此容器需要能够访问 s3 中的某些文件。使用的 IAM 角色可以访问我尝试访问的所有 s3 存储桶。
使用 boto3 客户端下载文件的代码:
import boto3
model_bucket = 'my-bucket'
def download_file_from_s3(s3_path, local_path):
client = boto3.client('s3')
client.download_file(model_bucket, s3_path, local_path)
IAM 角色的策略:
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"s3:GetObject",
"s3:PutObject",
"s3:DeleteObject"
],
"Effect": "Allow",
"Resource": [
"arn:aws:s3:::my-bucket/*"
]
}
]
}
在本地启动 docker 容器允许我像预期的那样从 s3 下载文件。
在 Sagemaker 上部署为端点,但是,请求超时:
botocore.vendored.requests.exceptions.ConnectTimeout: HTTPSConnectionPool(host='my-bucket.s3.eu-central-1.amazonaws.com', port=443): Max retries exceeded with url: /path/to/my-file (Caused by ConnectTimeoutError(<botocore.awsrequest.AWSHTTPSConnection object at 0x7f66244e69b0>, 'Connection to my-bucket.s3.eu-central-1.amazonaws.com timed out. (connect timeout=60)'))
感谢任何帮助!
出于安全原因,他们不允许它本地访问 s3,您需要将它连接到 VPC https://docs.aws.amazon.com/sagemaker/latest/dg/host-vpc.html
对于遇到此问题的任何人,在创建模型时,'Enable Network Isolation' 属性 默认为 True。 来自 AWS 文档:
If you enable network isolation, the containers are not able to make any outbound network calls, even to other AWS services such as Amazon S3. Additionally, no AWS credentials are made available to the container runtime environment.
所以这个 属性 需要设置为 False 才能连接到任何其他 AWS 服务。