请求并处理可​​恢复上传的响应:Google Drive api [php] [英] Make requests and handle responses for resumable upload: Google Drive api [php]

查看:347
本文介绍了请求并处理可​​恢复上传的响应:Google Drive api [php]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP API(Beta)的API客户端库来使用谷歌驱动器api,到目前为止,我可以授权和上传文件在chuncks。
根据文档,应采取以下三个步骤来上传文件:
$ b



  • 开始一个可恢复的会话

  • 保存可恢复的会话URI 。

  • 上传档案。


客户端库处理。
同样,根据文档,如果我想显示进度或恢复中断上传,或处理错误,我需要捕获的响应,也可以发送这样的请求:

pre $ > PUT {session_uri} HTTP / 1.1 Content-Length:0 Content-Range:bytes
> * / 2000000

但我不知道应该如何提出这样的要求,我可以在哪里得到回应从我用来上传的php代码,就像任何其他的PHP代码,只有当它完成执行,这是上传完成时返回值。
这里是我用来上传文件的功能(Resumable):
$ b $ pre $ function $ uploadFile $ service $客户端,$ filetoUpload,$ parentId){
$ file = new Google_Service_Drive_DriveFile();
$ file-> title = $ filetoUpload ['name'];
$ chunkSizeBytes = 1 * 1024 * 1024;

//设置父文件夹。
if($ parentId!= null){
$ parent = new Google_Service_Drive_ParentReference();
$ parent-> setId($ parentId);
$ file-> setParents(array($ parent));
}

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

//创建媒体文件上传来表示我们的上传过程。
$ media = new Google_Http_MediaFileUpload(
$ client,
$ request,
$ filetoUpload ['type'],
null,
true,
$ chunkSizeBytes
);
$ media-> setFileSize(filesize($ filetoUpload ['tmp_name']));

//上传各种块。 $状态将是假的,直到进程是
//完成。
$ status = false;
$ handle = fopen($ filetoUpload ['tmp_name'],rb); (!$ status&&!feof($ handle)){
set_time_limit(120);


$ chunk = fread($ handle,$ chunkSizeBytes);
$ status = $ media-> nextChunk($ chunk);
}

// $ status的最终值将是来自已经上传的对象
//的API的数据。
$ result = false;
if($ status!= false){
$ result = $ status;
set_time_limit(30);
echo< pre>;
print_r($ result);
}

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



$ b我应该做一个单独的php文件来处理这些请求吗?
如果是这样的话,应该怎么知道我请求的是哪个文件的状态?
谢谢。

解决方案

引人注意的是,客户端API不支持恢复上传。
请参阅Github上的此问题,其中要求为了解决这个问题当然,应该很容易修改这个类(见下面),并创建一个Pull请求来支持在session-URL提供时恢复已有的上传。



但是,在上传块之后,还可以轻松获得进度。
在你的例子中 Google_Http_MediaFileUpload -object( $ media )有一个名为getProgress的公共方法可以随时调用。
(请查看

因此,您可以修改您的源代码,如下所示在每个块之后输出进度:

$ p $ function uploadFile($ service,$ client,$ filetoUpload,$ parentId,$ progressPrecision = 1){
$ file = new Google_Service_Drive_DriveFile();
$ file-> title = $ filetoUpload ['name'];
$ filesize = filesize($ filetoUpload ['tmp_name']);
//最小块大小需要为256K
$ chunkSizeBytes = min($ filesize / 100 * $ progressPrecision,262144);

//设置父文件夹。
if($ parentId!= null){
$ parent = new Google_Service_Drive_ParentReference();
$ parent-> setId($ parentId);
$ file-> setParents(array($ parent));
}

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

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

...

  while(!$ status&&!feof($ handle)){
set_time_limit(120);
$ chunk = fread($ handle,$ chunkSizeBytes);
$ status = $ media-> nextChunk($ chunk);
if(!$ status){// nextChunk()在上传过程中仍然返回'false'
echo'成功上传文件到字节'。 $ media-> getProgress()。
'就是'。 ($ media-> getProgress()/ $ chunkSizeBytes)。 '整个文件的百分比';




$ b希望这会有所帮助。我会看看,如果我可以找到一些时间来添加简历支持客户端库。编辑:根据
$ b 编辑: google.com/storage/docs/concepts-techniquesrel =nofollow>这个文档,大块需要至少256KB大。更改了代码。



EDIT2:我刚刚添加了拉取请求添加简历功能。如果被拒绝,你仍然可以决定是否可以修改/扩展客户端。如果它被接受,只需在数据库中存储 $ media-> getResumeUri()的返回值,稍后调用 $ media-> ; resume($ prior_stored_return_value)在实例化后恢复进程。


I'm using the API Client Library for PHP (Beta) to work with google drive api, So far I can authorize and and upload a file in chuncks. According to the documentation, these three steps should be taken to upload a file:

  • Start a resumable session.
  • Save the resumable session URI.
  • Upload the file.

Which I think the Client Library handles. Again, according to the documentation, if I want to show the progress or resume an interrupted upload, or to handle errors I need to capture the response and also be able to send requests like this:

> PUT {session_uri} HTTP/1.1 Content-Length: 0 Content-Range: bytes
> */2000000

But I have no idea how should I make such request and where can I get the response from, the php code I'm using to upload,like any other php code, only returns values when it is done executing, which is when the upload is done. here is the function I'm using to upload files (Resumable):

function uploadFile($service,$client,$filetoUpload,$parentId){
    $file = new Google_Service_Drive_DriveFile();
    $file->title = $filetoUpload['name'];
    $chunkSizeBytes = 1 * 1024 * 1024;

    // Set the parent folder.
      if ($parentId != null) {
        $parent = new Google_Service_Drive_ParentReference();
        $parent->setId($parentId);
        $file->setParents(array($parent));
      }

    // 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,
      $filetoUpload['type'],
      null,
      true,
      $chunkSizeBytes
    );
    $media->setFileSize(filesize($filetoUpload['tmp_name']));

    // Upload the various chunks. $status will be false until the process is
    // complete.
    $status = false;
    $handle = fopen($filetoUpload['tmp_name'], "rb");

    while (!$status && !feof($handle)) {
      set_time_limit(120);    
      $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;
      set_time_limit(30);
      echo "<pre>";
      print_r($result);
    }

    fclose($handle);
    // Reset to the client to execute requests immediately in the future.
    $client->setDefer(false);
}

Should i make a separate php file to handle these requests? if so, How should tell that which file's status I'm requesting for? Thanks.

解决方案

Apperantly, the BETA Client API simply doesn't support resuming Uploads. Please see this issue on Github, which asks for fixing this. Of course it should be easy to modify the class (see below) and create a Pull-request to enable support for resuming existing uploads when the session-URL is supplied.

However, there's an easy way to get the progress after an chunk has been uploaded. The Google_Http_MediaFileUpload-object ($media in your example) has a public method called 'getProgress' which can be called anytime. (Please have a look at the source code of the API-client-library).

To get the upload status, I'd add a parameter to modify the progress precision by adjusting the chunk size. Since the more chunks are used, the more protocol overhead is generated, setting the precision as low as possible should be avoided.

Therefore, you could modify your source code as below to output the progress after each chunk:

function uploadFile($service,$client,$filetoUpload,$parentId,$progressPrecision = 1){
$file = new Google_Service_Drive_DriveFile();
$file->title = $filetoUpload['name'];
$filesize = filesize($filetoUpload['tmp_name']);
//minimum chunk size needs to be 256K
$chunkSizeBytes = min( $filesize / 100 * $progressPrecision, 262144);

// Set the parent folder.
  if ($parentId != null) {
    $parent = new Google_Service_Drive_ParentReference();
    $parent->setId($parentId);
    $file->setParents(array($parent));
  }

// 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,
  $filetoUpload['type'],
  null,
  true,
  $chunkSizeBytes
);
$media->setFileSize($filesize);

while (!$status && !feof($handle)) {
  set_time_limit(120);    
  $chunk = fread($handle, $chunkSizeBytes);
  $status = $media->nextChunk($chunk);
  if(!$status){ //nextChunk() returns 'false' whenever the upload is still in progress
   echo 'sucessfully uploaded file up to byte ' . $media->getProgress() . 
   ' which is ' . ( $media->getProgress() / $chunkSizeBytes ) . '% of the whole file';
  }
 }

Hope this helps. I'll see if I can find some time to add resume-support to the client Library.

EDIT: according to this doc, the chunks need to be at least 256KB big. Changed in code.

EDIT2: I just added a Pull request To add the Resume-Feature. If it gets rejected you could still decide whether it would be ok for you to modify/extend the client. If it gets accepted, just use store the return value of $media->getResumeUri() in a database, and later call $media->resume($previously_stored_return_value) after instantiation to resume the process.

这篇关于请求并处理可​​恢复上传的响应:Google Drive api [php]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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