Example librados application

We will now go through some example librados applications which use librados to get a better understanding of what you can accomplish with the library.

The following example will take you through the steps to create an application which, when given an image file as a parameter, will store the image as an object in a Ceph cluster and store various attributes about the image file as object attributes. The application will also allow you to retrieve the object and export it as an image file. This example will be written in Python, which is also supported by librados. The following example also uses the Python Imaging Library (PIL) to read an image's size and the Argument Parser library to read command-line parameters:

  1. We first need to install the librados Python bindings and image manipulation libraries:
       $ sudo apt-get install python-rados python-imaging

The preceding command gives the following output:

  1. Create a new file for your Python application ending with the extension .py and enter the following into it:
       import rados, sys, argparse 
from PIL import Image

#Argument Parser used to read parameters and generate --help
parser = argparse.ArgumentParser(description='Image to RADOS
Object Utility')
parser.add_argument('--action', dest='action', action='store',
required=True, help='Either upload or download image to/from
Ceph')
parser.add_argument('--image-file', dest='imagefile',
action='store', required=True, help='The image file to
upload to RADOS')
parser.add_argument('--object-name', dest='objectname',
action='store', required=True, help='The name of the
RADOS object')
parser.add_argument('--pool', dest='pool', action='store',
required=True, help='The name of the RADOS pool to store
the object')
parser.add_argument('--comment', dest='comment', action=
'store', help='A comment to store with the object')


args = parser.parse_args()

try: #Read ceph.conf config file to obtain monitors
cluster = rados.Rados(conffile='/etc/ceph/ceph.conf')
except:
print "Error reading Ceph configuration"
sys.exit(1)

try: #Connect to the Ceph cluster
cluster.connect()
except:
print "Error connecting to Ceph Cluster"
sys.exit(1)

try: #Open specified RADOS pool
ioctx = cluster.open_ioctx(args.pool)
except:
print "Error opening pool: " + args.pool
cluster.shutdown()
sys.exit(1)

if args.action == 'upload': #If action is to upload
try: #Open image file in read binary mode
image=open(args.imagefile,'rb')
im=Image.open(args.imagefile)
except:
print "Error opening image file"
ioctx.close()
cluster.shutdown()
sys.exit(1)
print "Image size is x=" + str(im.size[0]) + " y=" +
str(im.size[1])
try: #Write the contents of image file to object and add
attributes
ioctx.write_full(args.objectname,image.read())
ioctx.set_xattr(args.objectname,'xres',str(im.size[0])
+" ")
ioctx.set_xattr(args.objectname,'yres',str(im.size[1])
+" ")
im.close()
if args.comment:
ioctx.set_xattr(args.objectname,'comment',args.comment
+" ")
except:
print "Error writing object or attributes"
ioctx.close()
cluster.shutdown()
sys.exit(1)
image.close()
elif args.action == 'download':
try: #Open image file in write binary mode
image=open(args.imagefile,'wb')
except:
print "Error opening image file"
ioctx.close()
cluster.shutdown()
sys.exit(1)
try: #Write object to image file
image.write(ioctx.read(args.objectname))
except:
print "Error writing object to image file"
ioctx.close()
cluster.shutdown()
sys.exit(1)
image.close()
else:
print "Please specify --action as either upload or download"
ioctx.close() #Close connection to pool
cluster.shutdown() #Close connection to Ceph
#The End
  1. Test the help functionality generated by the Argument Parser library:
       $ sudo python app1.py --help

The preceding command gives the following output:

  1. Download the Ceph logo to use as a test image:
       wget http://docs.ceph.com/docs/master/_static/logo.png

The preceding command gives the following output:

  1. Run our Python application to read an image file and upload it to Ceph as an object:
       $ sudo python app1.py --action=upload --image-file=test1.png
--object-name=image_test --pool=rbd --comment="Ceph Logo"

The preceding command gives the following output:

  1. Verify that the object has been created:
       $ sudo rados -p rbd ls

The preceding command gives the following output:

  1. Use rados to verify that the attributes have been added to the object:
       $ sudo rados -p rbd listxattr image_test

The preceding command gives the following output:

  1. Use rados to verify the attributes' contents, as shown in the following screenshot:
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.77.250