Google Vision for PDF



我需要向Google Vision发送PDF文件以提取和返回文本。从文档中,我了解到DPF文件必须位于Google存储中,因此我将文件放在我的Google存储存储桶中:

require '../vendor/autoload.php';
use GoogleCloudStorageStorageClient;
$storage = new StorageClient([
    'keyFilePath' => '/my-keyfile.json',
    'projectId' => PROJECT_ID
]);
$bucket = $storage->bucket(BUCKET_NAME);
$bucket->upload(
    fopen($_SESSION['local_pdf_url'], 'r')
);

它有效。在我重定向到另一个页面之后,该页面应该将该文件转换为视觉,这就是它失败的地方。我找到了一个示例功能。这是代码:

require '../vendor/autoload.php';
use GoogleCloudStorageStorageClient;
use GoogleCloudVisionV1AnnotateFileResponse;
use GoogleCloudVisionV1AsyncAnnotateFileRequest;
use GoogleCloudVisionV1Feature;
use GoogleCloudVisionV1FeatureType;
use GoogleCloudVisionV1GcsDestination;
use GoogleCloudVisionV1GcsSource;
use GoogleCloudVisionV1ImageAnnotatorClient;
use GoogleCloudVisionV1InputConfig;
use GoogleCloudVisionV1OutputConfig;
$storage = new StorageClient([
    'keyFilePath' => '/my-keyfile.json',
    'projectId' => PROJECT_ID
]);
$path = 'gs://my-bucket/'.$_SESSION['pdf_file_name'];

当我运行第二个脚本时,我会收到以下错误:

致命错误:未识别的域外:无法加载默认值 证书。浏览 https://developers.google.com/accounts/docs/application-default-credentials 有关更多信息 /home/domain/vendor/google/auth/src/applicationdefaultcredentials.php:168 堆栈跟踪:#0 /home/domain/vendor/google/gax/src/credentialswrapper.php(197(: Google auth ApplicationDefaultCredentials :: getCredentials(数组, 对象(Google auth httphandler guzzle6httphandler(,null,null(#1 /home/domain/vendor/google/gax/src/credentialswrapper.php(114(: Google apicore recretentialswrapper :: buildapplicationdefaultcredentials(数组, 对象(Google auth httphandler guzzle6httphandler(#2 /home/domain/vendor/google/gax/src/gapicclienttrait.php(326(: Google apicore recretentialswrapper :: build(array(#3 /home/domain/vendor/google/gax/src/gapicclienttrait.php(308(: Google Cloud Vision V1 gapic imaNeanNotatorGapicClient-> createCredentialSwrapper(null,null, 数组(#4 /home/domain/vendor/google/cloud/vision/src/v1/gapic/imageannotatorgapicclient.php(216(: Google clou in /home/domain/vendor/google/gax/src/credentialswrapper.php on Line 200

我该如何为此服务进行身份验证?我想念什么?

我意识到,当文档缺乏组织,内容或良好示例时,它可能会令人沮丧。这是我最终做自己的事情,最终使我的脚本起作用。希望它也会帮助某人:

require '../vendor/autoload.php';
use GoogleCloudStorageStorageClient;
use GoogleCloudVisionV1AnnotateFileResponse;
use GoogleCloudVisionV1AsyncAnnotateFileRequest;
use GoogleCloudVisionV1Feature;
use GoogleCloudVisionV1FeatureType;
use GoogleCloudVisionV1GcsDestination;
use GoogleCloudVisionV1GcsSource;
use GoogleCloudVisionV1ImageAnnotatorClient;
use GoogleCloudVisionV1InputConfig;
use GoogleCloudVisionV1OutputConfig;
putenv('GOOGLE_APPLICATION_CREDENTIALS=/my-keyfile.json');
$path = 'gs://my-bucket/'.$_SESSION['pdf_file_name'];

错误指示身份验证问题。要解决问题,请使用服务帐户查看并关注有关使用服务帐户进行身份验证的说明。

"用于身份验证的帐户必须可以访问您为输出指定的云存储存储桶(角色/编辑器或角色/storage.ObjectCreator或更高版本(。" - 这里更多信息

相关内容

  • 没有找到相关文章

最新更新