云函数不会在firestore中创建/存储文档

问题描述 投票:0回答:1

我有以下云功能:

const { Firestore, Timestamp } = require('@google-cloud/firestore');
const { ImageAnnotatorClient } = require('@google-cloud/vision');

const client = new ImageAnnotatorClient();
const projectId = "quick-charger-418202";
const databaseId = "photo-ai-db";
const db = new Firestore({ projectId, databaseId });
console.log('Firestore database client:', db);
console.log("Firestore database ID: ", db.databaseId);


exports.processImage = async (event, context) => {
  // Check if event data is present
  if (!event || !event.name || !event.bucket) {
    console.error('Invalid Cloud Storage event. Exiting.');
    return;
  }

  const filePath = event.name;
  const bucketName = event.bucket;
  const fileName = filePath.split('/').pop();
  
  // Prepare image for the Vision API
  const imageUri = `gs://${bucketName}/${filePath}`;

  console.log(`Calling Vision API for image: ${imageUri}`);
  
  // Call the Vision API
  try {
    const [result] = await client.labelDetection(imageUri);
    const labels = result.labelAnnotations.map(label => label.description);
    console.log(`Labels detected: ${labels.join(', ')}`);
    
    // Store the labels in Firestore
    const docRef = db.collection('photo-ai-db').doc(fileName);
    console.log(`Writing to Firestore document: ${docRef.path}`);
    try {
      // Set document data with merge option to create document if it doesn't exist
      await docRef.update({ labels, processedAt: Timestamp.now() });
      await docRef.set({ labels, processedAt: Timestamp.now() }, { merge: true });
      console.log(`Processed image ${fileName} and stored labels in Firestore.`);
    } catch (error) {
      console.error('Error writing to Firestore:', error);
    }
  } catch (error) {
    console.error('Error calling Vision API:', error);
  }
};

这是我的 firestore 数据库,我手动创建了这些集合,因为我不断收到下面的错误,所以我想如果我创建它们,我可能会有所收获。

该函数从云存储中读取(似乎读取正确,因为日志中提取的标签是正确的)。问题是它不将标签存储在 firestore 中。

日志:

