오브젝트스토리지

iwinv Object Storage - Python SDK

해당 메뉴얼은 AWS S3에서 제공하는 Python SDK를 이용하여 iwinv Object Storage를 사용하는 방법을 설명합니다.

 

참고문서

  https://github.com/boto/boto3 

  https://boto3.readthedocs.io/en/latest/reference/services/s3.html

 

 

 Python SDK 사용하기


 
테스트 OS : Ubuntu 18.04 LTS

 

[인증키 확인]

Endpoint -> 오브젝트 스토리지 -> 스토리지 관리 -> 인증키 관리 -> API {IDC} Endpoint 

## Endpoint 입력시에는  http://kr.object.iwinv.kr  혹은 https://kr.object.iwinv.kr  과 같은 형식으로 입력해주어야 코드가 정상 실행됩니다.

Access_Key  : console.iwinv.kr -> 오브젝트 스토리지 -> 스토리지 관리 -> 인증키 관리 -> Access Key ID

Secret_Key   : console.iwinv.kr -> 오브젝트 스토리지 -> 스토리지 관리 -> 인증키 관리 -> Secret Key ID

 

 

 

AWS Python SDK 설치하기  

 pip install boto3

 

 

# 예제코드 


 

버킷 생성

import boto3

service_name = 's3'
endpoint_url = 'Endpoint'
region_name = 'kr-standard'
access_key = 'Access_Key'
secret_key = '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 = 'Buket_Name'

    s3.create_bucket(Bucket=bucket_name)

 

 

버킷 삭제

import boto3

service_name = 's3'
endpoint_url = 'Endpoint'
region_name = 'kr-standard'
access_key = 'Access_Key'
secret_key = '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 = 'Buket_Name'

s3.delete_bucket(Bucket=bucket_name)

 


버킷 리스트 확인

import boto3

service_name = 's3'
endpoint_url = 'Endpoint'
region_name = 'kr-standard'
access_key = 'Access_Key'
secret_key = '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')

  

 

오브젝트 업로드

# 폴더 생성

import boto3


service_name = 's3'
endpoint_url = 'Endpoint'
region_name = 'kr-standard'
access_key = 'Access_Key'
secret_key = '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 = 'Buket_Name'

    folder_name = 'Folder_Name/'

    s3.put_object(Bucket=bucket_name, Key=folder_name)


# 오브젝트 업로드

import boto3

service_name = 's3'
endpoint_url = 'Endpoint'
region_name = 'kr-standard'
access_key = 'Access_Key'
secret_key = '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 = 'Buket_Name'

    object_name = 'Object_Name'
    local_file_path = 'File_Path/File_Name'  # ex) home/ubuntu/test.txt

    s3.upload_file(local_file_path, bucket_name, object_name)
 

 

 

오브젝트 다운로드

import boto3

service_name = 's3'
endpoint_url = 'Endpoint'
region_name = 'kr-standard'
access_key = 'Access_Key'
secret_key = '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 = 'Buket_Name'

    object_name = 'Object_Name'
    local_file_path = 'File_Path/File_Name'  # ex) home/ubuntu/test.txt
 s3.download_file(bucket_name, object_name, local_file_path)

 

 

오브젝트 리스트 확인 

import boto3


service_name = 's3'
endpoint_url = 'Endpoint'
region_name = 'kr-standard'
access_key = 'Access_Key'
secret_key = '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 = 'Buket_Name'

    # list all in the bucket
    max_keys = 300
    response = s3.list_objects(Bucket=bucket_name, MaxKeys=max_keys)

    print('list all in the bucket')

    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

    # top level folders and files in the bucket
    delimiter = '/'
    max_keys = 300

    response = s3.list_objects(Bucket=bucket_name, Delimiter=delimiter, MaxKeys=max_keys)

    print('top level folders and files in the bucket')

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

        print('Folder List')
        for folder in response.get('CommonPrefixes'):
            print(' Name=%s' % folder.get('Prefix'))

        print('File 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, Delimiter=delimiter, MaxKeys=max_keys,
                                       Marker=response.get('NextMarker'))
        else:
            break

 

 

오브젝트 삭제

import boto3


service_name = 's3'
endpoint_url = 'Endpoint'
region_name = 'kr-standard'
access_key = 'Access_Key'
secret_key = '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 = 'Buket_Name'
    object_name = 'Object_Name'

    s3.delete_object(Bucket=bucket_name, Key=object_name)