我一直在尝试让 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('
result: %s
',serialize($result));
// doSomethingWith($result)
} else {
$error = $operationResponse->getError();
printf('
error: %s
', serialize($error));
// handleError($error)
}
} finally {
$documentProcessorServiceClient->close();
}
} Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
这被证明是一个 ID-10-T 错误,具有明确的 PEBKAC 泛音。
$object->name() 不会将存储桶名称作为路径的一部分返回。
将
$doc->setGcsUri('gs://'.$object->name());更改为$doc->setGcsUri('gs://'. $bucketName.'/'.$object->name());解决了该问题。