Skip to main content

Apache Libcloud (Python)

Apache Libcloud is a Python library that abstracts multiple cloud-storage providers behind one interface. It's useful when you want to be able to swap providers without rewriting your application.

Install

pip install apache-libcloud

Initialize the driver

Filebase doesn't have a dedicated Libcloud driver; use the S3 driver with a custom host:

from libcloud.storage.types import Provider
from libcloud.storage.providers import get_driver

S3Driver = get_driver(Provider.S3)

driver = S3Driver(
key='YOUR_FILEBASE_KEY',
secret='YOUR_FILEBASE_SECRET',
region='auto',
host='s3.filebase.io',
secure=True,
)

List containers (buckets)

for container in driver.list_containers():
print(container.name)

Create a container

container = driver.create_container('my-bucket')

Upload an object

extra = {
'content_type': 'image/jpeg',
'meta_data': {'project': 'demo'},
}

with open('photo.jpg', 'rb') as f:
obj = driver.upload_object_via_stream(
iterator=f,
container=container,
object_name='photo.jpg',
extra=extra,
)

List objects in a container

for obj in container.list_objects(prefix='photos/'):
print(obj.name, obj.size)

Download an object

obj = driver.get_object('my-bucket', 'photo.jpg')

driver.download_object(
obj=obj,
destination_path='./downloaded.jpg',
overwrite_existing=True,
)

Delete an object

driver.delete_object(obj)

When to use Libcloud vs. boto3

  • Use boto3 when you're committed to the S3 protocol and want the broadest feature support, AWS-equivalent error handling, and the largest community.
  • Use Libcloud when your application needs to support multiple object storage backends transparently and you can live with a smaller, S3-flavored feature set.

What's next