如何使用Boto3获取AWS账户中存在的架构列表
在本文中,我们将看到用户如何获取 AWS 账户中存在的所有架构的列表。
更多Python相关文章,请阅读:Python 教程
示例
获取在 AWS Glue 数据目录中可用的所有架构的列表。
问题陈述: 使用 Python 中的 boto3 库获取所有架构的列表。
解决此问题的方法/算法
- 步骤1: 导入 boto3 和 botocore 异常以处理异常。
-
步骤2: 此函数中没有参数。
-
步骤3: 使用 boto3 lib 创建 AWS 会话。确保在默认配置文件中提到了 region_name 。如果没有提及,请在创建会话时明确传递 region_name 。
-
步骤4: 为 glue 创建 AWS client。
-
步骤5: 现在使用 list_schemas 函数。
-
步骤6: 它返回 AWS Glue 数据目录中存在的所有架构的列表,并提供架构的有限详细信息。它不包括其状态为Deleting的架构。它仅包含可用架构的列表。如果没有架构,则返回空字典。
-
步骤7: 处理通用异常,如果在检查架构时出现问题,则捕获异常。
示例代码
以下代码获取所有架构列表 –
import boto3
from botocore.exceptions import 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': ……
}
极客教程