Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Drive API v3 - downloading files in PHP

I'm trying to understand the download flow for the Google Drive API v3 using PHP. Using the API v2 to download a file I:

  • Got the file metadata
  • Used the downloadUrl parameter to get a direct link to the file, attached an oAuth token to it and made a GET request to that.

Using API v3 this appears to have been deprecated, and according to the docs you call files->get() on the Drive Service with an array parameter of "alt" => "media" to get the file itself rather than the metadata.

And their example was:

$fileId = '0BwwA4oUTeiV1UVNwOHItT0xfa2M';
$content = $driveService->files->get($fileId, array(
'alt' => 'media' ));

I'm having trouble understanding how this works though and have trawled through the code but it didn't give much more info.

When you call get(), what actually goes into $content in the example? Is it the contents of the file (in which case this seems troublesome when dealing with large files - surely you'll get out of memory?!) or is it some type of stream reference that I can call fopen on? How would I save this file to disk?

The documentation doesn't really go into any detail about what happens when you make that API call, it just says it performs a file download?

like image 685
jeonatl3 Avatar asked Oct 13 '16 18:10

jeonatl3


People also ask

How do I download files from Google Drive API?

To download a file stored on Google Drive, use the files. get method with the ID of the file to download and the alt=media URL parameter. The alt=media URL parameter tells the server that a download of content is being requested.

How do I automatically download files from Google Drive?

Click Show advanced settings and go to Downloads. Click Change and in the pop-up, navigate to the Downloads folder you dragged to your Google Drive folder and click Select. This selection is now your default download location. Note: If Drive File Stream is not running, files are saved to a different location.

How do I download a file from REST API?

Enter the URL of the REST Service (f.e. http://localhost:8080/rest-file-manager/resr/file/upload ) Select POST as method. Select form-data in the Body. Enter as key “attachment” of Type File.


1 Answers

I figured it out after a bit of experimenting.

When you call the get() method with the alt=>media parameter as specified in the docs you get the underlying HTTP response which is a Guzzle response object (as apparently the client library uses Guzzle for it's underlying transport).

From there you can call any Guzzle response method such as $response->getStatusCode() or you can get a stream of the actual file content.

Would have been helpful if they had documented this somewhere!

EDIT: Here's a rough example if anyone else gets stuck of how to save a file.

<?php

date_default_timezone_set("Europe/London");
require_once 'vendor/autoload.php';

// I'm using a service account, use whatever Google auth flow for your type of account.

putenv('GOOGLE_APPLICATION_CREDENTIALS=/path/to/service/account/key.json');
$client = new Google_Client();
$client->addScope(Google_Service_Drive::DRIVE);
$client->useApplicationDefaultCredentials();

$service = new Google_Service_Drive($client);

$fileId = "0Bxxxxxxxxxxxxxxxxxxxx"; // Google File ID
$content = $service->files->get($fileId, array("alt" => "media"));

// Open file handle for output.

$outHandle = fopen("/path/to/destination", "w+");

// Until we have reached the EOF, read 1024 bytes at a time and write to the output file handle.

while (!$content->getBody()->eof()) {
        fwrite($outHandle, $content->getBody()->read(1024));
}

// Close output file handle.

fclose($outHandle);
echo "Done.\n"

?>
like image 183
jeonatl3 Avatar answered Oct 09 '22 05:10

jeonatl3