简体   繁体   English

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

[英]How to download file using libcloud using Python3?

I have seen the documentation for downloading the file. 我看过下载文件的文档。 However, I could not understand what object is in the following api: 但是,我无法理解以下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

Initiate the libcloud driver: 初始化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))

Then download from s3/gcs: 然后从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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM