import os
import boto3
import base64
from aws_secretsmanager_caching import SecretCache, SecretCacheConfig
import json
from botocore.exceptions import ClientError
def get_dbsecret():
secret_name = os.getenv('DATABASE_SECRET')
region_name = os.getenv('SECRETS_MANAGER_REGION')
# Create a Secrets Manager client
session = boto3.session.Session()
client = session.client(
service_name='secretsmanager',
region_name=region_name
)
try:
# Create a cache
cache = SecretCache(SecretCacheConfig(),client)
# Get secret string from the cache
get_secret_value_response = cache.get_secret_string(secret_name)
return get_secret_value_response
except ClientError as e:
if e.response['Error']['Code'] == 'DecryptionFailureException':
# Deal with the exception here, and/or rethrow at your discretion.
raise e
elif e.response['Error']['Code'] == 'InternalServiceErrorException':
# An error occurred on the server side.
# Deal with the exception here, and/or rethrow at your discretion.
raise e
elif e.response['Error']['Code'] == 'InvalidParameterException':
# You provided an invalid value for a parameter.
# Deal with the exception here, and/or rethrow at your discretion.
raise e
elif e.response['Error']['Code'] == 'InvalidRequestException':
# You provided a parameter value that is not valid for the current state of the resource.
# Deal with the exception here, and/or rethrow at your discretion.
raise e
elif e.response['Error']['Code'] == 'ResourceNotFoundException':
# We can't find the resource that you asked for.
# Deal with the exception here, and/or rethrow at your discretion.
raise e
else:
print("-------------------------IN ELSE---------------------------------")
secret = client.get_secret_value(
SecretId=secret_name
)
return secret
Я написал приведенный выше код, чтобы прочитать секретное имя. Секретное имя и регион задаются как переменные среды. Когда я вызываю приведенный выше код, функция всегда возвращает значение NONE. Я что-то делаю не так?