Google Cloud函数 – 文档AI错误

huangapple go评论86阅读模式
英文:

Google Cloud function - Document Ai error

问题

我正在尝试编写一个Firebase云函数,当图像上传到特定存储桶时触发该函数。它将该图像发送到Document AI以由自定义处理器处理,并将返回记录在Firestore数据库中。

我之前使用Cloud Vision做了同样的事情,它运行得很好。我只是需要比Vision提供的更好的解析。

然而,当我触发这个新的云函数时,我一直在Google日志中收到错误。它以Error开始:

> 3 INVALID_ARGUMENT: 请求包含无效参数。

我在代码中添加了一些日志记录以尝试缩小问题范围。最终,我得到了以下截断的响应:

> statusDetails: [ BadRequest { fieldViolations: [Array] } ]

请注意,我已经检查并多次检查了我的处理器ID、项目ID和位置。我还检查了我的服务帐户权限。

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const { DocumentProcessorServiceClient } = require('@google-cloud/documentai').v1beta3;

admin.initializeApp({
    credential: admin.credential.cert(require('./sample-room-58c38-bd767e088e9d.json')),
});

const client = new DocumentProcessorServiceClient({
    projectId: 'sample-room-58c38',
    keyFilename: './sample-room-58c38-bd767e088e9d.json',
});

const bucketName = 'sample-room-58c38-q6gtc';

exports.processImage = functions.storage
    .bucket(bucketName)
    .object()
    .onFinalize(async (object) => {
        const gcsUri = `gs://${bucketName}/${object.name}`;
        const request = {
            name: `projects/sample-room-58c38/locations/us/processor/a89f2d4fd6b09a9a`,
            inputConfig: {
                gcsDocumentUri: gcsUri,
                mimeType: 'image/jpeg',
            },
        };

        console.log('gcsUri:', gcsUri);
        console.log('Request:', JSON.stringify(request, null, 2));

        try {
            const [result] = await client.processDocument(request);

            const { document } = result;
            const { formFields } = document;
            const data = {};

            formFields.forEach(field => {
                const fieldName = field.fieldName.text;
                const fieldValue = field.fieldValue.text;

                
                data[fieldName] = fieldValue;
            });

            console.log('Data:', data);

            return admin.firestore().collection('DocumentAI Photos').doc(object.name).set(data);
        } catch (error) {
            console.error('Error in processDocument:', JSON.stringify(error, null, 2)); // <--- 在此处记录错误
            throw error;
        }
    });

希望这可以帮助你解决问题。

英文:

I'm trying to write a Firebase cloud function that triggers when an image is uploaded to a specific bucket. It sends that image over to document ai to be processed by a custom processor and logs the return in Firestore database.

I did the same thing with cloud vision and it worked fine. I just needed better parsing than Vision had to offer.

However, when I trigger this new cloud function I keep receiving an error in the Google logs. It started with Error:

> 3 INVALID_ARGUMENT: Request contains an invalid argument.

I added some logging to the code to try and narrow down the problem. eventually, I got this but it was a truncated response.

> statusDetails: [ BadRequest { fieldViolations: [Array] } ]

Just to note I've checked and triple-checked my processor id, project id, and location. I've also checked my service account permissions.

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const { DocumentProcessorServiceClient } = require('@google-cloud/documentai').v1beta3;
admin.initializeApp({
credential: admin.credential.cert(require('./sample-room-58c38-bd767e088e9d.json')),
});
const client = new DocumentProcessorServiceClient({
projectId: 'sample-room-58c38',
keyFilename: './sample-room-58c38-bd767e088e9d.json',
});
const bucketName = 'sample-room-58c38-q6gtc';
exports.processImage = functions.storage
.bucket(bucketName)
.object()
.onFinalize(async (object) => {
const gcsUri = `gs://${bucketName}/${object.name}`;
const request = {
name: `projects/sample-room-58c38/locations/us/processor/a89f2d4fd6b09a9a`,
inputConfig: {
gcsDocumentUri: gcsUri,
mimeType: 'image/jpeg',
},
};
console.log('gcsUri:', gcsUri);
console.log('Request:', JSON.stringify(request, null, 2));
try {
const [result] = await client.processDocument(request);
const { document } = result;
const { formFields } = document;
const data = {};
formFields.forEach(field => {
const fieldName = field.fieldName.text;
const fieldValue = field.fieldValue.text;
data[fieldName] = fieldValue;
});
console.log('Data:', data);
return admin.firestore().collection('DocumentAI Photos').doc(object.name).set(data);
} catch (error) {
console.error('Error in processDocument:', JSON.stringify(error, null, 2)); // <--- Error logging here
throw error;
}
});

答案1

得分: 1

你的请求对象结构不正确。

目前,Document AI 的 Node.JS 客户端库不支持对存储在 Google Cloud Storage 中的文档进行在线(同步)处理,您需要使用批处理来处理 Cloud Storage 中的文档。

您可以参考此页面上的代码示例,了解如何使用 Node.JS 客户端库进行上传文件的在线处理以及处理 Google Cloud Storage 中的文件的批处理

英文:

Your request object structure is not correct.

Currently, The Node.JS Client libraries for Document AI don't allow online (synchronous) processing with documents in Google Cloud Storage, you will need to use Batch processing to process documents in Cloud Storage.

You can refer to the code samples on this page to see how to use the Node.JS client library for online processing with an uploaded file and batch processing for files in Google Cloud Storage

huangapple
  • 本文由 发表于 2023年8月4日 23:53:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837483.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定