diff --git a/firstapp/samples/shade/durability.py b/firstapp/samples/shade/durability.py new file mode 100644 index 000000000..2d9e87a33 --- /dev/null +++ b/firstapp/samples/shade/durability.py @@ -0,0 +1,79 @@ +#! /usr/bin/env python + +# step-1 +from __future__ import print_function +import hashlib +from shade import * + +conn = openstack_cloud(cloud='myfavoriteopenstack') + +# step-2 +container_name = 'fractals' +container = conn.create_container(container_name) +print(container) + +# step-3 +print(conn.list_containers()) + +# step-4 +file_path = 'goat.jpg' +object_name = 'an amazing goat' +container = conn.get_container(container_name) + +object = conn.create_object(container=container_name, name=object_name, filename=file_path) + +# step-5 +print(conn.list_objects(container_name)) + +# step-6 +object = conn.get_object(container_name, object_name) +print(object) + +# step-7 +print(hashlib.md5(open('goat.jpg', 'rb').read()).hexdigest()) + +# step-8 +conn.delete_object(container_name, object_name) + +# step-9 +# step-10 +container_name = 'fractals' +print(conn.get_container(container_name)) + +# step-11 +import base64 +import cStringIO +import json + +import requests + +endpoint = 'http://IP_API_1' +params = { 'results_per_page': '-1' } +response = requests.get('%s/v1/fractal' % endpoint, params=params) +data = json.loads(response.text) +for fractal in data['objects']: + r = requests.get(url, stream=True) + with open(fractal['uuid'], 'wb') as f: + for chunk in r.iter_content(chunk_size=1024): + if chunk: + f.write(chunk) + + f.close() +conn.create_object(container=container_name, name=fractal['uuid']) + +for object in conn.list_objects(container_name): + print(object) + +# step-12 +for object in conn.list_objects(container_name): + conn.delete_objects(container_name, object['name']) + +conn.delete_container(container_name) + +# step-13 +metadata = {'foo': 'bar'} +conn.create_object(container=container_name, name=fractal['uuid'], + metadata=metadata +) + +# step-14 diff --git a/firstapp/source/durability.rst b/firstapp/source/durability.rst index 213ae4a7f..03f72e04e 100644 --- a/firstapp/source/durability.rst +++ b/firstapp/source/durability.rst @@ -117,6 +117,11 @@ First, learn how to connect to the Object Storage endpoint: .. warning:: This section has not yet been completed for the PHP-OpenCloud SDK. +.. only:: shade + + .. literalinclude:: ../samples/shade/durability.py + :start-after: step-1 + :end-before: step-2 To begin to store objects, we must first make a container. Call yours :code:`fractals`: @@ -152,6 +157,23 @@ Call yours :code:`fractals`: +.. only:: shade + + .. literalinclude:: ../samples/shade/durability.py + :start-after: step-2 + :end-before: step-3 + + You should see output such as: + + .. code-block:: python + + Munch({u'content-length': u'0', u'x-container-object-count': u'0', + u'accept-ranges': u'bytes', u'x-container-bytes-used': u'0', + u'x-timestamp': u'1463950178.11674', u'x-trans-id': + u'txc6262b9c2bc1445b9dfe3-00574277ff', u'date': u'Mon, 23 May 2016 + 03:24:47 GMT', u'content-type': u'text/plain; charset=utf-8'}) + + You should now be able to see this container appear in a listing of all containers in your account: @@ -186,6 +208,17 @@ all containers in your account: [] +.. only:: shade + + .. literalinclude:: ../samples/shade/durability.py + :start-after: step-3 + :end-before: step-4 + + .. code-block:: python + + [Munch({u'count': 0, u'bytes': 0, u'name': u'fractals'}), + Munch({u'count': 0, u'bytes': 0, u'name': u'fractals_segments'})] + The next logical step is to upload an object. Find a photo of a goat on line, name it :code:`goat.jpg`, and upload it to your :code:`fractals` container: @@ -209,6 +242,12 @@ on line, name it :code:`goat.jpg`, and upload it to your :start-after: step-4 :end-before: step-5 +.. only:: shade + + .. literalinclude:: ../samples/shade/durability.py + :start-after: step-4 + :end-before: step-5 + List objects in your :code:`fractals` container to see if the upload was successful. Then, download the file to verify that the md5sum is the same: @@ -301,6 +340,29 @@ the same: 7513986d3aeb22659079d1bf3dc2468b +.. only:: shade + + .. literalinclude:: ../samples/shade/durability.py + :start-after: step-5 + :end-before: step-6 + + :: + + [Munch({u'hash': u'd1408b5bf6510426db6e2bafc2f90854', u'last_modified': + u'2016-05-23T03:34:59.353480', u'bytes': 63654, u'name': u'an amazing + goat', u'content_type': u'application/octet-stream'})] + + .. literalinclude:: ../samples/shade/durability.py + :start-after: step-6 + :end-before: step-7 + + .. literalinclude:: ../samples/shade/durability.py + :start-after: step-7 + :end-before: step-8 + + :: + + d1408b5bf6510426db6e2bafc2f90854 Finally, clean up by deleting the test object: @@ -335,6 +397,21 @@ Finally, clean up by deleting the test object: [] +.. only:: shade + + .. literalinclude:: ../samples/shade/durability.py + :start-after: step-8 + :end-before: step-9 + + :: + + Munch({u'content-length': u'0', u'x-container-object-count': u'0', + u'accept-ranges': u'bytes', u'x-container-bytes-used': u'0', + u'x-timestamp': u'1463950178.11674', u'x-trans-id': + u'tx46c83fa41030422493110-0057427af3', u'date': u'Mon, 23 May 2016 + 03:37:23 GMT', u'content-type': u'text/plain; charset=utf-8'}) + + Back up the Fractals from the database on the Object Storage ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -362,6 +439,12 @@ Place the images in the :code:`fractals` container: :start-after: step-10 :end-before: step-11 +.. only:: shade + + .. literalinclude:: ../samples/shade/durability.py + :start-after: step-10 + :end-before: step-11 + Next, back up all existing fractals from the database to the swift container. A simple loop takes care of that: @@ -399,6 +482,19 @@ A simple loop takes care of that: Before you try to run the previous script, make sure that it is installed on your system. +.. only:: shade + + .. literalinclude:: ../samples/shade/durability.py + :start-after: step-11 + :end-before: step-12 + + .. note:: Replace :code:`IP_API_1` with the IP address of the API instance. + + .. note:: The example code uses the awesome + `Requests library `_. + Before you try to run the previous script, make sure that + it is installed on your system. + Configure the Fractals app to use Object Storage ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -435,6 +531,12 @@ Otherwise, the delete operation fails: :start-after: step-12 :end-before: step-13 +.. only:: shade + + .. literalinclude:: ../samples/shade/durability.py + :start-after: step-12 + :end-before: step-13 + .. warning:: It is not possible to restore deleted objects. Be careful. Add metadata to objects @@ -475,6 +577,20 @@ your SDK. .. todo:: It would be nice to have a pointer here to section 9. +.. only:: shade + + This adds a "foo" key to the metadata that has a value of "bar". + + .. Note:: + + Swift metadata keys are prepended with "x-object-meta-" so when you get + the object with get_object(), in order to get the value of the metadata + your key will be "x-object-meta-foo". + + .. literalinclude:: ../samples/shade/durability.py + :start-after: step-13 + :end-before: step-14 + Large objects ~~~~~~~~~~~~~ @@ -527,6 +643,12 @@ For efficiency, most Object Storage installations treat large objects, .. literalinclude:: ../samples/jclouds/Durability.java :language: java +.. only:: shade + + Shade's create_object function has a "use_slo" parameter (that defaults to + true) which will break your object into smaller objects for upload and + rejoin them if needed. + Next steps ----------