Google Drive API - PHP客户端库 - 将uploadType设置为可恢复的上传 [英] Google Drive API - PHP Client Library - setting uploadType to resumable upload

查看:123
本文介绍了Google Drive API - PHP客户端库 - 将uploadType设置为可恢复的上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对新的Google Drive API客户端库的文档存在严重问题。看来这应该是一个很容易回答的问题,而不必把它放在stackoverflow上。我正在认真考虑在自己的作品中翻阅自己的作品,一个64页的刚刚起作用的图书馆到目前为止是一个令人头痛的问题。

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

  //插入文件
$ file = new Google_DriveFile();
$ file-> setTitle('我的文档');
$ file-> setDescription('测试文档');
$ file-> setMimeType('text / plain');

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

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

另一页上的文档仅将uploadType作为地址的一部分显示为GET:< a href =https://www.googleapis.com/upload/drive/v2/files> https://www.googleapis.com/upload/drive/v2/files ?uploadType = resumable 但是当您使用 $ service->文件 - >插入时,库会设置地址。

解决方案

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



从文章: b
$ b


可恢复文件上传



也可以跨多个请求拆分上传。这个
适用于较大的文件,并且如果
有问题则允许恢复上传。可恢复的上传文件可以使用单独的
元数据发送。

  $ file = new Google_Service_Drive_DriveFile(); 
$ file-> title =大文件;
$ chunkSizeBytes = 1 * 1024 * 1024;

//通过媒体上传来调用API,推迟它不会立即返回。
$ client-> setDefer(true);
$ request = $ service-> files-> insert($ file);

//创建媒体文件上传来表示我们的上传过程。
$ media = new Google_Http_MediaFileUpload(
$ client,
$ request,
text / plain',
null,
true,
$ chunkSizeBytes
);
$ media-> setFileSize(filesize(path / to / file));

//上传各种块。在进程是
//完成之前,$状态将为false。
$ status = false;
$ handle = fopen(path / to / file,rb);
while(!$ status&&!feof($ handle)){
$ chunk = fread($ handle,$ chunkSizeBytes);
$ status = $ media-> nextChunk($ chunk);
}

// $ status的最终值将是来自已上传对象
//的API的数据。
$ result = false;
if($ status!= false){
$ result = $ status;
}

fclose($ handle);
//重置到客户端以在将来立即执行请求。
$ client-> setDefer(false);



I am having serious issues with the documentation for the new google drive API client library. It seems this should be an easy one to answer without having to put it on stackoverflow. I am seriously considering rolling my own on this one, a 64 page library that "just works" is so far a "total headache"

How the heck do you set the uploadType to "resumable" instead of the default "simple". I have searched the library for a way to do this, but it seems non-existent. Their only hint is the code on their sample uploader page 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',
    ));

Nothing here sets the uploadType...???

Their docs on another page just show uploadType as a part of the address as a GET: https://www.googleapis.com/upload/drive/v2/files?uploadType=resumable but when you use $service->files->insert, the library sets the address.

解决方案

This may be a newer reference, but here is Google's official take on this question: https://developers.google.com/api-client-library/php/guide/media_upload

From the article:

Resumable File Upload

It is also possible to split the upload across multiple requests. This is convenient for larger files, and allows resumption of the upload if there is a problem. Resumable uploads can be sent with separate metadata.

$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);

这篇关于Google Drive API - PHP客户端库 - 将uploadType设置为可恢复的上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