Import storage api examples.
Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/e1d8dd99 Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/e1d8dd99 Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/e1d8dd99 Branch: refs/heads/trunk Commit: e1d8dd992716d12656893c92f2dbb582099361c7 Parents: 13c2b8a Author: Tomaz Muraus <[email protected]> Authored: Sat Aug 3 15:19:52 2013 +0200 Committer: Tomaz Muraus <[email protected]> Committed: Sat Aug 3 15:19:52 2013 +0200 ---------------------------------------------------------------------- .../concurrent_file_download_using_gevent.py | 38 ++++++++++++++++++++ .../create_directory_backup_stream_to_cf.py | 32 +++++++++++++++++ .../storage/publish_static_website_on_cf.py | 28 +++++++++++++++ docs/storage/examples.rst | 24 +++++++++++++ 4 files changed, 122 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/e1d8dd99/docs/examples/storage/concurrent_file_download_using_gevent.py ---------------------------------------------------------------------- diff --git a/docs/examples/storage/concurrent_file_download_using_gevent.py b/docs/examples/storage/concurrent_file_download_using_gevent.py new file mode 100644 index 0000000..3bd9083 --- /dev/null +++ b/docs/examples/storage/concurrent_file_download_using_gevent.py @@ -0,0 +1,38 @@ +import os.path + +from gevent import monkey +from gevent.pool import Pool +monkey.patch_all() + +from libcloud.storage.providers import get_driver +from libcloud.storage.types import Provider + +USERNAME = 'username' +API_KEY = 'api key' + +cls = get_driver(Provider.CLOUDFILES_US) +driver = cls(USERNAME, API_KEY) + + +def download_obj(container, obj): + driver = cls(USERNAME, API_KEY) + obj = driver.get_object(container_name=container.name, + object_name=obj.name) + filename = os.path.basename(obj.name) + path = os.path.join(os.path.expanduser('~/Downloads'), filename) + print 'Downloading: %s to %s' % (obj.name, path) + obj.download(destination_path=path) + +containers = driver.list_containers() + +jobs = [] +pool = Pool(20) + +for index, container in enumerate(containers): + objects = container.list_objects() + + for obj in objects: + pool.spawn(download_obj, container, obj) + +pool.join() +print 'Done' http://git-wip-us.apache.org/repos/asf/libcloud/blob/e1d8dd99/docs/examples/storage/create_directory_backup_stream_to_cf.py ---------------------------------------------------------------------- diff --git a/docs/examples/storage/create_directory_backup_stream_to_cf.py b/docs/examples/storage/create_directory_backup_stream_to_cf.py new file mode 100644 index 0000000..0c32617 --- /dev/null +++ b/docs/examples/storage/create_directory_backup_stream_to_cf.py @@ -0,0 +1,32 @@ +import subprocess +from datetime import datetime + +from libcloud.storage.types import Provider, ContainerDoesNotExistError +from libcloud.storage.providers import get_driver + +driver = get_driver(Provider.CLOUDFILES_US)('username', 'api key') + +directory = '/home/some/path' +cmd = 'tar cvzpf - %s' % (directory) + +object_name = 'backup-%s.tar.gz' % (datetime.now().strftime('%Y-%m-%d')) +container_name = 'backups' + +# Create a container if it doesn't already exist +try: + container = driver.get_container(container_name=container_name) +except ContainerDoesNotExistError: + container = driver.create_container(container_name=container_name) + +pipe = subprocess.Popen(cmd, bufsize=0, shell=True, stdout=subprocess.PIPE) +return_code = pipe.poll() + +print 'Uploading object...' + +while return_code is None: + # Compress data in our directory and stream it directly to CF + obj = container.upload_object_via_stream(iterator=pipe.stdout, + object_name=object_name) + return_code = pipe.poll() + +print 'Upload complete, transferred: %s KB' % ((obj.size / 1024)) http://git-wip-us.apache.org/repos/asf/libcloud/blob/e1d8dd99/docs/examples/storage/publish_static_website_on_cf.py ---------------------------------------------------------------------- diff --git a/docs/examples/storage/publish_static_website_on_cf.py b/docs/examples/storage/publish_static_website_on_cf.py new file mode 100644 index 0000000..a4fdd38 --- /dev/null +++ b/docs/examples/storage/publish_static_website_on_cf.py @@ -0,0 +1,28 @@ +from StringIO import StringIO + +from libcloud.storage.types import Provider +from libcloud.storage.providers import get_driver + +CloudFiles = get_driver(Provider.CLOUDFILES_US) + +driver = CloudFiles('username', 'api key') + +container = driver.create_container(container_name='my_website') + +iterator1 = StringIO('<p>Hello World from Libcloud!</p>') +iterator2 = StringIO('<p>Oh, noez, 404!!</p>') +iterator3 = StringIO('<p>Oh, noez, 401!!</p>') + +driver.upload_object_via_stream(iterator=iterator1, container=container, + object_name='index.html') +driver.upload_object_via_stream(iterator=iterator2, container=container, + object_name='404error.html') +driver.upload_object_via_stream(iterator=iterator3, container=container, + object_name='401error.html') + +driver.ex_enable_static_website(container=container) +driver.ex_set_error_page(container=container, file_name='error.html') +driver.enable_container_cdn(container=container) + +print('All done you can view the website at: ' + + driver.get_container_cdn_url(container=container)) http://git-wip-us.apache.org/repos/asf/libcloud/blob/e1d8dd99/docs/storage/examples.rst ---------------------------------------------------------------------- diff --git a/docs/storage/examples.rst b/docs/storage/examples.rst new file mode 100644 index 0000000..c73c0a0 --- /dev/null +++ b/docs/storage/examples.rst @@ -0,0 +1,24 @@ +Storage Examples +================ + +Create a backup of a directory and directly stream it to CloudFiles +------------------------------------------------------------------- + +.. literalinclude:: /examples/storage/create_directory_backup_stream_to_cf.py + :language: python + + +Efficiently download multiple files using gevent +------------------------------------------------ + +.. literalinclude:: /examples/storage/concurrent_file_download_using_gevent.py + :language: python + +Publishing a static website using CloudFiles driver +--------------------------------------------------- + +.. note:: + This example works with Libcloud version 0.11.0 and above. + +.. literalinclude:: /examples/storage/publish_static_website_on_cf.py + :language: python
