Skip to main content
 首页 » 编程设计

python之在 Python 中使用 ARN iam 将文件上传到 Amazon s3 存储桶

2024年12月31日10yyy_WW

我得到了一个带有 ARN 编号的存储桶名称,如下所示:

arn:aws:iam::<>:user/user-name

我还获得了访问 key 。

我知道这可以使用 boto 来完成。

Connect to s3 bucket using IAM ARN in boto3

在上面的链接中,我需要使用“sts”吗? 如果是这样,为什么要向我提供访问 key ?

请您参考如下方法:

首先,我建议您安装 AWS Command-Line Interface (CLI) ,它提供了一个用于访问 AWS 的命令行。

然后您可以通过运行以下命令将您的凭据存储在配置文件中:

aws configure 

它会提示您输入访问 key 和 key ,它们将存储在配置文件中。

然后,您将要引用S3 — Boto 3 documentation了解如何从 Python 访问 Amazon S3。

下面是一些示例代码:

import boto3 
 
client = boto3.client('s3', region_name = 'ap-southeast-2') # Change as appropriate 
 
client.upload_file('/tmp/hello.txt', 'mybucket', 'hello.txt')