PyCharm:为什么 "Python Console" 没有访问 ~\.aws\credentials 文件?如何在 "Python Console" 内设置

PyCharm: Why "Python Console" is not accessing ~\.aws\credentials file? How to set it within "Python Console"

我正在 Pycharm 中开发 AWS DynamoDb 表。为此,我使用 Python 3.6 创建了一个虚拟环境,并安装了所需的库,如 boto3。我还在 ~/.aws/credentials 文件中使用 AWS CLI 工具设置了我的 AWS 凭证。

问题是当我简单地 运行 代码时,它就像一个魅力并且能够读取凭据文件。但是,当我 select 到 运行 "Python console" 中的代码时,我收到凭据已过期的错误。在我看来,"Python console" 以某种方式无法访问 ~/.aws/credentials 文件,并且正在其他地方寻找凭据。或者当我在 python 控制台中 select 编码为 运行 时,boto3 没有从 ~/.aws/credentials 访问凭据文件。

有人可以指导我如何在 Python 控制台中设置凭据,以便我可以 运行 交互代码。

谢谢,

来自Credentials — Boto 3 Docs 1.9.61 documentation

The mechanism in which boto3 looks for credentials is to search through a list of possible locations and stop as soon as it finds credentials. The order in which Boto3 searches for credentials is:

  • Passing credentials as parameters in the boto.client() method
  • Passing credentials as parameters when creating a Session object
  • Environment variables
  • Shared credential file (~/.aws/credentials)
  • AWS config file (~/.aws/config)
  • Assume Role provider
  • Boto2 config file (/etc/boto.cfg and ~/.boto)
  • Instance metadata service on an Amazon EC2 instance that has an IAM role configured.

因此,如果它没有使用凭据文件,它可能正在从环境变量中获取凭据。

我在 Intellij 中使用 Pycharm 遇到了同样的问题。

Boto3 找不到凭据文件,我没有设置默认配置文件。

> os.environ["AWS_SHARED_CREDENTIALS_FILE"]
None
> os.environ["AWS_DEFAULT_PROFILE"]
None

解决方法,我显式设置了变量

> import boto3
> import os
> os.environ["AWS_SHARED_CREDENTIALS_FILE"] = "<full-path>/.aws/credentials"
> os.environ["AWS_DEFAULT_PROFILE"] = "<profile-name>"

> boto3.client('sts').get_caller_identity().get('Account')
1234567890