Python SDK for Swift API
OpenStackで提供するPython SDKを利用して、NAVERクラウドプラットフォームのArchive Storageを使用する方法を説明します。
このテキストは、python-swiftclient 3.6.0, python-keystoneclient 3.17.0 バージョンに基づいて作成されました。
インストール
pip install python-swiftclient==3.6.0
pip install python-keystoneclient==3.17.0
python-swiftclient
- ソース : https://github.com/openstack/python-swiftclient
- テキスト : https://pypi.org/project/python-swiftclient/3.6.0/
python-keystoneclient
- ソース : https://github.com/openstack/python-keystoneclient
- テキスト : https://pypi.org/project/python-keystoneclient/3.17.0/
例
例で使用される username, passwordはポータルマイページ > アカウント管理 > 認証キー管理で作成したAPI認証キーを使用します。(Access Key IDはusername, Secret Keyはpassword) project_idとdomain_idの情報は、Archive Storageコンソールから利用を申込んで、 [API利用情報の確認]をクリックすることで確認できまます。
認証
認証情報を含むkeystoneauth Sessionオブジェクトを用いて、swiftclient Connectionのオブジェクトを生成します。 swiftclient Connectionのオブジェクトは、リトライ機能とトークンの管理機能を提供してオペレーションを簡単に使用することができます。 一度発行されたトークンを再利用することは、不要な認証のリクエストをしなくてもいいということです。また、トークンが有効でない場合や有効期限が切れた場合には、トークンの再発行要求を通じて更新します。 デフォルトの再試行の設定は5回までです。
import swiftclient
from keystoneauth1 import session
from keystoneauth1.identity import v3
endpoint = 'https://kr.archive.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)
すべてのオペレーションの例は、上記で作成したswiftclient Connectionを利用します。
コンテナ(バケット)リストの照会
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)
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')
大容量ファイルSegmentsの照会(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')