Computer >> 컴퓨터 >  >> 프로그램 작성 >> Python

Boto3를 사용하여 AWS Glue 리소스에서 태그를 가져오는 방법

<시간/>

이 기사에서는 사용자가 AWS Glue 리소스에 연결된 태그를 가져오는 방법을 살펴보겠습니다.

예시

"glue-db:테스트 태그 가져오기 " AWS 글루 데이터베이스에서.

문제 설명: Python에서 boto3 라이브러리를 사용하여 AWS Glue 리소스에서 태그를 가져옵니다.

이 문제를 해결하기 위한 접근 방식/알고리즘

  • 1단계: boto3 가져오기 및 보토코어 예외를 처리하는 예외.

  • 2단계: resource_arn 이 함수의 필수 매개변수입니다.

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:연결/연결 이름
크롤러 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단계: 글루용 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}}