Home > Enterprise >  Google Drive - Obtaining the FileSize of a File with Just the FileID
Google Drive - Obtaining the FileSize of a File with Just the FileID

Time:12-24

I need to obtain a file's meta-data from just using the Google Drive FileID only. The assumption being that I don't have knowledge of the folderID. By just using the FileID the API just returns null for the filesize but the correct values for the other meta-data.

Just to be clear, I can get all the meta data (including filesize) by processing the files within a folder, for example:

<?php
/*
* With this example I have the folderID from a Google Drive shared link
*/

 $folderId = "https://drive.google.com/drive/folders/1ggWcacF9qQroZOkhfb3tTEwvBzclccwI?usp=share_link";
 $Auth = new Oauth2Authentication();
 $client = $Auth->GetClient();
 $client = $Auth->initializeClient($client);
 $drive = new DriveService($client);
 $pattern = "/[-\w]{25,}(?!.*[-\w]{25,})/";
 if (preg_match($pattern, $folderId, $match)); // grabs folderID from the url
 
 /*
  * Now list the folder contents
 */
 $responseFiles = $drive->ListFiles($match[0]);
 $files = $responseFiles['files'];
 foreach ($files as $file) {
     $id[] = $file->id;
     $name[] = $file->name;
     $mimeType[] = $file->mimeType;
     $size[] = $file->size; //<- correct file size
 };
 $drive->DownloadFile($id, $name, $size);

The code below, which I need to work, returns most of the meta-data except that, as stated above, the $file->size is always null. My DownloadFile method needs the file size so that it can better handle large files. Is there anyone who can help me better understand the problem?

<?php
/*
* With this example I have the only the fileID from a Google Drive shared link
*/

 $fileId = "https://drive.google.com/file/d/1EjNk1ijPLKJMwXfzkEIG487HFzx0v80v/view?usp=share_link";
 $Auth = new Oauth2Authentication();
 $client = $Auth->GetClient();
 $client = $Auth->initializeClient($client);
 $drive = new DriveService($client);
 $pattern = "/[-\w]{25,}(?!.*[-\w]{25,})/";
 if (preg_match($pattern, $fileId, $match)); // grabs fileID from the url
 /*
  * Now get the file meta data and download the file
 */
 $fileId = $match[0];
 $service = new Google_Service_Drive($client);
 $file = $service->files->get($fileId);
 $drive->DownloadFile($file->id, $file->name, $file->size);  //$file->size is always null

Update: Thanks to @DaImTo I was pointed in the right direction. The full fileID's details can be obtained by adding

array('fields' =>'*')

to the argument list of the $service->files->get method, i.e.

$file = $service->files->get($fileId,array('fields' =>'*'));

CodePudding user response:

Files.list retuns a list of files with limited response.

files": [
{
  "kind": "drive#file",
  "mimeType": "application/vnd.google-apps.spreadsheet",
  "id": "1x8-vD-X2wltablGF22Lpwup8VtxNY",
  "name": "Experts Activity Dump go/ExpertsActivities"
},

To get the file size either tweek the optimal parameter fields or just use .

Fileds=*

or

$responseFiles = $drive->ListFiles($match[0],array('fields' =>'*'))
  • Related