HowTo access HCPs Swift interface

This HowTo is not intended to train you on how to code against Swift or compatible services, but as a short instruction how to connect to HCPs Swift-compatible interface (HSwift).

This is for Python 3 using the python-swiftclient package (at the time of writing, we used version 2.6.0).

Make sure you have at least Python 3.4 installed, then use pip to install the python-swiftclient package (you might want to consider using a virtual environment):

$ pip install python-swiftclient

Without Keystone authentication

Pre-requisites on HCP:

  • MAPI enabled on System level

  • A Tenant to be used as Swift service endpoint

  • MAPI enabled within that Tenant

  • Proper setup Namespace defaults

    • Default size for new Namespaces (Containers)

    • Versioning enabled

  • A local user account within the Tenant having Allow namespace management enabled

Let’s have a look at this piece of sample code; the highlighted lines are crucial and commented below:

 1from swiftclient import client
 2from base64 import b64encode
 3from hashlib import md5
 4from pprint import pprint
 5from requests.packages.urllib3 import disable_warnings
 6
 7USER = 'myuser'
 8KEY = 'myuser01'
 9TENANT = 'swifty'
10ENDPOINT = 'https://api.hcp72.archivas.com/swift/v1/{}'.format(TENANT)
11CONTAINER = 'test1'
12OBJECT = 'test.txt'
13
14
15if __name__ == '__main__':
16
17    disable_warnings()
18
19    TOKEN = 'HCP {}:{}'.format(b64encode(USER.encode()).decode(),
20                               md5(KEY.encode()).hexdigest())
21
22    # create a connection to HCPs HSwift service
23    swc = client.Connection(preauthurl=ENDPOINT,
24                            preauthtoken=TOKEN, insecure=True
25                            )
26
27    # create a container and get it's metadata afterwards
28    swc.put_container(CONTAINER, headers=None, response_dict=None)
29    container = swc.head_container(CONTAINER)
30    print('Container: {}'.format(CONTAINER))
31    pprint(container, indent=4)
32    print()
33
34    # store an object and get it's metadata afterwards
35    swc.put_object(CONTAINER, obj=OBJECT, contents='This is a Test!')
36    obj = swc.head_object(CONTAINER, OBJECT)
37    print('Object: {}'.format(OBJECT))
38    pprint(obj, indent=4)
39
40    # close the connection
41    swc.close()

We create an authentication token (line 19 and 20), which is then used along with the endpoint constructed in line 10 to connect to HCP (line 22 and 23).

BTW, python-swiftclient outputs a warning if SSL certificates verification is switched off. To supress this warning, use the code on line 5 and 17.

But take it from me, you really shouldn’t do so!

With Keystone authentication

TBD soon

For everything else I leave it to the python-swiftclient documentation…