[英]Extract db details from AWS secret manager throws an error - Python
我正在使用下面的 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)
您需要配置 AWS 憑證以與 AWS API 交互,否則您會收到Unable to locate credentials
錯誤。
配置憑據有多種方法,因此只需查看此文檔並選擇最適合您需求的方法即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.