如何使用 Boto3 获取 AWS 账户中所有注册表的列表
在本文中,我们将看到用户如何在 AWS 账户中获取所有注册表的列表。
阅读更多:Python 教程
示例
获取 AWS Glue 数据目录中可用的所有注册表的列表。
问题陈述: 使用 Python 中的 boto3 库获取所有注册表的列表。
解决问题的方法/算法
- 步骤 1: 导入 boto3 和 botocore 异常以处理异常情况。
-
步骤 2: 此函数中没有参数。
-
步骤 3: 使用 boto3 在 AWS 中创建会话。确保在默认配置文件中提到 ****region_name** ** 。如果没有提到,则在创建会话时明确传递 region_name 。
-
步骤 4: 为 glue 创建一个 AWS 客户端。
-
步骤 5: 现在使用 list_registries
-
步骤 6: 它返回 AWS Glue 数据目录中所有注册表的列表,并包含注册表的有限细节。它不包括状态为 Deleting 的注册表。它只有可用的注册表列表。如果没有注册表,则返回空字典。
-
步骤 7: 处理一般性异常,如果在检查作业时出现了什么问题。
示例代码
以下代码获取所有注册表的列表:
import boto3
from botocore.exceptions import ClientError
def list_of_registries()
session = boto3.session.Session()
glue_client = session.client('glue')
try:
registries_name = glue_client.list_registries()
return registries_name
except ClientError as e:
raise Exception("boto3 client error in list_of_registries: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in list_of_registries: " + e.__str__())
print(list_of_registries())
输出
{
'Registries':[
{
'RegistryName': 'employee_details',
'RegistryArn': 'string',
'Description': 'Registry for employees record',
'Status': 'AVAILABLE',
'CreatedTime': 'string',
'UpdatedTime': 'string'
},
{
'RegistryName': 'security_details',
'RegistryArn': 'string',
'Description': 'Registry for security record',
'Status': 'AVAILABLE',
'CreatedTime': 'string',
'UpdatedTime': 'string'
},
],
'Request': ……
}
极客教程