본문 바로가기

AWS

[AWS] 3. Amazon SDK

Amazon SDK

 

SDK를 이용하여 개발환경에 연동하여 AWS를 사용할 수 있다.

 

AWS SDK 설정 및 S3와 연동

 

aws --version # aws-cli/2.2.43 Python/3.8.8 Windows/10 exe/AMD64 prompt/off

aws configure
AWS Access Key ID [None]: <액세스 키 ID>
AWS Secret Access Key [None]: <비밀 액세스 키>
Default region name [None]: ap-northeast-2
Default output format [None]: json

# 업로드
aws s3 cp {파일명} s3://{버킷이름} --acl public-read

# ex 파일이 있는 폴더로 이동 후) 
aws s3 cp diff.png s3://lukaidsparta --acl public-read

 

근데 사실 이렇게는 잘 안쓴다고 한다. AWS 서비스의 모든 제품들은 SDK 라이브러리를 거의 모든 언어에 제공하기 때문에 개발언어와 연동해서 사용한다. 파이썬에서는 boto3라는 라이브러리를 제공하고 있다.

 

파이썬과 연동하여 사용하기

 

 

Boto3를 사용하면 Python 애플리케이션, 라이브러리 또는 스크립트를 Amazon S3, Amazon EC2, Amazon DynamoDB 등 AWS 서비스와 쉽게 통합할 수 있다.

 

다음 예시와 같이 boto3를 통해 flask서버와 S3를 연동할 수 있다.

import boto3

@app.route('/fileupload', methods=['POST'])
def file_upload(): # 파일 업로드 API
    file = request.files['file'] # 프론트에서 업로드 되는 파일의 정보
    s3 = boto3.client('s3') # s3와 연동
    s3.put_object(
        ACL="public-read", # 권한
        Bucket="lukaidsparta", # 내 버킷 이름
        Body=file, # 업로드 할 파일
        Key=file.filename, # 업로드 할 파일 이름
        ContentType=file.content_type) # 업로드 할 파일 형식
    return jsonify({'result': 'success'})

'AWS' 카테고리의 다른 글

[AWS] 5. Github Actions를 통한 배포_front  (0) 2021.10.11
[AWS] 4. Amazon CloudFront  (0) 2021.10.11
[AWS] 2. AWS S3  (0) 2021.10.11
[AWS] 1. AWS Identity and Access Management (IAM)  (0) 2021.10.11
[AWS] 0. 사용법 정리  (0) 2021.10.11