개요
이 글에서는 Python의 boto3 라이브러리를 사용하여 S3 버킷의 소유권 제어(Ownership Controls) 세부 정보를 조회하는 방법을 알아봅니다.
예를 들어, S3에 있는 Bucket_1 버킷의 소유권 제어 설정을 확인하고 싶다고 가정해 보겠습니다.
문제 해결 접근 방식 및 알고리즘
1단계 – 예외 처리를 위해 boto3와 botocore.exceptions를 임포트합니다.
2단계 – 함수의 매개변수로 bucket_name(버킷 이름)을 사용합니다.
3단계 – boto3 라이브러리를 이용해 AWS 세션(Session)을 생성합니다.
4단계 – S3 서비스를 위한 AWS 클라이언트를 생성합니다.
5단계 – get_bucket_ownership_controls 함수를 호출하면서 버킷 이름을 전달합니다.
6단계 – 해당 함수는 S3 버킷의 소유권 제어 세부 정보가 담긴 딕셔너리를 반환합니다.
7단계 – 조회 과정에서 오류가 발생할 경우, ClientError를 비롯한 일반 예외를 처리하여 적절한 에러 메시지를 출력합니다.
예제 코드
다음 코드를 사용하면 특정 버킷의 소유권 제어 정보를 조회할 수 있습니다.
import boto3
from botocore.exceptions import ClientError
def get_bucket_ownership_control_of_s3(bucket_name):
session = boto3.session.Session()
s3_client = session.client('s3')
try:
result = s3_client.get_bucket_ownership_controls(Bucket=bucket_name,)
except ClientError as e:
raise Exception("boto3 client error in get_bucket_ownership_control_of_s3: " + e.__str__())
except Exception as e:
raise Exception("Unexpected error in get_bucket_ownership_control_of_s3: " + e.__str__())
return result
print(get_bucket_ownership_control_of_s3("Bucket_1"))실행 결과
{
'OwnershipControls': {
'Rules': [
{
'ObjectOwnership': 'BucketOwnerPreferred'|'ObjectWriter'
},
]
}
}참고 사항
ObjectOwnership 값은 크게 두 가지입니다. BucketOwnerPreferred는 버킷 소유자가 객체 소유권을 우선적으로 가지도록 설정하는 것이고, ObjectWriter는 객체를 업로드한 계정이 소유권을 가지도록 설정하는 것입니다. 또한, 대상 버킷에 소유권 제어 규칙이 아직 설정되어 있지 않으면 OwnershipControlsNotFoundError 오류가 발생할 수 있으므로, 필요 시 먼저 put_bucket_ownership_controls로 규칙을 설정해 두는 것이 좋습니다.