Python SDK

Prev Next

Available in Classic and VPC.

This is an example of using NAVER Cloud Platform’s Archive Storage using Python SDK provided by OpenStack. This guide is based on python-swiftclient 3.6.0 and python-keystoneclient 3.17.0 versions.

Install SDK

The following describes how to install an SDK.

pip install python-swiftclient==3.6.0
pip install python-keystoneclient==3.17.0
Note

The source information and related documents for python-swiftclient 3.6.0 and python-keystoneclient 3.17.0 are as follows:

Authenticate

Create swiftclient Connection object by using keystoneauth Session object containing authentication information. The swiftclient Connection object provides authentication retry capabilities and token management capabilities to facilitate consuming operations. By reusing tokens issued once, unnecessary authentication requests can be reduced, and even when tokens are invalid or expired, they are automatically renewed by requesting a token reissuance. The default setting for authentication retries is 5 times.

Note

All examples described in this guide use swiftclient Connection.

The values​to be applied to username, password, in the example use the API authentication key information created on the NAVER Cloud Platform portal. In the API authentication key information, enter the Access Key ID for the value of username and the Secret Key for the value of password. domain_id, project_id use the information in the pop-up window that appears when you click the [Check API usage information] button on the Archive Storage screen. Enter the domain ID for the value of domain_id and the project ID value for the value of project_id.

Note
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)

Example

This example shows how to use Archive Storage using the Python SDK.

View container (bucket) list

Here's an example of how you can view a list of containers (buckets).

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

Create a container (bucket)

Here's an example of how you can create a container (bucket).

container_name = 'sample-container'

swift_connection.put_container(container_name)

Delete container (bucket)

Here's an example of how you can delete a container (bucket).

container_name = 'sample-container'

swift_connection.delete_container(container_name)

Upload object

Here's an example of how you can upload an object.

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)

Create directory object

Here's an example of how you can create a directory object.

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)

View object list

Here's an example of how you can view the object list.

container_name = 'sample-container'

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

Download object

Here's an example of how you can download an object.

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])

Delete object

Here's an example of how you can delete an object.

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

swift_connection.delete_object(container_name, object_name)

Uploading a large file(SLO)

Here's an example of how you can upload an object using the SLO method.

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')

View large file segments (SLO)

Here's an example of how you can view large file 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]))

Delete large file (SLO)

Here's an example of how you can delete large files.

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

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