Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

ใช้ Boto3 ดึง Tags จาก AWS Glue Resources

ภาพรวม

สาธิตการดึงรายการ tags ที่ผูกกับทรัพยากร AWS Glue โดยใช้ get_tags ของ boto3

รูปแบบ Resource ARN

เหมือนกับการลบ tags (ดูตารางในบทความก่อนหน้า)

ตัวอย่างโค้ด

import boto3
from botocore.exceptions import ClientError

def get_tags(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(f"boto3 client error: {e}")
    except Exception as e:
        raise Exception(f"Unexpected error: {e}")

# ใช้งาน
arn = "arn:aws:glue:us-east-1:1122225*****88:database/test-db"
print(get_tags(arn))

ผลลัพธ์ตัวอย่าง

{
    'Tags': {'glue-job': 'test'},
    'ResponseMetadata': {
        'RequestId': 'c9f418b0-8d02-4a26-...',
        'HTTPStatusCode': 200,
        ...
    }
}

คำอธิบาย

  • ส่งคืน dictionary ที่ key คือ tag key และ value คือ tag value
  • ใช้ตรวจสอบ tags ที่มีอยู่ก่อนตัดสินใจเพิ่ม/ลบ
  • ต้องมี permission glue:GetTags