Google Vision for PDF
Asked Answered
S

2

9

I need to send a PDF file to Google Vision to extract and return text. From documentation I understood that DPF file must be located on Google Storage, so I am putting the file to my Google Storage bucket like this:

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

It works. After I redirect to another page that is suppose to get that file to Vision, and that's where it fails. I found an example function. Here's the code:

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

When I run the second script I get the following errors:

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

How do I authenticate for this service? What am I missing?

Strop answered 16/5, 2019 at 21:31 Comment(0)
S
6

I realize how frustrating it could be when documentation is somewhat lacking organization, content or good examples. Here's what I ended-up doing myself, that finally allowed my script to work. Hope it'll help someone too:

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'];
Strop answered 26/5, 2019 at 22:26 Comment(0)
I
5

The error indicates authentication issues. To resolve the issue, see and follow Using a service account for instructions on authenticating with 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)." - more information here

Inhospitality answered 17/5, 2019 at 3:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.