본문 바로가기

IT&코딩/AWS

S3 bucket에 파일 업로드 테스트하기

728x90
반응형

1. requirements.txt에 boto3 추가 및 설치

 

boto3 == 1.34.34

pip3 install -r requirements.txt

 

2. Django에 아래와 같은 python 코드와 png 파일을 추가

 

import os
import calendar
import time
import boto3

data_file = "test.png"  # 업로드하려는 디렉터리 경로
filename = "test.png"
s3_path = ""
# s3 정보
ACCESS_KEY_ID = "키 ID"
ACCESS_SECRET_KEY = "시크릿 키"
BUCKET_NAME = "버킷 이름"

s3_client = boto3.client(
    "s3",
    aws_access_key_id=ACCESS_KEY_ID,
    aws_secret_access_key=ACCESS_SECRET_KEY,
)

with open(data_file, 'rb') as file:
    s3_client.upload_fileobj(
        file,
        BUCKET_NAME,
        s3_path + filename,
        ExtraArgs={
            "ContentType": "image/png"  # 텍스트 파일의 경우 text/plain으로 설정
            # "ACL": "public-read",
        },
    )

 

3. 해당 파이썬 코드를 실행 시킨 후 성공적으로 완료되면 url에서 확인 가능

 

https://{버킷 URL}/test.png

728x90
반응형