def download()

in container-filetransfer/cloud-transfer.py [0:0]


def download(url, path, force_handler=None):
    """
    Downloads a remote file to local storage. Assumes that path is a local
    directory, and files are downloaded preserving their remote filenames.
    """
    logger = logging.getLogger(logger_name)

    if os.path.exists(path):
        if not os.path.isdir(path):
            logger.warning(f"Skipping download of '{url}': destination '{path}' exists and is not a directory")
            return False
    else:
        dirname = os.path.dirname(path)
        if dirname and not os.path.isdir(dirname):
            try:
                os.makedirs(dirname)
                logger.debug(f"Created directory '{dirname}'")
            except Exception:
                logger.exception(f"Skipping download of '{url}': failed to create destination directory '{dirname}'")
                return False

    handlers = {
        'sftp': download_sftp,
        'ftp': download_ftp,
        'ftps': download_ftp,
        'http': download_http_auto,
        'https': download_http_auto,
        'blob': download_azure_blob,
        's3': download_amazon_s3,
        'gs': download_google_storage,
        '': local_copy
    }

    if force_handler:
        handler = force_handler
    else:
        handler = urlparse(url).scheme

    if handler not in handlers:
        logger.error(f"Skipping download of '{url}': no implemented handlers for '{handler}'")
        return False

    logger.debug(f"Starting downloading of '{url}' to '{path}'")
    try:
        handlers[handler](url, path)
    except FileNotFoundError:
        logger.warning(f"Skipping download of '{url}' because it was not found on the remote server")
    except PermissionError:
        logger.warning(f"Skipping download of '{url}' because of insufficient permissions.")
    except Exception:
        logger.exception(f"Download of '{url}' to '{path}' failed")