Document AI批量提交报错“Failed to process all documents”求助
问题背景
已实现通过RawDocument单文件提交处理,但数据集包含27k张图片,需使用批量处理方案。运行代码时触发错误:Failed to process all documents,调试信息开头如下:
O:17:"Google\Rpc\Status":5:{
s:7:"*code";i:3;s:10:"*message";s:32:"Failed to process all documents.";
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:"code";```
官方错误说明
官方文档指出该错误原因:
gcsUriPrefix和gcsOutputConfig.gcsUri参数需以gs://开头并以斜杠(/)结尾,请检查Bucket URI配置。
现状与疑问
- 未使用
gcsUriPrefix,不确定是否必须使用(存储桶文件数量超过批量处理上限) gcsOutputConfig.gcsUri已符合格式要求(以gs://开头且以/结尾)- 文件列表指向存储桶内正确文件名,文件名本身不带斜杠
相关代码
function filesFromBucket( $directoryPrefix ) { // NOT recursive, does not search the structure $gcsDocumentList = []; $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/')] ); $outputConfig = new DocumentOutputConfig( [ 'gcs_output_config' => new GcsOutputConfig( ['gcs_uri'=>'gs://my-output-bucket/'] ) ] ); $documentProcessorServiceClient = new DocumentProcessorServiceClient(); try { $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)); } else { $error = $operationResponse->getError(); printf('<br>error: %s<br>', serialize($error)); } } finally { $documentProcessorServiceClient->close(); } }
技术建议
修正GCS文档URI格式
代码中$doc->setGcsUri('gs://'.$object->name());存在错误:$object->name()仅返回存储桶内的对象路径,拼接后缺少桶名,正确格式应为gs://<bucket-name>/<object-path>。修改为:$doc->setGcsUri('gs://'.$bucketName.'/'.$object->name());这是导致文件无法被正确定位的核心问题。
解决批量处理上限问题
若存储桶文件数量超过单批处理上限,推荐使用gcsUriPrefix替代手动列举文件,输入配置可改为:$inputConfig = new BatchDocumentsInputConfig( [ 'gcs_uri_prefix' => 'gs://my-input-bucket/the-bucket-path/' ] );此方式无需手动遍历文件,Document AI会自动拉取指定前缀下的所有文件,避免单批数量超限。
验证权限配置
确认Document AI服务账号拥有以下权限:- 输入存储桶:
storage.objects.get - 输出存储桶:
storage.objects.create
- 输入存储桶:
获取详细错误信息
当前错误的details字段为空,可通过Google Cloud Console的Document AI作业页面查看具体失败文件及精准错误原因,便于定位问题。
内容的提问来源于stack exchange,提问作者Stephen

