- 印刷する
- PDF
Python SDK
- 印刷する
- PDF
Classic/VPC環境で利用できます。
OpenStackで提供する Python SDKを利用して NAVERクラウドプラットフォームの Archive Storageを使用する例です。このガイドでは python-swiftclient 3.6.0, python-keystoneclient 3.17.0バージョンを基準に説明します。
SDKのインストール
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バージョンのソース情報および関連ドキュメントは以下のとおりです。
認証
認証情報を含む keystoneauth Sessionオブジェクトを利用し、swiftclient Connectionオブジェクトを作成します。swiftclient Connectionオブジェクトは認証の再試行機能とトークン管理機能を提供し、オペレーションを簡単に使用できます。一度発行されたトークンを再利用するため、不要な認証リクエストを減らすことができ、トークンが無効または満了した場合でもトークンの再発行をリクエストして自動的に更新します。認証の再試行デフォルト設定値は5回です。
このガイドで説明するすべての例は、swiftclient Connectionを利用します。
例で使用する username
、 password
に適用する値は NAVERクラウドプラットフォームポータルで作成した API認証キー情報を使用します。API認証キー情報で Access Key IDを username
の値に入力して Secret Keyを password
の値に入力します。domain_id
、project_id
は Archive Storage画面の [API利用情報の確認] ボタンをクリックし、表示されるポップアップウィンドウの情報を使用します。Domain IDを domain_id
の値に入力して Project IDの値を project_id
の値に入力します。
- API認証キー情報を確認する方法は API認証キー作成および確認をご参照ください。
- project_idと domain_id情報を確認する方法は API利用情報の確認をご確認ください。
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)
例
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')
大容量ファイル Segmentsの照会(SLO)
大容量ファイル Segmentsを照会する例は以下のとおりです。
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')