Google Drive SDK下载文件-它的作用


Google Drive SDK downloadFile - What does it do?

我正在Drupal中开发,并使用Google Drive SDK中的php代码,该代码应该下载一个文件:

/**
 * Download a file's content.
 *
 * @param Google_DriveService $service Drive API service instance.
 * @param File $file Drive File instance.
 * @return String The file's content if successful, null otherwise.
 */
function downloadFile($service, $file) {
  $downloadUrl = $file->getDownloadUrl();
  if ($downloadUrl) {
    $request = new Google_HttpRequest($downloadUrl, 'GET', null, null);
    $httpRequest = Google_Client::$io->authenticatedRequest($request);
    if ($httpRequest->getResponseHttpCode() == 200) {
      return $httpRequest->getResponseBody();
    } else {
      // An error occurred.
      return null;
    }
  } else {
    // The file doesn't have any content stored on Drive.
    return null;
  }
}

它返回了一个成功的200响应代码,但我无法判断它实际上做了什么。然而,通过观察我的局域网连接状态,我可以看出正在下载以字节为单位的合适大小的东西。我只是不知道它的结局。我使用的是Acquia Dev Desktop,所以我在电脑上运行网站,但如果下载的文件确实存储在网站文件夹中,我找不到它的最终位置。然后我尝试了我在这里找到的另一个函数,得到了类似的结果。更熟悉这一点的人能解释发生了什么吗?

function GetFile($service, $fileId) {
   $fileVars = null;
   try {
      /*
       * Retrieve metadata for the file specified by $fileId.
       */
      $file = $service->files->get($fileId);
      $fileVars = get_object_vars($file);
      /*
       * Retrieve the file's content using download URL specified in metadata.
       */
      $downloadUrl = $file->getDownloadUrl();
      error_log('Download URL file from Drive: ' . $downloadUrl);
      if ($downloadUrl) {
         $request = new Google_HttpRequest($downloadUrl, 'GET', null, null);
         $httpRequest = Google_Client::$io->authenticatedRequest($request);
         error_log(print_r($httpRequest, 1));
         if ($httpRequest->getResponseHttpCode() == 200) {
            $content = $httpRequest->getResponseBody();
            $fileVars['content'] = $content?($content):'';
         } else {
            // An error occurred.
            return null;
         }
      } else {
         // The file doesn't have any content stored on Drive.
         return null;
      }
   } catch (apiServiceException $e) {
      /*
       * Log error and re-throw
       */
      error_log('Error retrieving file from Drive: ' . $e->getMessage());
      throw $e;
   }
   return json_encode($fileVars);
}

此方法返回以下结果:

{"alternateLink":"https:'/'/docs.google.com'/file'/d'/0B6ahcE1NPpIaMTJzcFRLMnNiYU0'/edit?usp=drivesdk","appDataContents":false,"createdDate":"2013-12-05T21:05:03.026Z","defaultOpenWithLink":null,"description":"Flashum mp4","downloadUrl":"https:'/'/doc-0g-50-docs.googleusercontent.com'/docs'/securesc'/apic76cj8gku48a9ogarn9khpku0s46q'/gc9ck66nc600ebdk0ak9f58gkthnnacm'/1386345600000'/08295678552528470768'/15372451357194678536'/0B6ahcE1NPpIaMTJzcFRLMnNiYU0?h=16653014193614665626&e=download&gd=true","editable":true,"embedLink":"https:'/'/video.google.com'/get_player?ps=docs&partnerid=30&docid=0B6ahcE1NPpIaMTJzcFRLMnNiYU0&BASE_URL=https:'/'/docs.google.com'/","etag":"'"maCRbN5nR56FjQPLihEiz9nzpho'/MTM4NjI3NzUwNDU1MA'"","explicitlyTrashed":null,"exportLinks":null,"fileExtension":"mp4","fileSize":"788456","headRevisionId":"0B6ahcE1NPpIaeVJ5b3RxdExvamlDdWNrcGFvWXdvaWg3VU5JPQ","iconLink":"https:'/'/ssl.gstatic.com'/docs'/doclist'/images'/icon_11_video_list.png","id":"0B6ahcE1NPpIaMTJzcFRLMnNiYU0","imageMediaMetadata":null,"indexableText":null,"kind":"drive#file","labels":{"hidden":false,"restricted":false,"starred":false,"trashed":false,"viewed":false},"lastModifyingUser":{"displayName":"C. David Young","isAuthenticatedUser":false,"kind":"drive#user","permissionId":"08295678552528470768","picture":{"url":"https:'/'/lh6.googleusercontent.com'/-tjPzr0pfd_4'/AAAAAAAAAAI'/AAAAAAAAAWc'/DZtQHXrxkgQ'/s64'/photo.jpg"}},"lastModifyingUserName":"C. David Young","lastViewedByMeDate":null,"md5Checksum":"00701d2dd7a1b99e8ebb68cf62305b0d","mimeType":"application'/video","modifiedByMeDate":null,"modifiedDate":"2013-12-05T21:05:04.550Z","openWithLinks":null,"originalFilename":"charlesdavidyoung@gmail.com@test.mp4","ownerNames":["C. David Young"],"owners":[{"displayName":"C. David Young","isAuthenticatedUser":false,"kind":"drive#user","permissionId":"08295678552528470768","picture":{"url":"https:'/'/lh6.googleusercontent.com'/-tjPzr0pfd_4'/AAAAAAAAAAI'/AAAAAAAAAWc'/DZtQHXrxkgQ'/s64'/photo.jpg"}}],"parents":[{"id":"0B6ahcE1NPpIaQlMyWFBPamZIeTA","isRoot":false,"kind":"drive#parentReference","parentLink":"https:'/'/www.googleapis.com'/drive'/v2'/files'/0B6ahcE1NPpIaQlMyWFBPamZIeTA","selfLink":"https:'/'/www.googleapis.com'/drive'/v2'/files'/0B6ahcE1NPpIaMTJzcFRLMnNiYU0'/parents'/0B6ahcE1NPpIaQlMyWFBPamZIeTA"}],"quotaBytesUsed":"788456","selfLink":"https:'/'/www.googleapis.com'/drive'/v2'/files'/0B6ahcE1NPpIaMTJzcFRLMnNiYU0","shared":true,"sharedWithMeDate":null,"thumbnail":null,"thumbnailLink":"https:'/'/lh4.googleusercontent.com'/Ja2DZ3vzXSTn7kY_xY8VmC2N7nCeBrbvnkr-lO4GbMuOmpETwLAYlaC7qr5fOpBTTQ=s220","title":"charlesdavidyoung@gmail.com@test.mp4","userPermission":{"additionalRoles":null,"authKey":null,"etag":"'"maCRbN5nR56FjQPLihEiz9nzpho'/8jq33BWTNkV1S9Wn1TTkFYEVpVc'"","id":"me","kind":"drive#permission","name":null,"photoLink":null,"role":"writer","selfLink":"https:'/'/www.googleapis.com'/drive'/v2'/files'/0B6ahcE1NPpIaMTJzcFRLMnNiYU0'/permissions'/me","type":"user","value":null,"withLink":null},"webContentLink":"https:'/'/docs.google.com'/uc?id=0B6ahcE1NPpIaMTJzcFRLMnNiYU0&export=download","webViewLink":null,"writersCanShare":true,"copyable":true,"content":null}

如果您向脚本传递文档id,它应该在字段exportLinks下返回下载链接。事实上,您的exportLinksnull,这意味着您无法下载该文件(权限?)。