AWS S3, MINIO를 위한 python boto3 사용법

Hyungeun Lee·2024년 11월 17일

boto3란?

boto3는 AWS의 공식 Python SDK로, S3를 포함한 모든 AWS 서비스와 상호작용할 수 있게 해주는 라이브러리입니다. MinIO는 S3 호환 API를 제공하므로, 동일한 boto3 코드에 약간의 수정을 거치게 되면 MinIO 또한 사용할 수 있습니다.

설치

pip install boto3

Object Storage 클라이언트 생성

import boto3

s3_client = boto3.client(
    's3',
    aws_access_key_id='YOUR_ACCESS_KEY',
    aws_secret_access_key='YOUR_SECRET_KEY',
    region_name='YOUR_S3_OBJECT_STORAGE_REGION'
) # endpoint_url: Optional[str] = 'http://localhost:9000'

버킷 생성

def create_bucket(bucket_name):
    try:
        s3_client.create_bucket(
            Bucket=bucket_name,
            CreateBucketConfiguration={
                'LocationConstraint': 'ap-northeast-2'
            }
        )
        print(f"버킷 생성 완료: {bucket_name}")
    except Exception as e:
        print(f"버킷 생성 실패: {str(e)}")

파일 업로드

def upload_file(file_path, bucket_name, object_name):
    try:
        s3_client.upload_file(
            file_path,
            bucket_name,
            object_name
        )
        print(f"파일 업로드 완료: {object_name}")
    except Exception as e:
        print(f"파일 업로드 실패: {str(e)}")

파일 다운로드

def download_file(bucket_name, object_name, file_path):
    try:
        s3_client.download_file(
            bucket_name,
            object_name,
            file_path
        )
        print(f"파일 다운로드 완료: {file_path}")
    except Exception as e:
        print(f"파일 다운로드 실패: {str(e)}")

마치며

이번 블로그 포스트에서는 boto3 python sdk를 활용하여 object storage에 버킷을 생성하고 파일을 업로드 및 다운로드하는 방법에 대해서 간략하게 살펴보았습니다.

추가 학습 자료

s3 docs
boto3 docs
minio docs

0개의 댓글