我需要将PDF文件发送到Google Vision以提取并返回文本。根据文档,我了解到PDF文件必须位于Google存储中,因此我会像这样将文件放入我的Google存储桶中:
require '../vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
$storage = new StorageClient([
'keyFilePath' => '/my-keyfile.json',
'projectId' => PROJECT_ID
]);
$bucket = $storage->bucket(BUCKET_NAME);
$bucket->upload(
fopen($_SESSION['local_pdf_url'], 'r')
);
它可以工作。当我重定向到另一个页面,该页面应该将该文件发送到Vision,但这就是失败的地方。我找到了一个示例函数。以下是代码:
require '../vendor/autoload.php';
use Google\Cloud\Storage\StorageClient;
use Google\Cloud\Vision\V1\AnnotateFileResponse;
use Google\Cloud\Vision\V1\AsyncAnnotateFileRequest;
use Google\Cloud\Vision\V1\Feature;
use Google\Cloud\Vision\V1\Feature\Type;
use Google\Cloud\Vision\V1\GcsDestination;
use Google\Cloud\Vision\V1\GcsSource;
use Google\Cloud\Vision\V1\ImageAnnotatorClient;
use Google\Cloud\Vision\V1\InputConfig;
use Google\Cloud\Vision\V1\OutputConfig;
$storage = new StorageClient([
'keyFilePath' => '/my-keyfile.json',
'projectId' => PROJECT_ID
]);
$path = 'gs://my-bucket/'.$_SESSION['pdf_file_name'];
当我运行第二个脚本时,我收到以下错误提示:
致命错误:未捕获的DomainException异常:无法加载默认凭据。请查看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(Array,Object(Google\Auth\HttpHandler\Guzzle6HttpHandler),NULL,NULL)#1 /home/domain/vendor/google/gax/src/CredentialsWrapper.php(114):Google\ApiCore\CredentialsWrapper::buildApplicationDefaultCredentials(Array,Object(Google\Auth\HttpHandler\Guzzle6HttpHandler)) #2 /home/domain/vendor/google/gax/src/GapicClientTrait.php(326):Google\ApiCore\CredentialsWrapper::build(Array)#3 /home/domain/vendor/google/gax/src/GapicClientTrait.php(308):Google\Cloud\Vision\V1\Gapic\ImageAnnotatorGapicClient->createCredentialsWrapper(NULL,Array)#4 /home/domain/vendor/google/cloud/Vision/src/V1/Gapic/ImageAnnotatorGapicClient.php(216):Google\Clou在/home/domain/vendor/google/gax/src/CredentialsWrapper.php的200行。
如何为此服务进行身份验证?我缺少了什么?