def download()

in ark-demo/pipelines/nfhl/nfhl_gcs_stage.py [0:0]


def download(nfhl_file, nfhl_url, gcs_bucket_name, gcs_path):
    gcs_client = storage.Client()
    gcs_bucket = gcs_client.bucket(gcs_bucket_name)
    gcs_filepath = gcs_path + nfhl_file
    gcs_exists = storage.Blob(bucket=gcs_bucket, name=gcs_filepath).exists(gcs_client)
    if gcs_exists:
        return None, 'exists'

    if Path(nfhl_file).exists():
        return nfhl_file, 'exists'

    try:
        urllib.request.urlretrieve(nfhl_url, nfhl_file, show_progress(nfhl_file))
        return nfhl_file, 'success'
    except urllib.error.HTTPError as e:
        if e.code == 404:
            return None, e.code
    except Exception as e:
        return None, e