10

我在使用新的 google drive API 客户端库的文档时遇到了严重问题。看起来这应该很容易回答,而不必将其放在 stackoverflow 上。我正在认真考虑在这个上推出我自己的,一个“正常工作”的 64 页库到目前为止是一个“令人头疼的问题”

您如何将uploadType 设置为“可恢复”而不是默认的“简单”。我已经在图书馆中搜索了一种方法来做到这一点,但它似乎不存在。他们唯一的提示是他们的示例上传页面上的代码https://developers.google.com/drive/quickstart-php

//Insert a file
$file = new Google_DriveFile();
$file->setTitle('My document');
$file->setDescription('A test document');
$file->setMimeType('text/plain');

$data = file_get_contents('document.txt');

$createdFile = $service->files->insert($file, array(
      'data' => $data,
      'mimeType' => 'text/plain',
    ));

这里没有设置uploadType ...???

他们在另一个页面上的文档只是将 uploadType 作为地址的一部分显示为 GET:https?uploadType=resumable ://www.googleapis.com/upload/drive/v2/files但是当您使用时$service->files->insert,库会设置地址。

4

2 回答 2

6

以下示例适用于最新版本的 Google API PHP 客户端 ( https://code.google.com/p/google-api-php-client/source/checkout )

if ($client->getAccessToken()) {
  $filePath = "path/to/foo.txt";
  $chunkSizeBytes = 1 * 1024 * 1024;

  $file = new Google_DriveFile();
  $file->setTitle('My document');
  $file->setDescription('A test document');
  $file->setMimeType('text/plain');

  $media = new Google_MediaFileUpload('text/plain', null, true, $chunkSizeBytes);
  $media->setFileSize(filesize($filePath));

  $result = $service->files->insert($file, array('mediaUpload' => $media));

  $status = false;
  $handle = fopen($filePath, "rb");
  while (!$status && !feof($handle)) {
    $chunk = fread($handle, $chunkSizeBytes);
    $uploadStatus = $media->nextChunk($result, $chunk);
  }

  fclose($handle);
}
于 2013-02-04T19:20:00.533 回答
6

这可能是一个较新的参考,但这里是谷歌对这个问题的官方看法:https ://developers.google.com/api-client-library/php/guide/media_upload

来自文章:

可恢复文件上传

也可以将上传拆分为多个请求。这对于较大的文件很方便,并且可以在出现问题时恢复上传。可恢复上传可以与单独的元数据一起发送。

$file = new Google_Service_Drive_DriveFile();
$file->title = "Big File";
$chunkSizeBytes = 1 * 1024 * 1024;

// Call the API with the media upload, defer so it doesn't immediately return.
$client->setDefer(true);
$request = $service->files->insert($file);

// Create a media file upload to represent our upload process.
$media = new Google_Http_MediaFileUpload(
  $client,
  $request,
  'text/plain',
  null,
  true,
  $chunkSizeBytes
);
$media->setFileSize(filesize("path/to/file"));

// Upload the various chunks. $status will be false until the process is
// complete.
$status = false;
$handle = fopen("path/to/file", "rb");
while (!$status && !feof($handle)) {
  $chunk = fread($handle, $chunkSizeBytes);
  $status = $media->nextChunk($chunk);
 }

// The final value of $status will be the data from the API for the object
// that has been uploaded.
$result = false;
if($status != false) {
  $result = $status;
}

fclose($handle);
// Reset to the client to execute requests immediately in the future.
$client->setDefer(false);
于 2014-07-08T17:38:13.050 回答