Amazon web services 设置MFA后,使用boto3访问aws dynamodb。获取ClientError
以前,当我没有将MFA设置为登录AWS控制台时,我已经通过Amazon web services 设置MFA后,使用boto3访问aws dynamodb。获取ClientError,amazon-web-services,amazon-dynamodb,Amazon Web Services,Amazon Dynamodb,以前,当我没有将MFA设置为登录AWS控制台时,我已经通过 dynamo = boto3.resource('dynamodb', region_name='ap-northeast-2', endpoint_url='http://dynamodb.ap-northeast-2.amazonaws.com') table = dynamo.Table('tablename') 查询到那张桌子非常好
dynamo = boto3.resource('dynamodb',
region_name='ap-northeast-2',
endpoint_url='http://dynamodb.ap-northeast-2.amazonaws.com')
table = dynamo.Table('tablename')
查询到那张桌子非常好
response = table.query(
KeyConditionExpression =Key("user_id").eq(123123)
)
在我将MFA设置为附加安全性以登录AWS控制台之后,现在当我执行上述代码时,我得到:
ClientError: An error occurred (UnrecognizedClientException) when calling the Query operation: The security token included in the request is invalid.
我使用tunnel for RDB,是否有类似的东西可以用于连接dynamodb,或者是否有访问dynamodb所需的权限?当您启用MFA时,SDK不会自动使用它。常规IAM用户的API和密钥不再足够。相反,您需要使用仅为MFA会话创建的临时凭据 要使MFA与boto3一起工作,必须显式调用: 启用MFA的IAM用户需要调用GetSessionToken,并提交与其MFA设备关联的MFA代码。使用调用返回的临时安全凭据,IAM用户可以对需要MFA身份验证的API操作进行编程调用 使用
get_session_token
您可以调用sts
服务,该服务将根据您的MFA详细信息为您提供临时凭证:
sts = boto3.client('sts')
mfa_response = sts.get_session_token(
DurationSeconds=123,
SerialNumber='string',
TokenCode='string'
)
呼叫将返回mfa_response
中的凭据,您可以使用该凭据创建新的boto3会话。例如:
mfa_session = boto3.session.Session(
aws_access_key_id=mfa_session['Credentials']['AccessKeyId'],
aws_secret_access_key=mfa_session['Credentials']['SecretAccessKey'],
aws_session_token=mfa_session['Credentials']['SessionToken'])
dynamo = mfa_session.resource('dynamodb', ...)
# and the rest of the code
什么是序列号?我一直在获取
ParamValidationError:参数验证失败:参数DurationSeconds的范围无效,值:3,有效范围:900 inf参数SerialNumber的长度无效,值:6,有效范围:9-inf
可以通过将123替换为900和将“string”替换为stringgggg来修复,但当我在TokenCode中插入mfa代码时,它会一直给我ClientError
@Ambleu序列号示例显示我进入控制台,复制粘贴的指定MFA设备名称,该名称类似于arn:aws:iam……
我已经输入了令牌代码,就像我的手机MFA给我的一样。它仍然给了ClientError。我参考了一些关于每一个的信息parameter@Ambleu可以尝试在aws CLI中执行此操作吗。现在不行。在尝试在boto3中使用MFA之前,最好尝试最基本的方法,以确保MFA按预期工作。