리셋 되지 말자

[nCloud] boto3 - Object Storage에 Image Upload 본문

프로젝트

[nCloud] boto3 - Object Storage에 Image Upload

kyeongjun-dev 2021. 11. 22. 22:08

Python 설치 및 boto3 설치

- 가상환경 확인

(venv) [vagrant@servera boto-test]$ python --version
Python 3.8.8

 

- pip upgrade

(venv) [vagrant@servera boto-test]$ pip install --upgrade pip

 

- boto3 설치

(venv) [vagrant@servera boto-test]$ pip install boto3

 

환경변수 설정

export ACCESS_KEY='ACCESSKEY'
export SECRET_KEY='SECRETKEY'

 

 

이미지 파일 준비

- wget 으로 이미지 파일 다운로드

(venv) [vagrant@servera boto-test]$ wget https://github.com/emsysTeam/ClothesSeparationAPI/raw/main/ClothesSeparationAPI/input.PNG

 

boto3 스크립트 작성

- 버킷 목록 조회 : get_bucket_list.py

import boto3
import os

service_name = 's3'
endpoint_url = 'https://kr.object.ncloudstorage.com'
region_name = 'kr-standard'
access_key = os.environ['ACCESS_KEY']
secret_key = os.environ['SECRET_KEY']

if __name__ == "__main__":
    s3 = boto3.client(service_name, endpoint_url=endpoint_url, aws_access_key_id=access_key,
                      aws_secret_access_key=secret_key)

    response = s3.list_buckets()

    for bucket in response.get('Buckets', []):
        print(bucket.get('Name'))

 

- 모든 오브젝트 목록 조회 : get_objects_list.py

import boto3
import os

service_name = 's3'
endpoint_url = 'https://kr.object.ncloudstorage.com'
region_name = 'kr-standard'
access_key = os.environ['ACCESS_KEY']
secret_key = os.environ['SECRET_KEY']

if __name__ == "__main__":
    s3 = boto3.client(service_name, endpoint_url=endpoint_url, aws_access_key_id=access_key,
                      aws_secret_access_key=secret_key)

    bucket_name = 'csapi'
    max_keys = 300
    response = s3.list_objects(Bucket=bucket_name, MaxKeys=max_keys)

    while True:
        print('IsTruncated=%r' % response.get('IsTruncated'))
        print('Marker=%s' % response.get('Marker'))
        print('NextMarker=%s' % response.get('NextMarker'))

        print('Object List')
        for content in response.get('Contents'):
            print(' Name=%s, Size=%d, Owner=%s' % \
                  (content.get('Key'), content.get('Size'), content.get('Owner').get('ID')))

        if response.get('IsTruncated'):
            response = s3.list_objects(Bucket=bucket_name, MaxKeys=max_keys,
                                       Marker=response.get('NextMarker'))
        else:
            break

 

- 특정 경로의 오브젝트 조회 : get_objects_list.py 에 Prefix 추가

import boto3
import os

service_name = 's3'
endpoint_url = 'https://kr.object.ncloudstorage.com'
region_name = 'kr-standard'
access_key = os.environ['ACCESS_KEY']
secret_key = os.environ['SECRET_KEY']

if __name__ == "__main__":
    s3 = boto3.client(service_name, endpoint_url=endpoint_url, aws_access_key_id=access_key,
                      aws_secret_access_key=secret_key)

    bucket_name = 'csapi'
    max_keys = 300
    response = s3.list_objects(Bucket=bucket_name, Prefix="", MaxKeys=max_keys)

    while True:
        print('IsTruncated=%r' % response.get('IsTruncated'))
        print('Marker=%s' % response.get('Marker'))
        print('NextMarker=%s' % response.get('NextMarker'))

        print('Object List')
        for content in response.get('Contents'):
            print(' Name=%s, Size=%d, Owner=%s' % \
                  (content.get('Key'), content.get('Size'), content.get('Owner').get('ID')))

        if response.get('IsTruncated'):
            response = s3.list_objects(Bucket=bucket_name, MaxKeys=max_keys,
                                       Marker=response.get('NextMarker'))
        else:
            break

 

- 이미지 업로드 : upload_object.py

import boto3
import os

service_name = 's3'
endpoint_url = 'https://kr.object.ncloudstorage.com'
region_name = 'kr-standard'
access_key = os.environ['ACCESS_KEY']
secret_key = os.environ['SECRET_KEY']

if __name__ == "__main__":
    s3 = boto3.client(service_name, endpoint_url=endpoint_url, aws_access_key_id=access_key,
                      aws_secret_access_key=secret_key)

    bucket_name = 'csapi'
    object_name = 'images/origin-images/input.PNG'
    local_file_path = '/home/vagrant/boto-test/input.PNG'
    s3.upload_file(local_file_path, bucket_name, object_name)

 

고찰

aws의 s3나 ncloud의 object storage 같은 객체 스토리지는 폴더건 파일이건 다 '객체' 취급한다. 이를 명심하자.

 

Comments