根据错误创建条件:google.api_core.exceptions.NotFound:404找不到:URI

时间:2019-12-09 20:13:13

标签: python google-bigquery

我想知道是否有可能在云存储中找不到csv文件的情况下,终止脚本(例如打印)。

def insert_bigquery(target_uri, dataset_id, table_id):
    bigquery_client = bigquery.Client()
    dataset_ref = bigquery_client.dataset(dataset_id)
    job_config = bigquery.LoadJobConfig()
    job_config.schema = [
        bigquery.SchemaField('id','STRING',mode='REQUIRED')
    ]
    job_config.source_format = bigquery.SourceFormat.CSV
    job_config.field_delimiter = ";"
    uri = target_uri
    load_job = bigquery_client.load_table_from_uri(
        uri,
        dataset_ref.table(table_id),
        job_config=job_config
        )
    print('Starting job {}'.format(load_job.job_id))
    load_job.result()
    print('Job finished.')

insert_bigquery(bucket_name, dataset, tabela)

1 个答案:

答案 0 :(得分:1)

由于我不知道将要使用的csv,所以我进行了目录级测试,并且它起作用了。

def blob_exists():
    client = storage.Client()
    bucket = client.get_bucket('bucket_name')
    bb = bucket.list_blobs(prefix='test_directory')
    for resource in bb:
        print(resource.name)
        blob = bucket.blob(resource.name)
        return blob.exists()

a = blob_exists()

if a is True:
    print("OK")
else:
    print("Not found")