DEFAULT 2024-03-27T05:18:24.393704Z Firestore database client: Firestore {
DEFAULT 2024-03-27T05:18:24.393731Z _settings: {
DEFAULT 2024-03-27T05:18:24.393739Z projectId: 'quick-charger-418202',
DEFAULT 2024-03-27T05:18:24.393745Z databaseId: 'photo-ai-db',
DEFAULT 2024-03-27T05:18:24.393751Z libName: 'gccl',
DEFAULT 2024-03-27T05:18:24.393757Z libVersion: '4.15.1'
DEFAULT 2024-03-27T05:18:24.393762Z },
DEFAULT 2024-03-27T05:18:24.393768Z _settingsFrozen: false,
DEFAULT 2024-03-27T05:18:24.393774Z _serializer: Serializer {
DEFAULT 2024-03-27T05:18:24.393781Z createReference: [Function (anonymous)],
DEFAULT 2024-03-27T05:18:24.393787Z createInteger: [Function (anonymous)],
DEFAULT 2024-03-27T05:18:24.393794Z allowUndefined: false
DEFAULT 2024-03-27T05:18:24.393799Z },
DEFAULT 2024-03-27T05:18:24.393805Z _projectId: 'quick-charger-418202',
DEFAULT 2024-03-27T05:18:24.393811Z registeredListenersCount: 0,
DEFAULT 2024-03-27T05:18:24.393816Z bulkWritersCount: 0,
DEFAULT 2024-03-27T05:18:24.393823Z _backoffSettings: { initialDelayMs: 100, maxDelayMs: 60000, backoffFactor: 1.3 },
DEFAULT 2024-03-27T05:18:24.393830Z _clientPool: ClientPool {
DEFAULT 2024-03-27T05:18:24.393836Z concurrentOperationLimit: 100,
DEFAULT 2024-03-27T05:18:24.393841Z maxIdleClients: 1,
DEFAULT 2024-03-27T05:18:24.393847Z clientFactory: [Function (anonymous)],
DEFAULT 2024-03-27T05:18:24.393854Z clientDestructor: [Function (anonymous)],
DEFAULT 2024-03-27T05:18:24.393860Z activeClients: Map(0) {},
DEFAULT 2024-03-27T05:18:24.393865Z failedClients: Set(0) {},
DEFAULT 2024-03-27T05:18:24.393870Z terminated: false,
DEFAULT 2024-03-27T05:18:24.393876Z terminateDeferred: Deferred {
DEFAULT 2024-03-27T05:18:24.393882Z resolve: [Function (anonymous)],
DEFAULT 2024-03-27T05:18:24.393888Z reject: [Function (anonymous)],
DEFAULT 2024-03-27T05:18:24.393893Z promise: [Promise]
DEFAULT 2024-03-27T05:18:24.393899Z }
DEFAULT 2024-03-27T05:18:24.393904Z }
DEFAULT 2024-03-27T05:18:24.393910Z }
DEFAULT 2024-03-27T05:18:24.394410Z Firestore database ID: undefined
INFO 2024-03-27T05:18:24.491815Z Default STARTUP TCP probe succeeded after 1 attempt for container "worker" on port 8080.
INFO 2024-03-27T05:18:24.592803Z [protoPayload.serviceName: run.googleapis.com] [protoPayload.methodName: v1] [protoPayload.resourceName: namespaces/quick-charger-418202/revisions/processimage-00014-yac] Ready condition status changed to True for Revision processimage-00014-yac.
INFO 2024-03-27T05:18:26.098282Z [protoPayload.serviceName: run.googleapis.com] [protoPayload.methodName: v1] [protoPayload.resourceName: namespaces/quick-charger-418202/services/processimage] Ready condition status changed to True for Service processimage.
NOTICE 2024-03-27T05:18:26.595138638Z [protoPayload.serviceName: cloudfunctions.googleapis.com] [protoPayload.methodName: google.cloud.functions.v2.FunctionService.UpdateFunction] [protoPayload.resourceName: projects/quick-charger-418202/locations/us-central1/functions/processimage] [protoPayload.authenticationInfo.principalEmail: [email protected]] audit_log, method: "google.cloud.functions.v2.FunctionService.UpdateFunction", principal_email: "[email protected]"
INFO 2024-03-27T05:18:32.701075Z [httpRequest.requestMethod: POST] [httpRequest.status: 204] [httpRequest.responseSize: 592 B] [httpRequest.latency: 723 ms] [httpRequest.userAgent: curl 7.74.0] https://us-central1-quick-charger-418202.cloudfunctions.net/processimage
DEFAULT 2024-03-27T05:18:32.759563Z Calling Vision API for image: gs://ece9016-raw-images/teeth-label.jpg
DEFAULT 2024-03-27T05:18:33.257399Z Labels detected: Eye, Mouth, Human body, Jaw, Gesture, Ear, Font, Happy, Tooth, Drawing
DEFAULT 2024-03-27T05:18:33.258075Z Writing to Firestore document: photo-ai-db/teeth-label.jpg
DEFAULT 2024-03-27T05:18:33.435653Z Error writing to Firestore: Error: 5 NOT_FOUND:
DEFAULT 2024-03-27T05:18:33.435678Z at Object.callErrorFromStatus (/workspace/node_modules/@grpc/grpc-js/build/src/call.js:31:19)
DEFAULT 2024-03-27T05:18:33.435684Z at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client.js:190:52)
DEFAULT 2024-03-27T05:18:33.435691Z at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:365:141)
DEFAULT 2024-03-27T05:18:33.435695Z at Object.onReceiveStatus (/workspace/node_modules/@grpc/grpc-js/build/src/client-interceptors.js:328:181)
DEFAULT 2024-03-27T05:18:33.435700Z at /workspace/node_modules/@grpc/grpc-js/build/src/call-stream.js:188:78
DEFAULT 2024-03-27T05:18:33.435705Z at process.processTicksAndRejections (node:internal/process/task_queues:77:11)
DEFAULT 2024-03-27T05:18:33.435711Z for call at
DEFAULT 2024-03-27T05:18:33.435717Z at ServiceClientImpl.makeUnaryRequest (/workspace/node_modules/@grpc/grpc-js/build/src/client.js:160:30)
DEFAULT 2024-03-27T05:18:33.435722Z at ServiceClientImpl.<anonymous> (/workspace/node_modules/@grpc/grpc-js/build/src/make-client.js:105:19)
DEFAULT 2024-03-27T05:18:33.435727Z at /workspace/node_modules/@google-cloud/firestore/build/src/v1/firestore_client.js:205:29
DEFAULT 2024-03-27T05:18:33.435732Z at /workspace/node_modules/google-gax/build/src/normalCalls/timeout.js:44:16
DEFAULT 2024-03-27T05:18:33.435737Z at repeat (/workspace/node_modules/google-gax/build/src/normalCalls/retries.js:80:25)
DEFAULT 2024-03-27T05:18:33.435742Z at /workspace/node_modules/google-gax/build/src/normalCalls/retries.js:118:13
DEFAULT 2024-03-27T05:18:33.435753Z at OngoingCallPromise.call (/workspace/node_modules/google-gax/build/src/call.js:67:27)
DEFAULT 2024-03-27T05:18:33.435758Z at NormalApiCaller.call (/workspace/node_modules/google-gax/build/src/normalCalls/normalApiCaller.js:34:19)
DEFAULT 2024-03-27T05:18:33.435764Z at /workspace/node_modules/google-gax/build/src/createApiCall.js:81:30
DEFAULT 2024-03-27T05:18:33.435768Z Caused by: Error
DEFAULT 2024-03-27T05:18:33.435774Z at WriteBatch.commit (/workspace/node_modules/@google-cloud/firestore/build/src/write-batch.js:417:23)
DEFAULT 2024-03-27T05:18:33.435780Z at DocumentReference.update (/workspace/node_modules/@google-cloud/firestore/build/src/reference.js:393:14)
DEFAULT 2024-03-27T05:18:33.435785Z at exports.processImage (/workspace/index.js:39:20) {
DEFAULT 2024-03-27T05:18:33.435789Z code: 5,
DEFAULT 2024-03-27T05:18:33.435793Z details: '',
DEFAULT 2024-03-27T05:18:33.435797Z metadata: Metadata { internalRepr: Map(0) {}, options: {} },
DEFAULT 2024-03-27T05:18:33.435803Z note: 'Exception occurred in retry method that was not classified as transient'
DEFAULT 2024-03-27T05:18:33.435807Z }
google-cloud-platform google-cloud-firestore google-cloud-functions google-cloud-storage google-cloud-datastore
1个回答
0
投票

我用代码复制了您的问题,发现您正在尝试

update
文档,甚至在它们创建或存在之前。因此,您会得到
Error: 5 NOT\_FOUND
。并且您无法创建
documents
collections
,因为您陷入了错误。

尝试使用以下代码片段:

try {
       await docRef.set({ labels, processedAt: Timestamp.now() }, { merge: true });
      await docRef.update({ labels, processedAt: Timestamp.now() });

      console.log(`Processed image ${fileName} and stored labels in Firestore.`);
    } catch (error) {
      console.error('Error writing to Firestore:', error);
    }

此外,避免在公共论坛上共享项目 ID 或任何其他 PII。请通过删除它们来编辑您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.