[AWS] boto3에서 잘못된 S3 region을 넘겨도 동작하는 이유

오도원공육사·2023년 1월 17일
0

AWS

목록 보기
1/1

본인은 서울리전(ap-northeast-2)의 s3 bucket을 사용한다.

session = boto3.Session(region_name='us-west-2')

s3 = session.resource('s3')

bucket_name = 'BUCKET_NAME'

try:
    s3.meta.client.head_bucket(Bucket = bucket_name)
except ClientError as c:
    print(c)

하지만 코드 상에서 수정되기 전 오레곤 리전인 us-west-2로 잘못된 리전 정보를 보내도 예외를 발생시키지 않는다. 이를 stackoverflow의 동일한 질문과 답변을 보고 따라서 awscli 디버깅을 통해서 원인을 분석해봤다.

> aws s3api head-bucket --bucket BUCKET_NAME --region us-west-2 --debug
DEBUG - S3 client configured for region us-west-2 but the bucket mybucket is in region ap-northeast-2; Please configure the proper region to avoid multiple unnecessary redirects and signing attempts.
DEBUG - Switching signature version for service s3 to version s3v4 based on config file override.
DEBUG - Updating URI from https://s3.us-west-2.amazonaws.com/BUCKET_NAME to https://s3.ap-northeast-2.amazonaws.com/mybucket

내부에서 리다이렉션이 발생된 것을 알 수 있다. 모든 리전에 대해서 버킷이름은 고유하기 때문에 자체 리디렉션이 가능한 것 같다. 하지만 불필요한 리디렉션과 인증절차를 피하기 위해 올바른 리전 정보를 전송하는 것이 좋다.

참고.
https://stackoverflow.com/questions/64850820/python-boto3-checking-for-valid-bucket-within-region

profile
잘 먹고 잘살기

0개의 댓글