How to download and upload files on S3 and GCS using Apache Libcloud using Python3?

443 Views Asked by At

The documentation states creating containers but no API has been explained as to how one can download and upload a file, single file not a bunch of them. The language I am looking for is Python3.

1

There are 1 best solutions below

2
Shakeel On

You can try this code which works for me to upload to S3.

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

FILE_PATH = 'test.txt'

cls = get_driver(Provider.S3)
driver = cls('api key', 'api secret key')

container = driver.get_container(container_name='testing-bucket')

extra = {'meta_data': {'owner': 'shakeel', 'created': '2014-02-2'}}

with open(FILE_PATH, 'rb') as iterator:
    obj = driver.upload_object_via_stream(iterator=iterator,
                                          container=container,
                                          object_name='test.txt',
                                          extra=extra)

source: https://libcloud.readthedocs.io/en/latest/storage/examples.html