boto3는 AWS의 공식 Python SDK로, S3를 포함한 모든 AWS 서비스와 상호작용할 수 있게 해주는 라이브러리입니다. MinIO는 S3 호환 API를 제공하므로, 동일한 boto3 코드에 약간의 수정을 거치게 되면 MinIO 또한 사용할 수 있습니다.
pip install boto3
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에 버킷을 생성하고 파일을 업로드 및 다운로드하는 방법에 대해서 간략하게 살펴보았습니다.