Change-Id: I1a9ef0c33c68a32808686194c74c27d66d2617e0
4.5 KiB
The swiftclient.Connection API
A low level API that provides methods for authentication and methods that correspond to the individual REST API calls described in the swift documentation.
For usage details see the client docs: swiftclient.client
.
Authentication
This section covers the various combinations of kwargs required when
creating an instance of the Connection
object for
communicating with a swift object store. The combinations of options
required for each authentication version are detailed below, but are
just a subset of those that can be used to successfully authenticate.
These are the most common and recommended combinations.
Keystone Session
from keystoneauth1 import session
from keystoneauth1.identity import v3
# Create a password auth plugin
= v3.Password(auth_url='http://127.0.0.1:5000/v3/',
auth ='tester',
username='testing',
password='Default',
user_domain_name='Default',
project_name='Default')
project_domain_name
# Create session
= session.Session(auth=auth)
keystone_session
# Create swiftclient Connection
= Connection(session=keystone_session) swift_conn
Keystone v3
= 'http://127.0.0.1:5000/v3/'
_authurl = '3'
_auth_version = 'tester'
_user = 'testing'
_key = {
_os_options 'user_domain_name': 'Default',
'project_domain_name': 'Default',
'project_name': 'Default'
}
= Connection(
conn =_authurl,
authurl=_user,
user=_key,
key=_os_options,
os_options=_auth_version
auth_version )
Keystone v2
= 'http://127.0.0.1:5000/v2.0/'
_authurl = '2'
_auth_version = 'tester'
_user = 'testing'
_key = 'test'
_tenant_name
= Connection(
conn =_authurl,
authurl=_user,
user=_key,
key=_tenant_name,
tenant_name=_auth_version
auth_version )
Legacy Auth
= 'http://127.0.0.1:8080/'
_authurl = '1'
_auth_version = 'tester'
_user = 'testing'
_key = 'test'
_tenant_name
= Connection(
conn =_authurl,
authurl=_user,
user=_key,
key=_tenant_name,
tenant_name=_auth_version
auth_version )
Examples
In this section we present some simple code examples that demonstrate
the usage of the Connection
API. You can find full details
of the options and methods available to the Connection
API
in the docstring generated documentation: swiftclient.client
.
List the available containers:
= conn.get_account()
resp_headers, containers print("Response headers: %s" % resp_headers)
for container in containers:
print(container)
Create a new container:
= 'new-container'
container
conn.put_container(container)= conn.get_account()
resp_headers, containers if container in containers:
print("The container was created")
Create a new object with the contents of a local text file:
= 'new-container'
container with open('local.txt', 'r') as local:
conn.put_object(
container,'local_object.txt',
=local,
contents='text/plain'
content_type )
Confirm presence of the object:
= 'local_object.txt'
obj = 'new-container'
container try:
= conn.head_object(container, obj)
resp_headers print('The object was successfully created')
except ClientException as e:
if e.http_status = '404':
print('The object was not found')
else:
print('An error occurred checking for the existence of the object')
Download the created object:
= 'local_object.txt'
obj = 'new-container'
container = conn.get_object(container, obj)
resp_headers, obj_contents with open('local_copy.txt', 'w') as local:
local.write(obj_contents)
Delete the created object:
= 'local_object.txt'
obj = 'new-container'
container try:
conn.delete_object(container, obj)print("Successfully deleted the object")
except ClientException as e:
print("Failed to delete the object with error: %s" % e)