我只需要使用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' =>'*'));

推荐答案

Files.list返回响应有限的文件列表.

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

要获得文件大小,可以 Select 最佳参数字段,也可以使用.

Fileds=*

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

Php相关问答推荐

在WooCommerce管理订单列表中显示项目总权重

使用WordPress wp_mail()的邮箱标题无效

如何将WooCommerce产品属性术语名称显示为链接术语?

在Laravel中修改JSON输出

当主页和帖子的缩略图相同时,如何减少主页和帖子的缩略图

如何不重新查询模型A-&>;相关模型B-&>模型

限制联系人页面仅允许在WooCommerce中登录的用户

根据WooCommerce的定制订单状态增加产品库存

如何将不同的 Woocommerce 费用合并为一个名称?

有没有办法获取触发流操作的页面的 URI?

主机中未检索用户表数据

使用自定义插件中的 WooCommerce 模板发送自定义邮箱

Laravel 10 中的自定义类未找到

在 vue 自定义渲染器中访问变量

如何获取用户之间的最新消息列表

Shopware 6:无法在新的自定义插件中保存关联字段(媒体)

如何在不解压缩/重新压缩的情况下连接(连接)两个缩小的值

如何在 Process facade 中转义特殊字符?

一个php应用程序可以实例化多个RNG吗

升级到 PHP 8.2:ksort 和 krsort 更改