Google PDF 的愿景

Google Vision for PDF

我需要将 PDF 文件发送到 Google Vision 以提取和 return 文本。从文档中我了解到 DPF 文件必须位于 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,这就是它失败的地方。我找到了 example function。这是代码:

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'];

当我 运行 第二个脚本时,出现以下错误:

Fatal error: Uncaught DomainException: Could not load the default credentials. Browse to https://developers.google.com/accounts/docs/application-default-credentials for more information in /home/domain/vendor/google/auth/src/ApplicationDefaultCredentials.php:168 Stack trace: #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 in /home/domain/vendor/google/gax/src/CredentialsWrapper.php on line 200

如何对此服务进行身份验证?我错过了什么?

该错误表示身份验证问题。要解决此问题,请参阅并遵循 Using a service account 以获取有关使用服务帐户进行身份验证的说明。

"The account used for authentication must have access to the Cloud Storage bucket that you specify for the output (roles/editor or roles/storage.objectCreator or above)." - 更多信息 here

我意识到当文档在某种程度上缺乏组织、内容或好的示例时,这会是多么令人沮丧。这是我 ended-up 自己做的,最终让我的脚本可以运行。希望它也能帮助别人:

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;

putenv('GOOGLE_APPLICATION_CREDENTIALS=/my-keyfile.json');

$path = 'gs://my-bucket/'.$_SESSION['pdf_file_name'];