You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Google Cloud Functions时Datastore无数据写入的问题排查

Datastore写入无数据但无报错排查问题

我配置了Google Cloud Function,当文档上传到存储桶时自动触发Document AI处理器,之后把结果写入Datastore(已创建Datastore模式数据库)。但函数运行后,Datastore里看不到任何数据,日志也没捕获到错误。

负责写入数据库的代码

private static void InsertEntitiesToDatastore(DBObject tempDBObject)
{
    Console.WriteLine("InsertEntitiesToDatastore");
    InsertInvoiceEntityToDatastore(tempDBObject.InvoiceObj, "Invoices");
    InsertItemsEntityToDatastore(tempDBObject.InvoiceObj.Store, tempDBObject.ItemsObj, "Items");
}

public static void InsertInvoiceEntityToDatastore(Invoice invoice, string invoiceKindName)
{
    Console.WriteLine("InsertInvoiceEntityToDatastore");
    var client = DatastoreDb.Create(projectId);

    var invoiceEntity = new DsEntity
    {
        Key = client.CreateKeyFactory(invoiceKindName).CreateIncompleteKey()
    };

    DateTime purchaseDateTime = invoice.Purchase_Date;
    if (purchaseDateTime.Kind != DateTimeKind.Utc)
    {
        purchaseDateTime = purchaseDateTime.ToUniversalTime();
    }
    Timestamp timestamp = Timestamp.FromDateTime(purchaseDateTime);

    invoiceEntity["Purchase_Date"] = timestamp;
    invoiceEntity["SubTotal"] = invoice.SubTotal;
    invoiceEntity["Store"] = invoice.Store;
    client.Upsert(invoiceEntity);
    Console.WriteLine($"InsertInvoiceEntityToDatastore __ Upsert: {invoiceEntity}");
}

public static void InsertItemsEntityToDatastore(string Store, List<Item> items, string invoiceKindName)
{
    Console.WriteLine("InsertItemsEntityToDatastore");
    var client = DatastoreDb.Create(projectId);
    var itemEntities = new List<DsEntity>();
    var keyFactory = client.CreateKeyFactory(invoiceKindName);

    foreach (var item in items)
    {
        DsEntity dsEntity = new DsEntity
        {
            Key = keyFactory.CreateIncompleteKey()
        };
        dsEntity["Item_name"] = item.Item_name;
        dsEntity["Item_price"] = item.Item_price;
        dsEntity["Item_pant"] = item.Item_pant;
        dsEntity["Item_quantity"] = item.Item_quantity;
        dsEntity["Store"] = Store;
        itemEntities.Add(dsEntity);
    }
    client.Upsert(itemEntities);
}

补充信息

  1. 第一个client.Upsert(Entity)的日志输出:
InsertInvoiceEntityToDatastore __ Upsert: { "key": { "partitionId": { "projectId": *projectID* }, "path": [ { "kind": "Invoices", "id": "5632499082330112" } ] }, "properties": { "Purchase_Date": { "timestampValue": "2024-02-29T00:00:00Z" }, "SubTotal": { "doubleValue": 77.30000305175781 }, "Store": { "stringValue": "REMA 1000 ELGESETER" } } }
  1. 服务账号的IAM角色:
  • Cloud Datastore User
  • Cloud Functions Developer
  • Service Account User
  • Storage Object Viewer

需要补充的排查信息

  • 确认Datastore数据库的位置:Cloud Function和Datastore是否处于同一GCP区域/位置?Datastore的位置是创建时指定的,跨区域可能导致写入延迟或配置问题。
  • 验证projectId变量:日志中的*projectID*是否为实际项目ID?确认代码中的projectId与Datastore所在项目ID完全一致。
  • 检查Datastore索引状态:虽然基础写入无需索引,但可查看控制台索引页面是否有未完成的构建任务(不过写入失败通常会报错)。
  • 补充InsertItemsEntityToDatastore的日志:目前仅能看到Invoice的Upsert日志,需确认Items方法是否被正常调用,以及Items列表是否为空。
  • 查看函数执行超时设置:默认超时为1分钟,若Document AI处理+Datastore写入耗时超过该时间,可能导致写入未完成。
  • 确认Datastore命名空间:代码中是否指定了自定义命名空间?若控制台查看时选错命名空间,可能看不到写入的实体。

内容的提问来源于stack exchange,提问作者kriwet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.20 16:58:17