Storage Examples

Create a backup of a directory and directly stream it to CloudFiles

import subprocess
from datetime import datetime

from import Provider, ContainerDoesNotExistError
from 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' % ('%Y-%m-%d'))
container_name = 'backups'

# Create a container if it doesn't already exist
    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,
    return_code = pipe.poll()

print 'Upload complete, transferred: %s KB' % ((obj.size / 1024))

Efficiently download multiple files using gevent

import os.path

from gevent import monkey
from gevent.pool import Pool

from import get_driver
from 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(,
    filename = os.path.basename(
    path = os.path.join(os.path.expanduser('~/Downloads'), filename)
    print 'Downloading: %s to %s' % (, 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)

print 'Done'

Publishing a static website using CloudFiles driver


This example works with Libcloud version 0.11.0 and above.

from StringIO import StringIO

from import Provider
from 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,
driver.upload_object_via_stream(iterator=iterator2, container=container,
driver.upload_object_via_stream(iterator=iterator3, container=container,

driver.ex_set_error_page(container=container, file_name='error.html')

print('All done you can view the website at: ' +