|
| 1 | +import swiftclient |
| 2 | + |
| 3 | +username = '57347988074919328399:swiftadmin' |
| 4 | +password = 'supersecret' |
| 5 | +authurl = 'https://swift.mycompany.com:8083/auth/v1.0' |
| 6 | + |
| 7 | +swift = swiftclient.client.Connection(auth_version='1', user=username, key=password, insecure=True, authurl=authurl) |
| 8 | + |
| 9 | +''' |
| 10 | +If you are using self-signed certificates, you can use the following code: |
| 11 | +
|
| 12 | +cacert = '/path/to/server/cert' |
| 13 | +swift = swiftclient.client.Connection(auth_version='1', user=username, key=password, cacert=cacert, authurl=authurl) |
| 14 | +''' |
| 15 | + |
| 16 | +container = 'test-container' |
| 17 | +obj_key = 'test-object' |
| 18 | + |
| 19 | +# Get authentication information |
| 20 | +print(swift.get_auth()) |
| 21 | + |
| 22 | +# Create container |
| 23 | +swift.put_container(container) |
| 24 | + |
| 25 | +# List all containers and account information |
| 26 | +response = swift.get_account() |
| 27 | +account_info = response[0] |
| 28 | +containers = response[1] |
| 29 | + |
| 30 | +print "Bytes used by account: ", account_info['x-account-bytes-used'] |
| 31 | +print "Number of containers in account: ", account_info['x-account-container-count'] |
| 32 | +print "Number of objects in account: ", account_info['x-account-object-count'] |
| 33 | + |
| 34 | +for c in containers: |
| 35 | + name = c['name'] |
| 36 | + num_objects = c['count'] |
| 37 | + size = c['bytes'] |
| 38 | + print("Container name: %s (total: %s objects, %s bytes)" % (name, num_objects, size)) |
| 39 | + |
| 40 | +# Get information of a single container |
| 41 | +response = swift.get_container(container) |
| 42 | +print "Container: ", response |
| 43 | + |
| 44 | +# Put object into container |
| 45 | +swift.put_object(container, obj_key, |
| 46 | + contents='This is my object\'s content', |
| 47 | + headers={'X-Object-Meta-CustomerID':'42', |
| 48 | + 'X-Object-Meta-Color':'red'}) |
| 49 | + |
| 50 | +# List contents of a container |
| 51 | +for obj in swift.get_container(container)[1]: |
| 52 | + print "Object key: ", obj['name'] |
| 53 | + print "Object size: ", obj['bytes'] |
| 54 | + print "Object last modified: ", obj['last_modified'] |
| 55 | + |
| 56 | +# Get object from container |
| 57 | +response = swift.get_object(container, obj_key) |
| 58 | +object_headers = response[0] |
| 59 | +object_content = response[1] |
| 60 | +print "Object headers: ", object_headers |
| 61 | +print "Object content: ", object_content |
| 62 | + |
| 63 | +# Delete object |
| 64 | +swift.delete_object(container, obj_key) |
| 65 | + |
| 66 | +# Delete container |
| 67 | +swift.delete_container(container) |
0 commit comments