这个错误通常表示AWS Vault没有正确配置或者使用了无效的安全令牌。下面是一种解决方法,包含一个代码示例:
$ brew install --cask aws-vault
$ aws-vault add my-profile
在上面的示例中,"my-profile"是你的AWS配置文件的名称,你需要提供有效的访问密钥和秘密访问密钥。
import boto3
from awscli.clidriver import create_clidriver
# 使用aws-vault获取临时凭证
driver = create_clidriver()
driver.main(['aws-vault', 'exec', 'my-profile', '--', 'python', 'script.py'])
# 使用boto3访问AWS资源
session = boto3.Session(profile_name='my-profile')
ec2_client = session.client('ec2')
response = ec2_client.describe_instances()
print(response)
在上面的示例中,我们首先使用aws-vault从配置文件"my-profile"中获取临时凭证。然后,我们使用boto3创建一个与该配置文件相关联的会话,并使用该会话访问AWS EC2实例。
请确保在运行你的代码之前,你已经通过aws-vault登录并获取了有效的安全令牌。
希望这个解决方法对你有帮助!