【问题标题】:Is there a way to check if a file is successfully uploaded with Google Drive API?有没有办法使用 Google Drive API 检查文件是否成功上传?
【发布时间】:2014-12-18 03:41:54
【问题描述】:

我正在使用 PHP sdk。我使用下面的代码将文件插入驱动器。

$file = new Google_DriveFile();
$file->setTitle($title);
$file->setDescription($description);
$file->setMimeType($mimeType);


if ($parentId != null) {
   $parent = new Google_ParentReference();
   $parent->setId($parentId);
   $file->setParents(array($parent));
}

$data = file_get_contents($uplodedFile['file']['tmp_name']);

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

我要做的是知道文件是否成功上传到服务器。这个文件可能非常大,所以一旦完成,我想要通知。有没有办法用 PHP sdk 做到这一点?我无法使用实时 API。

【问题讨论】:

    标签: php google-drive-api


    【解决方案1】:

    如果您阅读他们的API Documentation (insert),您会看到他们将上传的内容包装在try/catch 块中。

    try {
    $data = file_get_contents($filename);
    
    $createdFile = $service->files->insert($file, array(
       'data' => $data,
       'mimeType' => $mimeType,
    ));
    
    // Uncomment the following line to print the File ID
    // print 'File ID: %s' % $createdFile->getId();
    
    return $createdFile;
    } catch (Exception $e) {
        print "An error occurred: " . $e->getMessage();
    }
    

    您会在他们的Response 部分注意到:

    如果成功,此方法会在响应正文中返回一个 Files 资源。

    这意味着您将获得上传的文件作为回报。否则将抛出catch() 异常,如上所示。

    此外,如果您向下滚动,您会注意到,他们有一个您可以使用的 PHP 库,并且在他们的功能中他们声明:

    @return Google_DriveFile The file that was inserted. NULL is returned if an API error occurred.
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 2010-10-26
      • 2021-08-11
      • 1970-01-01
      相关资源
      最近更新 更多