如何使用 Boto3 获取 AWS 账户中存在的架构列表
在本文中,我们将了解用户如何获取AWS账户中存在的所有架构的列表。
示例
获取AWSGlue数据目录中所有可用架构的列表。
问题陈述:使用Python中的boto3库获取所有模式的列表。
解决这个问题的方法/算法
第一步:导入boto3和botocore异常处理异常。
步骤2:此函数中没有参数。
步骤3:使用boto3lib创建AWS会话。确保在默认配置文件中提到region_name。如果未提及,则在创建会话时显式传递region_name。
第4步:为glue创建一个AWS客户端。
第5步:现在使用list_schemas函数
步骤6:它返回AWSGlue数据目录中存在的所有架构的列表,其中包含有限的架构详细信息。它不包括状态为正在删除的模式。它只有可用模式的列表。如果没有模式,则它返回一个空的字典。
第7步:如果在检查模式时出现问题,则处理通用异常。
示例代码
以下代码获取所有模式的列表-
import boto3
frombotocore.exceptionsimport ClientError
def list_of_schemas()
session = boto3.session.Session()
glue_client = session.client('glue')
try:
schemas_name = glue_client.list_schemas()
return schemas_name
except ClientError as e:
raise Exception("boto3 client error in list_of_schemas: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in list_of_schemas: " + e.__str__())
print(list_of_schemas())输出结果{
'Schemas':[
{
'RegistryName': 'employee_details',
'SchemaName': 'employee_table',
'SchemaArn': 'string',
'Description': 'Schema for employees record',
'Status': 'AVAILABLE',
'CreatedTime': 'string',
'UpdatedTime': 'string'
},
{
'RegistryName': 'security_details',
'SchemaName': 'security_table',
'SchemaArn': 'string',
'Description': 'Schema for security record',
'Status': 'AVAILABLE',
'CreatedTime': 'string',
'UpdatedTime': 'string'
},
],
'Request': ……
}