python代码出错:botocore.exceptions.NoCredentialsError:找不到凭据

2024-10-01 11:22:07 发布

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

我在运行以下代码时遇到了如下错误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')

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

^{pr2}$

所以基本上在我们的组织中,有一个名为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 profile部分在凭证中,不工作

当我调用这样的角色时:

 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细节的会话,最后使用STS客户端为假定角色创建一个新的会话。使用此会话可以连接到DynamoDB。在

我们非常感谢你的帮助。在


Tags: tokeydevmfaawsdefault角色access