S3 presigned URL SHA Issue

Barney Park·2023년 3월 29일

AWS

목록 보기
1/4

항상 에러

S3의 presigned URL을 적용하고 난 뒤, return 받은 URL로 접속을 하면 아래와 같이 엑세스가 거부되면서, AWS4-HMAC-SHA256 을 사용하라는 메세지가 나타났다.

This XML file does not appear to have any style information associated with it. The document tree is shown below.  
<Error>  
	<Code>InvalidRequest</Code>  
	<Message>  
		The authorization mechanism you have provided is not supported. Please
		use AWS4-HMAC-SHA256.  
	</Message>  
	<RequestId>W0304WYHZV121T2D</RequestId>  
	<HostId>
	E3VyxbADDE123wO0GGK8eGF0YgEcVZ9d3wpjWivNlTi2AOYvXjX6thXuonyx2bYSBO8ic+4R+ls=  
	</HostId>  
</Error>

AWS에서 정한 정책으로 2014년 2월 이후 생성된 리전은 4버전의 인증을 이용해야한다는 뜻이었는데, 콘솔환경도 아니었고, boto3 를 이용해서 S3을 이용하고 있어서 난감했다.

다행히도 공식문서에서 boto3 의 config설정을 통해 해당 문제를 해결할 수 있다고 나와있었다. 참고해서 해결한 코드는 아래와 같다.

def s3_get_client():
	# 아래 컨피그 부분 추가
	my_config = Config(
			region_name = 'ap-northeast-2',
			signature_version = 'v4',
			retries = {
				'max_attempts': 10,
				'mode': 'standard'
			}
	)
	
	client = boto3.client(
			's3',
			aws_access_key_id=ACCESS_KEY,
			aws_secret_access_key=SECRET_KEY,
			config=my_config
	)
	
	return client

def generate_presigned_url(
		s3_client,
		client_method,
		method_parameters,
		expires_in):
	try:
		url = s3_client.generate_presigned_url(
				ClientMethod=client_method,
				Params=method_parameters,
				ExpiresIn=expires_in
			)
	except ClientError:
		raise
		
	return url

def real_work_function():
	url_list = []

	s3_client = s3_get_client()
	
	client_action = 'get_object'
	
	for i in result:
		url = generate_presigned_url(
				s3_client,
				client_action,
				{
					'Bucket':RESOURCE_BUCKET_NAME,
					'Key': f{i['resource_key']}"
				},
				60
			)

		url_list.append(url)

	return url_list

참고문서

참고한 블로그
스택오버플로
AWS질문
presigned URL 예시
boto3 docs

profile
개선합시다

0개의 댓글