我只需要使用Google Drive FileID来获取文件的元数据.假设我不知道FolderID.通过仅使用FileID
,API只为filesize
返回null
,但为其他元数据返回正确的值.
为了清楚起见,我可以通过处理文件夹中的文件来获取所有元数据(包括文件大小),例如:
<?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);
下面的代码是我需要使用的,它返回大多数元数据,但如上所述,$file->size
始终是null
.我的DownloadFile
方法需要文件大小,这样它才能更好地处理大文件.有谁能帮我更好地理解这个问题吗?
<?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:感谢@DaImto,我被指向了正确的方向.完整的fileID的详细信息可以通过添加
array('fields' =>'*')
添加到$service->files->get
方法的参数列表,即
$file = $service->files->get($fileId,array('fields' =>'*'));