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:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from swiftclient import client
from base64 import b64encode
from hashlib import md5
from pprint import pprint
from requests.packages.urllib3 import disable_warnings

USER = 'myuser'
KEY = 'myuser01'
TENANT = 'swifty'
ENDPOINT = 'https://api.hcp72.archivas.com/swift/v1/{}'.format(TENANT)
CONTAINER = 'test1'
OBJECT = 'test.txt'


if __name__ == '__main__':

    disable_warnings()

    TOKEN = 'HCP {}:{}'.format(b64encode(USER.encode()).decode(),
                               md5(KEY.encode()).hexdigest())

    # create a connection to HCPs HSwift service
    swc = client.Connection(preauthurl=ENDPOINT,
                            preauthtoken=TOKEN, insecure=True
                            )

    # create a container and get it's metadata afterwards
    swc.put_container(CONTAINER, headers=None, response_dict=None)
    container = swc.head_container(CONTAINER)
    print('Container: {}'.format(CONTAINER))
    pprint(container, indent=4)
    print()

    # store an object and get it's metadata afterwards
    swc.put_object(CONTAINER, obj=OBJECT, contents='This is a Test!')
    obj = swc.head_object(CONTAINER, OBJECT)
    print('Object: {}'.format(OBJECT))
    pprint(obj, indent=4)

    # close the connection
    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…