1

我读过这个问题,我有一个类似的问题,但是通过打印调试信息,我得到了一些不同的东西,我不确定我在这里缺少什么:

当我运行以下代码时,我总是会收到此错误。

    s3 = boto3.resource('s3')
    bucket_name = "python-sdk-sample"
    print("Creating new bucket with name:", bucket_name)
    s3.create_bucket(Bucket=bucket_name)

我已将我的凭据文件保存在

C:\Users\myname\.aws\credentials, Boto 应该从那里读取我的凭据。

我的设置错了吗?

这是来自 的输出boto3.set_stream_logger('botocore', level='DEBUG')

2020-11-21 18:18:39,686 botocore.credentials [DEBUG] Looking for credentials via: env
2020-11-21 18:18:39,686 botocore.credentials [DEBUG] Looking for credentials via: assume-role
2020-11-21 18:18:39,686 botocore.credentials [DEBUG] Looking for credentials via: assume-role-with-web-identity
2020-11-21 18:18:39,686 botocore.credentials [DEBUG] Looking for credentials via: sso
2020-11-21 18:18:39,686 botocore.credentials [DEBUG] Looking for credentials via: shared-credentials-file
2020-11-21 18:18:39,686 botocore.credentials [DEBUG] Looking for credentials via: custom-process
2020-11-21 18:18:39,686 botocore.credentials [DEBUG] Looking for credentials via: config-file
2020-11-21 18:18:39,687 botocore.credentials [DEBUG] Looking for credentials via: ec2-credentials-file
2020-11-21 18:18:39,687 botocore.credentials [DEBUG] Looking for credentials via: boto-config
2020-11-21 18:18:39,687 botocore.credentials [DEBUG] Looking for credentials via: container-role
2020-11-21 18:18:39,687 botocore.credentials [DEBUG] Looking for credentials via: iam-role
2020-11-21 18:18:39,693 botocore.utils [DEBUG] Caught retryable HTTP exception while making metadata service request to http://xxx.xxx.xxx.xxx/latest/api/token: Could not connect to the endpoint URL: "http://xxx.xxx.xxx.xxx/latest/api/token"
4

1 回答 1

2

根据我的经验,当您在 boto3 会话中错误地指定了凭证时,这似乎会发生,因此在与 AWS 通信时会失败。config也就是说,您在 boto3 中加载的配置文件名称与您的文件中的配置文件不匹配credentials。所以对于一个看起来像这样的配置:

[profile dev]
us-east-2

匹配的凭据配置文件还应包含[profile dev],并且您使用以下内容加载会话:

import boto3
session = boto3.session.Session(profile_name='profile dev')

你指出的错误发生在我使用时:

import boto3
session = boto3.session.Session(profile_name='dev')

对于像 'foobar' 这样完全不正确的名称,错误消息更清楚一些:

ProfileNotFound: The config profile (foobar) could not be found
于 2021-04-30T19:02:10.077 回答