Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Thêm tags vào tài nguyên AWS Glue với Boto3

Minh họa cách gắn tags (key-value) cho tài nguyên AWS Glue sử dụng boto3.

Quy trình

  1. Import boto3, ClientError.
  2. Hàm nhận resource_arn và tags_dict (dictionary key-value).
  3. Tạo session, client Glue.
  4. Gọi glue_client.tag_resource(ResourceArn=..., TagsToAdd=...).
  5. Trả về response metadata.
  6. Xử lý ngoại lệ.

Code mẫu

import boto3
from botocore.exceptions import ClientError

def them_tags(resource_arn, tags_dict):
    session = boto3.session.Session()
    glue_client = session.client('glue')
    try:
        response = glue_client.tag_resource(ResourceArn=resource_arn, TagsToAdd=tags_dict)
        return response
    except ClientError as e:
        raise Exception("Lỗi boto3 client: " + str(e))
    except Exception as e:
        raise Exception("Lỗi không mong đợi: " + str(e))

tags_dict = {"glue-db": "test"}
print(them_tags("arn:aws:glue:us-east-1:1122225*****88:database/test-db", tags_dict))

Kết quả

{'ResponseMetadata': {'RequestId': 'c9f418b0-...', 'HTTPStatusCode': 200, ...}}