我一直在尝试让 Document AI 批量提交正常工作,但遇到了一些困难。我使用 RawDocument 进行单个文件提交,假设我可以迭代我的数据集(27k 图像),但选择批处理,因为它似乎是更合适的技术。
当我运行代码时,我看到错误:“无法处理所有文档”。调试信息的前几行是:
O:17:"Google\Rpc\Status":5:{ s:7:"*代码";i:3;s:10:"*消息";s:32:"无法处理所有文档。"; s:26:"Google\Rpc\Statusdetails"; O:38:"Google\Protobuf\Internal\RepeatedField":4:{ s:49:"Google\Protobuf\Internal\RepeatedFieldcontainer";a:0:{}s:44:"Google\Protobuf\Internal\RepeatedFieldtype";i:11;s:45:"Google\Protobuf\Internal\RepeatedFieldklass ";s:19:"Google\Protobuf\Any";s:52:"Google\Protobuf\Internal\RepeatedFieldlegacy_klass";s:19:"Google\Protobuf\Any";}s:38:"Google\Protobuf\ Internal\Messagedesc";O:35:"Google\Protobuf\Internal\Descriptor":13:{s:46:"Google\Protobuf\Internal\Descriptorfull_name";s:17:"google.rpc.Status";s: 42:"Google\Protobuf\Internal\Descriptorfield";a:3:{i:1;O:40:"Google\Protobuf\Internal\FieldDescriptor":14:{s:46:"Google\Protobuf\Internal\FieldDescriptorname ";s:4:"代码";```
对此错误的支持指出错误的原因是:
gcsUriPrefix 和 gcsOutputConfig.gcsUri 参数需要以 gs:// 开头并以反斜杠字符 (/) 结尾。检查存储桶 URI 的配置。
我没有使用 gcsUriPrefix(应该吗?我的存储桶 > 最大批次限制),但我的 gcsOutputConfig.gcsUri 在这些限制之内。我提供的文件列表给出了文件名(指向右侧存储桶),因此不应有尾部反斜杠。
欢迎咨询
function filesFromBucket( $directoryPrefix ) {
// NOT recursive, does not search the structure
$gcsDocumentList = [];
// see https://cloud.google.com/storage/docs/samples/storage-list-files-with-prefix
$bucketName = 'my-input-bucket';
$storage = new StorageClient();
$bucket = $storage->bucket($bucketName);
$options = ['prefix' => $directoryPrefix];
foreach ($bucket->objects($options) as $object) {
$doc = new GcsDocument();
$doc->setGcsUri('gs://'.$object->name());
$doc->setMimeType($object->info()['contentType']);
array_push( $gcsDocumentList, $doc );
}
$gcsDocuments = new GcsDocuments();
$gcsDocuments->setDocuments($gcsDocumentList);
return $gcsDocuments;
}
function batchJob ( ) {
$inputConfig = new BatchDocumentsInputConfig( ['gcs_documents'=>filesFromBucket('the-bucket-path/')] );
// see https://cloud.google.com/php/docs/reference/cloud-document-ai/latest/V1.DocumentOutputConfig
// nb: all uri paths must end with / or an error will be generated.
$outputConfig = new DocumentOutputConfig(
[ 'gcs_output_config' =>
new GcsOutputConfig( ['gcs_uri'=>'gs://my-output-bucket/'] ) ]
);
// see https://cloud.google.com/php/docs/reference/cloud-document-ai/latest/V1.DocumentProcessorServiceClient
$documentProcessorServiceClient = new DocumentProcessorServiceClient();
try {
// derived from the prediction endpoint
$name = 'projects/######/locations/us/processors/#######';
$operationResponse = $documentProcessorServiceClient->batchProcessDocuments($name, ['inputDocuments'=>$inputConfig, 'documentOutputConfig'=>$outputConfig]);
$operationResponse->pollUntilComplete();
if ($operationResponse->operationSucceeded()) {
$result = $operationResponse->getResult();
printf('<br>result: %s<br>',serialize($result));
// doSomethingWith($result)
} else {
$error = $operationResponse->getError();
printf('<br>error: %s<br>', serialize($error));
// handleError($error)
}
} finally {
$documentProcessorServiceClient->close();
}
} Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
通常,错误
“无法处理所有文档”的原因是输入文件或输出存储桶的语法不正确。由于格式不正确的路径可能仍然是云存储的“有效”路径,但不是您期望的文件。 (感谢您首先检查错误消息页面!)如果您要提供要处理的特定文档列表,则不必使用
gcsUriPrefix。尽管根据您的代码,您似乎还是将 GCS 目录中的所有文件添加到BatchDocumentsInputConfig.gcs_documents字段,因此尝试在中发送前缀是有意义的>BatchDocumentsInputConfig.gcs_uri_prefix而不是单个文件的列表。注意:单个批处理请求中可以发送的文件最大数量(1000),并且特定处理器有自己的页面限制。
https://cloud.google.com/document-ai/quotas#content_limits
您可以尝试将文件分成多个批处理请求,以避免达到此限制。 Document AI Toolbox Python SDK 具有用于此目的的内置函数,但您可以尝试根据自己的用例在 PHP 中重新实现此函数。 https:// github.com/googleapis/python-documentai-toolbox/blob/ba354d8af85cbea0ad0cd2501e041f21e9e5d765/google/cloud/documentai_toolbox/utilities/gcs_utilities.py#L213
这被证明是一个 ID-10-T 错误,具有明确的 PEBKAC 泛音。
$object->name() 不会将存储桶名称作为路径的一部分返回。
将
$doc->setGcsUri('gs://'.$object->name());更改为$doc->setGcsUri('gs://'. $bucketName.'/'.$object->name());解决了该问题。