AWS의 라이브러리를 사용하여 S3에 파일 업로드를 도와준다.
# boto3 라이브러리 설치
pip install boto3
S3에 저장될 파일 이름 설정
# 클라이언트로부터 파일을 받아온다.
file = request.files['photo']
# 파일명을 우리가 변경해 준다.
# 파일명은 유니크하게 만들어야 한다.
current_time = datetime.now()
new_file_name = current_time.isoformat().replace(':','_') + '.jpg'
# 유저가 올린 파일의 이름을 내가 만든 파일명으로 변경
file.filename = new_file_name
S3에 파일 업로드
# s3에 접근하기 위한 키 설정
s3 = boto3.client('s3', aws_access_key_id = Config.ACCESS_KEY, aws_secret_access_key = Config.SECRET_ACCESS)
# s3에 파일 업로드 시,
# 파일 이름, 접근 제어 목록, 확장자 타입 설정
s3.upload_fileobj(file, Config.S3_BUCKET, file.filename,
ExtraArgs = {'ACL' : 'public-read', 'ContentType' : file.content_type})
저장된 이미지 확인