简体   繁体   中英

Getting 'client' not defined error while loading CSV file from cloud storage to Big Query table using Cloud Function Python

I am trying to load cloud csv file data to big query table using cloud function. However while deploying I am getting below error in log

line 25, in <module> load_job = client.load_table_from_uri( NameError: name 'client' is not defined

Below is the code -

def my_function():
 print("step-1")
 from google.cloud import bigquery
 print("step0")
# Construct a BigQuery client object.
 client = bigquery.Client()
 print("step1")
# TODO(developer): Set table_id to the ID of the table to create.
 table_id = "project-a-307309:DatasetA.TableD"
 print("step2")
 job_config = bigquery.LoadJobConfig(
    schema=[
        bigquery.SchemaField("Column_A", "STRING"),
        bigquery.SchemaField("Column_B", "STRING"),
        bigquery.SchemaField("Column_C", "STRING"),
        bigquery.SchemaField("Column_D", "STRING"),
        bigquery.SchemaField("Column_E", "STRING"),
    ],
    skip_leading_rows=1,
    # The source format defaults to CSV, so the line below is optional.
    source_format=bigquery.SourceFormat.CSV,
)
uri = "gs://bq_dummy_data/Big Query Dummy Data.csv"
#print("step3")
load_job = client.load_table_from_uri(
    uri, table_id, job_config=job_config
)  # Make an API request.
#print("step4")
load_job.result()  # Waits for the job to complete.
#print("step5")
destination_table = client.get_table(table_id)  # Make an API request.
print("Loaded {} rows.".format(destination_table.num_rows))
my_function()

Requirement.txt

# Function dependencies, for example:
# package>=version
google-cloud-bigquery==2.11.0
google-cloud-storage==1.35.0

Log

2021-03-17T15:00:29.488Zfunction-2 Traceback (most recent call last): File "/layers/google.python.pip/pip/bin/functions-framework", line 8, in <module> sys.exit(_cli()) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 829, in __call__ return self.main(*args, **kwargs) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 782, in main rv = self.invoke(ctx) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 1066, in invoke return ctx.invoke(self.callback, **ctx.params) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/click/core.py", line 610, in invoke return callback(*args, **kwargs) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/functions_framework/_cli.py", line 37, in _cli app = create_app(target, source, signature_type) File "/layers/google.python.pip/pip/lib/python3.8/site-packages/functions_framework/__init__.py", line 234, in create_app spec.loader.exec_module(source_module) File "<frozen importlib._bootstrap_external>", line 783, in exec_module File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed File "/workspace/main.py", line 25, in <module> load_job = client.load_table_from_uri( NameError: name 'client' is not defined

This looks not related with cloud APIS, as the user "Justin Ezequiel", the OP has non uniform indentation

Please fix your indentation to use at least 4 spaces. It's hard to see but it would appear that the line load_job = client.load_table_from_uri( is not indented and thus is not part of the function definition. Meaning client is indeed undefined at that point of your code. – Justin Ezequiel yesterday

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM