[PYTHON] Easy usage memo of OpenStack Swift API using swiftclient

Authentication using Session

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

username = 'admin'
tenant_name = 'admin'
password = 'password'
auth_url='http://192.x.x.x/identity/v3'

auth = v3.Password(auth_url=auth_url,
    username=username,
    password=password,
    project_name=tenant_name,
    user_domain_id='default',
    project_domain_id='default')

sess = session.Session(auth=auth)

swift_conn = client.Connection(session=sess)

Get container

I tried to write exception handling, but ... about a memo

try:
    resp_headers, containers = swift_conn.get_account()

except session.exceptions.Unauthorized:
    print "The request you have made requires authentication."

except client.ClientException:
    print "Account GET failed"

print(containers)

Method information displayed in bpython.

swift_conn.get_account: (self, marker=None, limit=None, prefix=None,      
end_marker=None, full_listing=False, headers=None)                        
get_account                                                               
Wrapper for :func:`get_account`                                           

Creating a container

container = 'new-container'
swift_conn.put_container(container)

Method information displayed in bpython. container is required.


swift_conn.put_container: (self, container, headers=None,                
response_dict=None, query_string=None)                                   
put_container                                                            
Wrapper for :func:`put_container`                                        

Upload to container

container = 'new-container'
with open('local.txt', 'r') as local:
    swift_conn.put_object(
        container=container,
        obj='local_object.txt',
        contents=local,
        content_type='text/plain'
    )

Method information displayed in bpython. container, obj, contents are required.


swift_conn.put_object: (self, container, obj, contents,                  
content_length=None, etag=None, chunk_size=None, content_type=None,      
headers=None, query_string=None, response_dict=None)                     
put_object                                                               
Wrapper for :func:`put_object`                                           

Download from container

obj = 'local_object.txt
container = 'new-container'
resp_headers, obj_contents = swift_conn.get_object(container, obj)
with open('local_copy.txt','w') as local:
    local.write(obj_contents)

Method information displayed in bpython. container, obj is required.


swift_conn.get_object: (self, container, obj, resp_chunk_size=None,      
query_string=None, response_dict=None, headers=None)                     
get_object                                                               
Wrapper for :func:`get_object`                                           

Delete object from container

obj = 'local_object.txt'
container = 'new-container'
try:
    swift_conn.delete_object(container, obj)
    print("Successfully deleted the object")
except ClientException as e:
    print("Failed to delete the object with error: %s" % e)

Method information displayed in bpython. container, obj is required.


swift_conn.delete_object: (self, container, obj, query_string=None,      
response_dict=None, headers=None)                                        
delete_object                                                            
Wrapper for :func:`delete_object`                                        

Folder edition (bonus)

Create a folder in container

Specify the normal path with ʻobj, but the path ending with /` is recognized as a folder. Is a folder created inside the container? ?? I'm sorry I don't understand Swift so much (´ ・ ω ・ `)

container = 'new-container'
swift_conn.put_object(
    container=container,
    obj="folder/",
    contents=None,
    content_type="application/octet-stream"
    )

Upload file in folder

container = 'new-container'
with open('local.txt', 'r') as local:
    swift_conn.put_object(
        container=container,
        obj='folder/local_object.txt',
        contents=local,
        content_type='text/plain'
    )

Recommended Posts

Easy usage memo of OpenStack Swift API using swiftclient
Installation and easy usage of pytest
Awareness of using Aurora Severless Data API
Control CPU usage of processes (groups) (using cgroups)
Easy to create API server using go-json-rest module
Anonymous upload of images using Imgur API (using Python)
Try using kabu station API of kabu.com Securities