如何使用 Boto3 获取 AWS Glue 资源的标签
在本文中,我们将了解用户如何获取与 AWS Glue 资源关联的标签。
示例
从 AWS Glue 数据库获取标签“glue-db: tests”。
问题陈述:使用 Python 中的 boto3 库从 AWS Glue 资源获取标签。
解决此问题的方法/算法
步骤 1:导入 boto3 和 botocore 异常以处理异常。
步骤 2:resource_arn 是此函数中所需的参数。
resource_arn 的格式应如下所示:
| 目录 | arn:aws:glue:region:account-id:catalog |
| 数据库 | arn:aws:glue:region:account-id:database/database name |
| 表 | arn:aws:glue:region:account-id:table/database name/table name |
| 连接 | arn:aws:glue:region:account-id:connection/connection name |
| 爬虫 | arn:aws:glue:region:account-id:crawler/crawler-name |
| 作业 | arn:aws:glue:region:account-id:job/job-name |
| 触发器 | arn:aws:glue:region:account-id:trigger/trigger-name |
| 开发端点 | arn:aws:glue:region:account-id:devEndpoint/development-endpoint-name |
| 机器学习转换 | arn:aws:glue:region:account-id:mlTransform/transform-id |
步骤 3:使用 boto3 库创建 AWS 会话。确保在默认配置文件中提到了 region_name。如果未提及,则在创建会话时显式传递 region_name。
步骤 4:为 glue 创建 AWS 客户端。
步骤 5:现在使用 get_tags 函数并将参数 resource_arn 作为 ResourceArn 传递。
步骤 6:它返回资源的标签和响应元数据。
步骤 7:如果在获取标签时出现错误,则处理通用异常。
示例代码
使用以下代码获取标签:
import boto3
from botocore.exceptions import ClientError
def get_tags_from_resource(resource_arn)
session = boto3.session.Session()
glue_client = session.client('glue')
try:
response = glue_client.get_tags(ResourceArn= resource_arn)
return response
except ClientError as e:
raise Exception("boto3 client error in get_tags_from_resource: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in get_tags_from_resource: " + e.__str__())
print(add_tags_in_resource("arn:aws:glue:us-east-1:1122225*****88:database/test-db"))输出
{'Tags': {'glue-job': 'test'}, 'ResponseMetadata': {'RequestId': 'c9f418b0-8d02-4a26-*************', 'HTTPStatusCode': 200, 'HTTPHeaders': {'date': 'Fri, 02 Apr 2021 08:04:54 GMT', 'content-type': 'application/x-amz-json-1.1', 'content-length': '27', 'connection': 'keep-alive', 'x-amzn-requestid': 'c9f418b0-8d02-4a26-**************'}, 'RetryAttempts': 0}}
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP