Как скачать файл, используя libcloud, используя Python3? - PullRequest
0 голосов
/ 31 января 2019

Я видел документацию для скачивания файла.Тем не менее, я не мог понять, что такое object в следующем API:

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 голосов
/ 01 февраля 2019

Инициируйте драйвер 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)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...