如何使用libcloud使用Python3下载文件?

时间:2019-01-31 20:16:01

标签: python python-3.x libcloud

我看到用于下载文件的文档。但是,我无法理解以下api中的object是什么:

download_object(obj, destination_path, overwrite_existing=False, delete_on_failure=True)[source]
Download an object to the specified destination path.

Parameters: 
obj (Object) – Object instance.
destination_path (str) – Full path to a file or a directory where the incoming file will be saved.
overwrite_existing (bool) – True to overwrite an existing file, defaults to False.
delete_on_failure (bool) – True to delete a partially downloaded file if the download was not successful (hash mismatch / file size).
Returns:    
True if an object has been successfully downloaded, False otherwise.

Return type:    
bool

1 个答案:

答案 0 :(得分:0)

启动libcloud驱动程序:

def __init__(self, bucket_name, storage_type='s3'):
        self.bucket_name = bucket_name
        self.storage_type = storage_type
        if storage_type == 's3':
            provider_class = S3_REGIONS[config.region_name]
            cls = get_driver(getattr(Provider, provider_class))
            self.driver = cls(config.AWS_ACCESS_KEY_ID, config.AWS_SECRET_ACCESS_KEY)
        elif storage_type == 'gcs':
            cls = get_driver(Provider.GOOGLE_STORAGE)
            self.driver = cls(config.GOOGLE_SERVICE_EMAIL, config.GOOGLE_APPLICATION_CREDENTIALS)
        else:
            raise ValueError('storage type {0} is not supported'.format(self.storage_type))

然后从s3 / gcs下载:

def download(self, cloud_path, local_path=os.getcwd()):
        os.makedirs(os.path.dirname(local_path), exist_ok=True)
        obj = self.driver.get_object(container_name=self.bucket, object_name=cloud_path)
        obj.download(destination_path=local_path, overwrite_existing=overwrite_existing)
相关问题