본문 바로가기
Cloud/AWS

S3에 파일 업로드, 다운로드하는 public API 만들기

by 민휘 2024. 1. 17.

Upload API

 

참고 자료

 

버킷 생성

  • 이름, 리전 지정
  • 버저닝 허용
  • 암호화 허용
  • 다른 설정은 모두 default

 

Role 생성 및 연결

 

Role 생성

  • Trust : AWS Service
  • Use case : API Gateway
  • Permission : AmazonAPIGatewayPushToCloudWatchLogs
  • Role name : api-gateway-s3-role

 

Role에 Policy 생성 및 연결

  • 생성한 Role 콘솔 → Add Permissions → Create Inline Policy
  • Service : s3
  • Actions : PutObject
  • Resources : ARN 생성 - 버킷 이름, 모든 오브젝트(*) 지정
  • Policy Name : s3-putobject-policy

 

API Gateway 작업

 

API Gatway 생성

  • REST API 선택
  • new api
  • name : file-upload-api

 

루트 하위에 리소스 생성

  • resource name : bucket
  • resource path : {bucket}

 

bucket 하위에 리소스 생성

  • resource name : filename
  • resource path : {filename}

 

filename에 Method 생성

  • Method type : PUT
  • Integration type : AWS service
  • AWS Region : s3 생성한 곳과 같은 지역
  • AWS service : S3
  • HTTP Method : PUT
  • Action type : path override
  • path : {bucket}/{filename}
  • execution role : api-gateway-s3-role의 arn
  • default timeout 활성화

 

Integration Request

  • URL path parameter 추가
  • bucket : method.request.path.bucket
  • filename : method.request.path.filename

 

API Settings

  • Binary Media Types : / (이미지, 텍스트, pdf 등 대부분의 파일 형식 지원)

 

Deploy

  • new Stage : dev
  • invoke url 확인

 

Postman Test

  • method : PUT
  • API Gateway url/{bucket}/{filename}
  • Body에 binary로 업로드할 파일 선택
  • 테스트!
  • 참고) body에 json 데이터 올려도 업로드 잘 된다

 

Download API

 

참고 자료

 

Role 생성 및 연결

 

role 생성

  • entity type : AWS Service
  • use case : API Gateway
  • policy : AmazonAPIGatewayPushToCloudWatchLogs
  • name : api-gateway-s3-role-get

 

Role에 Policy 생성 및 연결

  • 생성한 Role 콘솔 → Add Permissions → Create Inline Policy
  • Service : s3
  • Actions : ReadObject
  • Resources : ARN 생성 - 버킷 이름, 모든 오브젝트(*) 지정
  • Policy Name : s3-readobject-policy

 

API Gateway 작업

 

filename에 Method 생성

  • Method type : GET
  • Integration type : AWS service
  • AWS Region : s3 생성한 곳과 같은 지역
  • AWS service : S3
  • HTTP Method : GET
  • Action type : path override
  • path : {bucket}/{filename}
  • execution role : api-gateway-s3-role-get의 arn
  • default timeout 활성화

 

Integration Request

  • URL path parameter 추가
  • bucket : method.request.path.bucket
  • filename : method.request.path.filename

 

API Settings (앞에서 했다면 생략)

  • Binary Media Types : / (이미지, 텍스트, pdf 등 대부분의 파일 형식 지원)

 

Deploy

  • new Stage : dev
  • invoke url 확인

 

Postman 테스트

  • method : GET
  • API Gateway url/{bucket}/{filename}
  • 테스트!