如何使用 Boto3 從 AWS Glue 資源中移除標籤
在本文中,我們將學習如何從 AWS Glue 資源中移除標籤。
示例
從 AWS Glue 資料庫中移除標籤“glue-db: tests”。
問題陳述:使用 Python 中的 boto3 庫來移除 AWS Glue 資源中的標籤。
解決此問題的步驟/演算法
步驟 1:匯入 **boto3** 和 **botocore** 異常來處理異常。
步驟 2:**resource_arn** 和 **tags_list** 是此函式所需的必備引數。
**resource_arn** 的格式應如下所示:
| 目錄 | arn:aws:glue:region:account-id:catalog |
| 資料庫 | arn:aws:glue:region:account-id:database/資料庫名稱 |
| 表 | arn:aws:glue:region:account-id:table/資料庫名稱/表名稱 |
| 連線 | arn:aws:glue:region:account-id:connection/連線名稱 |
| 爬蟲 | arn:aws:glue:region:account-id:crawler/爬蟲名稱 |
| 作業 | arn:aws:glue:region:account-id:job/作業名稱 |
| 觸發器 | arn:aws:glue:region:account-id:trigger/觸發器名稱 |
| 開發端點 | arn:aws:glue:region:account-id:devEndpoint/開發端點名稱 |
| 機器學習轉換 | arn:aws:glue:region:account-id:mlTransform/轉換ID |
**tags_list** 應為 ["key1","key2"...] 的格式。
步驟 3:使用 **boto3 庫** 建立 AWS 會話。確保在預設配置檔案中提到了 **region_name**。如果沒有提到,則在建立會話時顯式傳遞 **region_name**。
步驟 4:為 **glue** 建立 AWS 客戶端。
步驟 5:現在使用 **untag_resource** 函式並將引數 **resource_arn** 作為 ResourceArn 和 **tags_list** 作為 TagsToRemove 傳遞。
步驟 6:它返回響應元資料並移除資源中的標籤。
步驟 7:如果在移除標籤時出現問題,則處理通用異常。
示例程式碼
使用以下程式碼移除標籤:
import boto3
from botocore.exceptions import ClientError
def remove_tags_in_resource(resource_arn, tags_list)
session = boto3.session.Session()
glue_client = session.client('glue')
try:
response = glue_client.untag_resource(ResourceArn= resource_arn,TagsToRemove=tags_list)
return response
except ClientError as e:
raise Exception("boto3 client error in remove_tags_in_resource: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in remove_tags_in_resource: " + e.__str__())
tags_dict = ["glue-db"]
print(remove_tags_in_resource("arn:aws:glue:us-east-1:1122225*****88:database/test- db",tags_list))輸出
{'ResponseMetadata': {'RequestId': 'c9f418b0-***************-fb96', '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-******************-fb96'}, 'RetryAttempts': 0}}
廣告
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP