문제 설명 − Python에서 boto3 라이브러리를 사용하여 S3 버킷의 소유권 제어 세부 정보를 얻습니다.
예를 들어 S3에서 Bucket_1의 소유권 제어 세부 정보를 찾습니다.
이 문제를 해결하기 위한 접근 방식/알고리즘
1단계 − boto3 및 botocore 예외를 가져와 예외를 처리합니다.
2단계 − 함수의 매개변수로 bucket_name을 사용합니다.
3단계 − boto3 라이브러리를 사용하여 AWS 세션을 생성합니다.
4단계 − S3용 AWS 클라이언트를 생성합니다.
5단계 − 이제 get_bucket_ownership_controls 함수를 사용하여 버킷 이름을 전달합니다.
6단계 − S3에 대한 내용이 담긴 사전을 반환합니다.
7단계 − 파일을 삭제하는 동안 문제가 발생한 경우 일반 예외를 처리합니다.
예시
버킷의 소유권 세부 정보를 얻으려면 다음 코드를 사용하십시오 -
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'
},
]
}
}