如何使用Boto3从AWS Glue资源获取标记

如何使用Boto3从AWS Glue资源获取标记

在本文中,我们将看到用户如何获取与AWS Glue资源关联的标记。

更多Python相关文章,请阅读:Python 教程

示例

从AWS Glue数据库获取标记“ glue-db:tests ”。

问题陈述: 使用Python中的boto3库从AWS Glue资源获取标记。

解决此问题的方法/算法

  • 第1步: 导入 boto3botocore 异常以处理异常。

  • 第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 lib 创建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}}

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程