【问题标题】:Google Drive API - PHP Client Library - setting uploadType to resumable uploadGoogle Drive API - PHP 客户端库 - 将 uploadType 设置为可恢复上传
【发布时间】:2012-11-16 08:38:14
【问题描述】:

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

你到底是如何将uploadType 设置为“resumable”而不是默认的“simple”的。我已经在图书馆中搜索了一种方法来做到这一点,但它似乎不存在。他们唯一的提示是他们的示例上传页面上的代码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...???

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

【问题讨论】:

  • 如果谷歌只有一组类似于 jQuery 文档库的文档,那么所有这些类型的问题都可以避免。如果他们至少有一个在线某个地方按类排序的 php 客户端库中所有可用方法的列表,那就太棒了。有这样的东西吗?

标签: php google-drive-api google-api-php-client


【解决方案1】:

以下示例适用于最新版本的 Google APIs 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);
}

【讨论】:

  • 这非常有效,并且当另一个开发人员使用导致内存问题的简单文件上传器实现时非常有用。这种实现允许我们使用的内存永远不会超过 700MB,即使同时上传 10 个 2-5GB 的内存也是如此。太棒了!
【解决方案2】:

这可能是一个较新的参考,但这里是 Google 对这个问题的官方看法: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);

【讨论】:

  • 您好 Andrew,我正在使用此代码,但我不知道应该如何请求获取块状态以处理恢复中断的上传并显示进度,此代码仅在整个文件已上传。
  • 嘿@RamtinGh;据我所知,Google_Http_MediaFileUpload 类为您处理所有这些; nextChunk() 函数的源代码似乎根据来自服务器的 HTTP 响应查看是否已经上传了完整的块,如果是,它会跳到下一个块,直到遇到需要上传的块:@987654322 @
  • 感谢@Andrew,我知道客户端库会处理这个问题,但是使用此代码,除非上传整个文件,否则无法获取块状态,该类使用它来查看是否上传完成了。要恢复中断的上传,我需要发出请求以查看发送了多少字节,库本身不会这样做,如果连接中断,脚本将停止执行,我不知道如何恢复。
  • 我发了一个问题,请你看看:stackoverflow.com/questions/32379007/…
猜你喜欢
  • 1970-01-01
  • 2012-08-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-18
  • 2018-07-11
  • 2016-06-17
  • 2021-11-30
相关资源
最近更新 更多