有没有boto3 + MFA的例子?

Bo *_*ang 5 python amazon-web-services boto3

我找到了boto + MFA的示例:

http://docs.aws.amazon.com/IAM/latest/UserGuide/id_credentials_mfa_sample-code.html

但是我找不到如何使用boto3的示例。任何等效的boto3示例?

谢谢!

Aro*_*a20 6

您可以使用 sts 和 get_session_token 方法通过 boto3 调用 use MFA。对于此预先要求,您应该创建 sts 的客户端对象,然后使用 mfa 令牌调用该函数。这将为您提供可以使用的临时凭证,这些凭证的有效期为 36 小时代码 -

sts = boto3.client('sts')
response = sts.get_session_token(DurationSeconds=<time>, SerialNumber='string',TokenCode='string')
Run Code Online (Sandbox Code Playgroud)

http://boto3.readthedocs.io/en/latest/reference/services/sts.html#STS.Client.get_session_token


小智 5

下面的代码有效,但是您必须使用具有正确凭据的〜/ .boto文件。SerialNumber是您的MFA设备序列号或其完整的AWS信息

#!/usr/bin/env python

import boto3

mfa_TOTP = raw_input("Enter the MFA code: ")

client=boto3.client( 'sts' )

response = client.assume_role(
    RoleArn='arn:aws:iam::123456789:role/admin_full',
    RoleSessionName='mysession',
    DurationSeconds=3600,
    SerialNumber='arn:aws:iam::987654321:mfa/myaccount',
    TokenCode=mfa_TOTP,
)
Run Code Online (Sandbox Code Playgroud)

  • 好的,这可行......但现在我该如何使用它?假设我想读出一个 s3 存储桶。 (2认同)