尝试测试由 pubsub 事件触发的谷歌云功能时出错

时间:2021-06-11 17:10:41

标签: python google-cloud-functions stripe-payments google-cloud-pubsub

我构建了一个由 pubsub 事件触发的谷歌云函数(以便稍后使用云调度程序进行调度)。基本上,我想从条带中检索数据,然后加载到 BigQuery。下面是我的代码源。

import json
import stripe
import pandas as pd
import io
from google.cloud import bigquery

def get_stripe_info(event, context):
    if 'data' in event :
        stripe.api_key = "my stripe key"
        data = stripe.Invoice.list(limit=1)
        result={}
        data_json =[]
        result["customer"] = data["data"][0]["customer"]
        result["amount_paid"] = data["data"][0]["amount_paid"]
        data_json.append(result)
        jsonStr = json.dumps(data_json)
        df = pd.DataFrame(data_json)
        json_data = df.to_json(orient = 'records', lines = True)
        stringio_data = io.StringIO(json_data)

        bq_client = Client()
        dataset_id = 'myDataset'
        table_id = 'myTable'
        dataset_ref = bq_client.dataset(dataset_id)
        table_ref = dataset_ref.table(table_id)
        job_config = bigquery.LoadJobConfig()
        job_config.schema = [
            bigquery.SchemaField("customer", "STRING"),
            bigquery.SchemaField("amount_paid", "INT64")
        ]
        #job_config.skip_leading_rows = 1
        job_config.source_format = bigquery.SourceFormat.NEWLINE_DELIMITED_JSON
        job_config.autodetect = True

        job = bq_client.load_table_from_file(
            stringio_data,
            table_ref,
            location="us",  # Must match the destination dataset location.
            job_config=job_config,
        )  # API request
        job.result()
    

当我尝试在 pubsub 主题上发布消息以触发我的函数时,我收到此错误。请任何帮助!

error log

0 个答案:

没有答案