简体   繁体   English

使用google-api-php-client不再进行文件更新

[英]Files updates no longer works using the google-api-php-client

I have a very small script that uploads and / or update files (wrote this about 1 year ago, borrowed 80% of the lines from the examples) 我有一个非常小的脚本,可以上传和/或更新文件(大约1年前编写,从示例中借用了80%的代码)

No major changes in the code since march (using 1.0.0-alpha) but mid-may the file updates stopped working, raising an Internal Server Error , i upgraded to 1.0.4-beta with no success : 自3月以来(使用1.0.0-alpha)以来,代码没有重大变化,但可能在5月中旬文件更新停止工作,并引发了内部服务器错误,我升级到1.0.4-beta,但未成功:

PHP Fatal error:  Uncaught exception 'Google_Service_Exception' with message 'Error calling PUT https://www.googleapis.com/upload/drive/v2/ [...] (500) Internal Error' in 
google-api-php-client-1.0.4-beta/src/Google/Http/REST.php:80

code: 码:

$client->setDefer(true);
$request = $service->files->update($update_id,$file);

$media = new Google_Http_MediaFileUpload(
    $client,
    $request,
    'text/plain',
    null,
    true,
    $chunkSizeBytes
);
$media->setFileSize(filesize($csvfile))
$status = false;
$handle = fopen($csvfile, "rb");
while (!$status && !feof($handle)) {
    $chunk = fread($handle, $chunkSizeBytes);
    $status = $media->nextChunk($chunk);
}

File inserts (HTTP POST) are still working (using the same code for uploading the chunks) 文件插入(HTTP POST)仍在工作(使用相同的代码上传块)

any ideas ? 有任何想法吗 ?

I can update a file ( previously created or copied from a template ) with this code : 我可以使用以下代码更新文件(先前创建或从模板复制的文件):

     (...)
     require_once 'src/Google/Client.php';
     require_once 'src/Google/Service/Oauth2.php';
     require_once 'src/Google/Service/Drive.php';    
     (...)

     $client = new Google_Client();
     // set scopes ...
     (...)

     $GDrive_service = new Google_Service_Drive($client);
     (...)


     // then I set parameters 
        (...)
        $newTitle = $title ;
        $newDescription = $description ;
        $newMimeType = 'text/csv' ;
        $newFileName = $filename ;
        $service = $GDrive_service ;
        (...)



$updatedFile = updateFile($service, $fileId, $newTitle, $newDescription, $newMimeType, $newFileName, $newRevision) ;


function updateFile($service, $fileId, $newTitle, $newDescription, $newMimeType, $newFileName, $newRevision) {
  try {
    // First retrieve the file from the API.
    $file = $service->files->get($fileId);

    // File's new metadata.
    $file->setTitle($newTitle);
    $file->setDescription($newDescription);
    $file->setMimeType($newMimeType);

    // File's new content.
    $data = file_get_contents($newFileName);
    $convert = 'true' ;

    $additionalParams = array(
      'uploadType' => 'multipart',
      'newRevision' => $newRevision,
      'data' => $data,
      'mimeType' => $newMimeType,
      'convert' => $convert,
    );

    // Send the request to the API.
    $updatedFile = $service->files->update($fileId, $file, $additionalParams);
    return $updatedFile;
  } catch (Exception $e) {
    print "An error occurred: " . $e->getMessage();
  }
}


(...)
// this is info of the updated file 
$fileId = $updatedFile->getId() ;
// link of file
$cF_link = $updatedFile->alternateLink ;
// pdf version
$enllacos = $updatedFile->exportLinks ;
$cF_PDF_link = $enllacos['application/pdf'] ;
(...)

This code is working with the 1.0.5-beta php client 此代码与1.0.5-beta PHP客户端一起使用

Sergi 塞尔吉

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM