我正在使用下面的Python代码从密码管理器中提取数据库详细信息,但继续收到相同的错误“无法找到凭据”。我在做什么错了?
import boto3 # Required to interact with AWS
import psycopg2
import base64
from botocore.exceptions import ClientError
secret_name = "X"
region_name = "X"
session = boto3.session.Session()
client = session.client(
service_name='secretsmanager',
region_name=region_name
)
try:
get_secret_value_response = client.get_secret_value(
SecretId=secret_name
)
except ClientError as e:
if e.response['Error']['Code'] == 'ResourceNotFoundException':
print("The requested secret " + secret_name + " was not found")
elif e.response['Error']['Code'] == 'InvalidRequestException':
print("The request was invalid due to:", e)
elif e.response['Error']['Code'] == 'InvalidParameterException':
print("The request had invalid params:", e)
else:
# Decrypted secret using the associated KMS CMK
# Depending on whether the secret was a string or binary, one of these fields will be populated
if 'SecretString' in get_secret_value_response:
secret = json.loads(get_secret_value_response['SecretString'])
else:
binary_secret_data = get_secret_value_response['SecretBinary']
user = secret['username']
password = secret['password']
db_name = secret['dbname']
host = secret['host']
conn = psycopg2.connect(host='host',port='5432',database=db_name, user=user, password=password)
答案 0 :(得分:0)
您需要配置AWS凭证以与AWS API交互,否则会出现Unable to locate credentials
错误。
有多种方法来配置凭据,因此只需选中此doc并选择最适合您的需求即可。