Python Boto3 MFA与Access_Key_Id、Access_Key、Session_Token和MFA建立连接,而不传递RoleArn

2024-04-28 06:25:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我们是否可以使用PythonBoto3和以下命令建立AWS连接,以列出和获取具有临时会话的对象?没有通过RoleArn

\u AWS\u访问\u密钥\u ID

_AWS_SECRET_ACCESS_KEY

_AWS_会话_令牌

MFA代码

我只有低于临时课程,我应该如何通过这个,因为我没有roleArn

enter image description here

我也查看了帖子 boto3 sessions and aws_session_token management 但所有人都在使用roleArn


Tags: 对象key代码命令mfaawsidsecret
1条回答
网友
1楼 · 发布于 2024-04-28 06:25:47

通过运行此代码,它不需要RoleArn

import boto
from boto.s3.connection import S3Connection
from boto.sts import STSConnection

# Prompt for MFA time-based one-time password (TOTP)
mfa_TOTP = raw_input("Enter the MFA code: ")

# The calls to AWS STS GetSessionToken must be signed with the access key ID and secret
# access key of an IAM user. The credentials can be in environment variables or in 
# a configuration file and will be discovered automatically
# by the STSConnection() function. For more information, see the Python SDK 
# documentation: http://boto.readthedocs.org/en/latest/boto_config_tut.html

sts_connection = STSConnection()

# Use the appropriate device ID (serial number for hardware device or ARN for virtual device). 
# Replace ACCOUNT-NUMBER-WITHOUT-HYPHENS and MFA-DEVICE-ID with appropriate values.

tempCredentials = sts_connection.get_session_token(
    duration=3600,
    mfa_serial_number="&region-arn;iam::ACCOUNT-NUMBER-WITHOUT-HYPHENS:mfa/MFA-DEVICE-ID",
    mfa_token=mfa_TOTP
)

# Use the temporary credentials to list the contents of an S3 bucket
s3_connection = S3Connection(
    aws_access_key_id=tempCredentials.access_key,
    aws_secret_access_key=tempCredentials.secret_key,
    security_token=tempCredentials.session_token
)

# Replace BUCKET-NAME with an appropriate value.
bucket = s3_connection.get_bucket(bucket_name="BUCKET-NAME")
objectlist = bucket.list()
for obj in objectlist:
    print obj.name

相关问题 更多 >