python代码获取错误:botocore.exceptions.NoCredentialsError:无法找到凭据

时间:2018-01-30 07:34:49

标签: python amazon-web-services boto boto3 amazon-iam

当我运行以下代码时,我收到了类似botocore.exceptions.NoCredentialsError: Unable to locate credentials的错误:

# setup AWS Connection details
awsSession = boto3.Session(profile_name="opsdev")

def getInfoFromDynamoDB(service):
    client = awsSession.client('kms')
    dynamodb = awsSession.resource('dynamodb')
    table = dynamodb.Table('app_info')

    response = table.get_item(
        Key={
        # here is where the eroor comes out as "Unable to locate credentials"
        "service": str(service)
    }
)

MongodbInfo = getInfoFromDynamoDB('Mongodb')

我也尝试过不指定配置文件名称,但它会给出相同的错误

awsSession = boto3.Session()

所以基本上在我们的组织中,有一个名为DEV的组有权从dynamoDb查询,我将自己假设为DEV角色,然后使用DynamoDB,我们也使用MFA。

我在boto文档和StackOverflow中做了一些研究。 我想我无法手动指定密钥,因为我有一个角色可以假设,我将使用假设的角色密钥信息,如本文Boto3 Error: botocore.exceptions.NoCredentialsError: Unable to locate credentials

中所建议的那样

这是我的配置文件:

[default]
aws_access_key_id = xxxxxxxxxxxxxxxxQQ
aws_secret_access_key = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxGf

[profile opsdev]
output = json
role_arn = arn:aws:iam::123456789123:role/DEV
mfa_serial = arn:aws:iam::123456789123:mfa/abc@def.com
source_profile = default
region = us-east-1

这是我的凭据文件

[default]
aws_access_key_id = xxxxxxxxxxxxxxxxQQ
aws_secret_access_key = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxGf
[opsdev]
role_arn = arn:aws:iam::123456789123:role/DEV
mfa_serial = arn:aws:iam::123456789123:mfa/abc@def.com
source_profile = default
region = us-east-1

我已经尝试过不在凭据中使用opsdev配置文件部分,但仍然没有工作

当我调用这样的角色时:

 aws --profile=role s3 ls --debug

没有生成错误,只是一个Bucket名称列表

我试过

$ aws sts assume-role --role-arn arn:aws:iam::709957318545:role/DEV_OperationsDevelopers --role-session-name testAssumeRole

An error occurred (AccessDenied) when calling the AssumeRole operation: User: arn:aws:sts::123456789123:assumed-role/DEV/AWS-CLI-session-987654321987 is not authoried to perform: sts:AssumeRole on resource:arn:aws:iam::123456789123:role/DEV

$ aws configure list
Name                    Value             Type    Location
----                    -----             ----    --------
profile                   opsdev           manual    --profile
access_key     ****************xxIQ      assume-role
secret_key     ****************xxf0      assume-role
region                us-east-1      config-file    ~/.aws/config

$ aws sts get-caller-identity --profile opsdev
Enter MFA code:
{
"Account": "123456789123",
"UserId": "xxxxxxxxxxxxxxxxxxxxxxxx:AWS-CLI-session-987654321",
"Arn": "arn:aws:sts::123456789123:assumed-role/DEV/AWS-CLI-session-987654321"}

我非常确定我的身份能够执行假设角色操作,因为我们有一个python文件提示输入MFA代码然后获得MFA会话,然后创建另一个具有MFA详细信息的会话,最后一个新的ctreat一个新的与STS客户端的会话,用于承担角色。使用此会话可以连接到DynamoDB。

非常感谢帮助。

0 个答案:

没有答案
相关问题