Python SDK

Prev Next

Classic/VPC 환경에서 이용 가능합니다.

OpenStack에서 제공하는 Python SDK를 이용하여 네이버 클라우드 플랫폼의 Archive Storage를 사용하는 예제입니다. 이 가이드에서는 python-swiftclient 3.6.0, python-keystoneclient 3.17.0 버전을 기준으로 안내합니다.

설치

SDK를 설치하는 방법은 다음과 같습니다.

pip install python-swiftclient==3.6.0
pip install python-keystoneclient==3.17.0
참고

python-swiftclient 3.6.0 버전과 python-keystoneclient 3.17.0 버전의 소스 정보 및 관련 문서는 다음과 같습니다.

인증

이 가이드의 모든 예제는 swiftclient.Connection 객체를 사용합니다.
swiftclient.Connection 객체는 인증 정보를 포함한 keystoneauthSession 오브젝트를 기반으로 생성됩니다. 인증 재시도 및 토큰 관리 기능을 제공하여 오퍼레이션을 보다 간편하게 실행할 수 있습니다.
한 번 발급된 토큰은 재사용되어 불필요한 인증 요청을 줄이며, 토큰이 만료되거나 유효하지 않은 경우에는 자동으로 재발급을 요청해 갱신합니다.
기본으로 설정된 인증 재시도 횟수는 5회입니다.

참고

예제에 사용된 ACCESS_KEY_ID, SECRET_KEY, DOMAIN_ID, PROJECT_ID는 네이버 클라우드 플랫폼에서 발급받은 API 인증키와 인증 정보 값입니다.
해당 값을 확인하는 방법은 서비스 연동을 참조해 주십시오.

import swiftclient
from keystoneauth1 import session
from keystoneauth1.identity import v3

endpoint = 'https://kr.archive.gov-ncloudstorage.com:5000/v3'
username = 'ACCESS_KEY_ID'
password = 'SECRET_KEY'
domain_id = 'DOMAIN_ID'
project_id = 'PROJECT_ID'

auth = v3.Password(auth_url=endpoint, username=username, password=password, project_id=project_id, user_domain_id=domain_id)
auth_session = session.Session(auth=auth)

swift_connection = swiftclient.Connection(retries=5, session=auth_session)

예제

Python SDK를 이용하여 Archive Storage를 사용하는 방법을 설명합니다.

컨테이너(버킷) 목록 조회

컨테이너(버킷) 목록을 조회하는 예시는 다음과 같습니다.

account = swift_connection.get_account()
for container in account[1]:
    pprint.pprint(container)

컨테이너(버킷) 생성

컨테이너(버킷)를 생성하는 예시는 다음과 같습니다.

container_name = 'sample-container'

swift_connection.put_container(container_name)

컨테이너(버킷) 삭제

컨테이너(버킷)를 삭제하는 예시는 다음과 같습니다.

container_name = 'sample-container'

swift_connection.delete_container(container_name)

오브젝트 업로드

오브젝트를 업로드하는 예시는 다음과 같습니다.

container_name = 'sample-container'
object_name = 'sample-object'

content_type = 'text/plain'

local_file_name = '/tmp/test.txt'

with open(local_file_name) as local_file:
    swift_connection.put_object(container_name, object_name,
                                contents=local_file.read(),
                                content_type=content_type)

디렉토리 오브젝트 생성

디렉터리 오브젝트를 생성하는 예시는 다음과 같습니다.

container_name = 'sample-container'
object_name = 'sample-directory'

content_type = 'application/directory'

swift_connection.put_object(container_name, object_name,
                            contents='',  # empty content
                            content_type=content_type)

오브젝트 목록 조회

오브젝트 목록을 조회하는 예시는 다음과 같습니다.

container_name = 'sample-container'

container = swift_connection.get_container(container_name)
for obj in container[1]:
    pprint.pprint(obj)

오브젝트 다운로드

오브젝트를 다운로드하는 예시는 다음과 같습니다.

container_name = 'sample-container'
object_name = 'sample-object'

local_file_name = '/tmp/test'

obj = swift_connection.get_object(container_name, object_name)
with open(local_file_name, 'wb+') as local_file:
    local_file.write(obj[1])

오브젝트 삭제

오브젝트를 삭제하는 예시는 다음과 같습니다.

container_name = 'sample-container'
object_name = 'sample-object'

swift_connection.delete_object(container_name, object_name)

대용량 파일 업로드(SLO)

SLO 방식을 사용하여 대용량 파일을 업로드하는 예시는 다음과 같습니다.

container_name = 'sample-container'
object_name = 'sample-big-file.mp4'
content_type = 'video/mp4'

segment_container_name = 'segment-container'
segment_object_prefix = 'segments-for-sample-big-file.mp4/segment-'

local_file_path = '/tmp/sample.mp4'
segment_size = 10 * 1024 * 1024  # 10MB
manifest = []

with open(local_file_path, 'rb') as local_file:
    segment_number = 1
    while True:
        data = local_file.read(segment_size)
        if not len(data):
            break

        segment_object_name = '%s%d' % (segment_object_prefix, segment_number)
        etag = swift_connection.put_object(segment_container_name, segment_object_name, contents=data)
        manifest.append({
            "path": '%s/%s' % (segment_container_name, segment_object_name),
            "etag": etag
        })
        segment_number += 1

body = json.dumps(manifest)

swift_connection.put_object(container_name, object_name,
                            contents=body,
                            content_type=content_type,
                            query_string='multipart-manifest=put')

대용량 파일 세그먼트 조회(SLO)

대용량 파일의 세그먼트 목록을 조회하는 예시는 다음과 같습니다.

container_name = 'sample-container'
object_name = 'sample-big-file.mp4'

obj = swift_connection.get_object(container_name, object_name, query_string='multipart-manifest=get')
pprint.pprint(json.loads(obj[1]))

대용량 파일 삭제(SLO)

대용량 파일을 삭제하는 예시는 다음과 같습니다.

container_name = 'sample-container'
object_name = 'sample-big-file.mp4'

swift_connection.delete_object(container_name, object_name, query_string='multipart-manifest=delete')