Raptor Raptor - 6 months ago 136
PHP Question

Upload image to Google Drive for OCR

I'm trying to upload an image to Google Drive for Optical Character Recognition (OCR). Here are my codes:

require_once('vendor/autoload.php');

// Initialize Google Client
$client_email = 'xxxxxx@yyyyy.iam.gserviceaccount.com';
$private_key = file_get_contents('key.p12');
$scopes = array(
'https://www.googleapis.com/auth/drive.file'
);
$credentials = new Google_Auth_AssertionCredentials(
$client_email,
$scopes,
$private_key
);
$client = new Google_Client();
$client->setAssertionCredentials($credentials);
if ($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion();
}

// Initialize Google Drive service
$service = new Google_Service_Drive($client);

// Upload File
$file = new Google_Service_Drive_DriveFile();
$file->setName('Test Image for OCR');
$file->setDescription('Test Image for OCR');
$file->setMimeType('image/jpeg');
try {
$data = file_get_contents($filename);
$createdFile = $service->files->create($file, array(
'data' => $data,
'mimeType' => 'image/jpeg',
));
var_dump($createdFile);
// ===========
// So, what's next?
// ===========
} catch(Exception $e) {
echo 'Error occurred: ' . $e->getMessage();
}


The above codes run without error, and the
$createdFile
is a valid resource in
Google_Service_Drive_DriveFile
object form.

Questions:


  1. I guess the upload is successful, as the
    create()
    function does not return an error. However, I can't see the files got uploaded in my Google Drive. Shouldn't it be uploaded to Google Drive's root folder?

  2. How can I perform OCR? I can read from here that there is a parameter named
    ocrLanguage
    . Where should I put it and how do I obtain the result?



Thanks in advance.

UPDATE The
var_dump()
results are as follow:

object(Google_Service_Drive_DriveFile)#18 (55) {
["collection_key":protected]=>
string(6) "spaces"
["internal_gapi_mappings":protected]=>
array(0) {
}
["appProperties"]=>
NULL
["capabilitiesType":protected]=>
string(42) "Google_Service_Drive_DriveFileCapabilities"
["capabilitiesDataType":protected]=>
string(0) ""
["contentHintsType":protected]=>
string(42) "Google_Service_Drive_DriveFileContentHints"
["contentHintsDataType":protected]=>
string(0) ""
["createdTime"]=>
NULL
["description"]=>
NULL
["explicitlyTrashed"]=>
NULL
["fileExtension"]=>
NULL
["folderColorRgb"]=>
NULL
["fullFileExtension"]=>
NULL
["headRevisionId"]=>
NULL
["iconLink"]=>
NULL
["id"]=>
string(28) "0B_XXXXX1yjq7dENaQWp4ckZoRk0"
["imageMediaMetadataType":protected]=>
string(48) "Google_Service_Drive_DriveFileImageMediaMetadata"
["imageMediaMetadataDataType":protected]=>
string(0) ""
["kind"]=>
string(10) "drive#file"
["lastModifyingUserType":protected]=>
string(25) "Google_Service_Drive_User"
["lastModifyingUserDataType":protected]=>
string(0) ""
["md5Checksum"]=>
NULL
["mimeType"]=>
string(10) "image/jpeg"
["modifiedByMeTime"]=>
NULL
["modifiedTime"]=>
NULL
["name"]=>
string(18) "Test Image for OCR"
["originalFilename"]=>
NULL
["ownedByMe"]=>
NULL
["ownersType":protected]=>
string(25) "Google_Service_Drive_User"
["ownersDataType":protected]=>
string(5) "array"
["parents"]=>
NULL
["permissionsType":protected]=>
string(31) "Google_Service_Drive_Permission"
["permissionsDataType":protected]=>
string(5) "array"
["properties"]=>
NULL
["quotaBytesUsed"]=>
NULL
["shared"]=>
NULL
["sharedWithMeTime"]=>
NULL
["sharingUserType":protected]=>
string(25) "Google_Service_Drive_User"
["sharingUserDataType":protected]=>
string(0) ""
["size"]=>
NULL
["spaces"]=>
NULL
["starred"]=>
NULL
["thumbnailLink"]=>
NULL
["trashed"]=>
NULL
["version"]=>
NULL
["videoMediaMetadataType":protected]=>
string(48) "Google_Service_Drive_DriveFileVideoMediaMetadata"
["videoMediaMetadataDataType":protected]=>
string(0) ""
["viewedByMe"]=>
NULL
["viewedByMeTime"]=>
NULL
["viewersCanCopyContent"]=>
NULL
["webContentLink"]=>
NULL
["webViewLink"]=>
NULL
["writersCanShare"]=>
NULL
["modelData":protected]=>
array(0) {
}
["processed":protected]=>
array(0) {
}
}


The file can be obtained via
$service->files->get($file_id);
, but it's invisible in my Google Drive. The File Resource object returned does not contain anything useful either.

Answer

A service account isn't you its more like a dummy user. It has its own drive account.

If you want to upload to your personal account. Take the service account email address and share it a directory in your personal drive account. Just like you would any other user you wanted to share a directory or file with.

Then you need to figure out the directory id only way I have found to do that was to have the service account do a files.list to get a list of everything it now has access to. Once you find the directory id or parent id you can change your code above to something like

'data' => $data,
'mimeType' => 'image/jpeg',
'parents'   => 'the directory id'

I think by the looks of your code you are using the V3 api I haven't had time to play with it. 'parents' => 'the directory id' <-- that is an educated guess. If it doesn't work let me know and I will google around a bit to figure out how to pass parent to v3.

Option nr 2:

Another option would be for the service account to share its folder with you then you will have access to its drive account, and you will be able to see that folder in your web version of Drive. Search around for permissions again I think you are using V3 I haven't looked at it yet. Difference is where the data is stored and who the storage gets counted against.

Comments