0

I would like to know if it is possible to make a condition if you do not find csv file in cloud storage, terminate the script (put a print for example).

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)
Felipe FB
  • 1,212
  • 6
  • 22
  • 55
  • May be this answer would help - https://stackoverflow.com/questions/13525482/python-gae-how-to-check-if-file-exists-in-google-cloud-storage – Priya Agarwal Dec 10 '19 at 07:55
  • @PriyaAgarwal This is relevant but as I do not know the exact name of the csv to come, I can do the verification at the directory level? Or Is there any way to capture the name of the coming csv? – Felipe FB Dec 10 '19 at 12:22

1 Answers1

2

As I will not know of the csv that will come, I did a directory level test and it worked.

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")
Felipe FB
  • 1,212
  • 6
  • 22
  • 55