【问题标题】:Unable to upload multiple files to Google drive using PHP SDK无法使用 PHP SDK 将多个文件上传到 Google 驱动器
【发布时间】:2013-12-02 02:21:30
【问题描述】:

我正在尝试使用 PHP SDK 将多个文件上传到 Google 驱动器。为此,我迭代地调用下面的函数并传递所需的参数:

function insertFile($driveService, $title, $description, $parentId, $fileUrl) {
    global $header;

    $file = new Google_DriveFile();
    $file->setTitle($title);
    $file->setDescription($description);
    $mimeType= "application/vnd.google-apps.folder";

    if ($fileUrl != null) {
      $fileUrl = replaceSpaceWithHtmlCode($fileUrl);
      $header = getUrlHeader($fileUrl);
      $mimeType = $header['content-type'];
    }
    $file->setMimeType($mimeType);
    $parent = new Google_ParentReference();
    // Set the parent folder.
    if ($parentId != null) {
      $parent->setId($parentId);
      $file->setParents(array($parent));
    }

    try {
      $data = null;
      if ($fileUrl != null) {
        if (hasErrors($driveService, $fileUrl) == True) {
          return null;
        }
        $data = file_get_contents($fileUrl);
    }

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

      return $createdFile;
    } catch (Exception $e) {
        echo "Error: 12";
    return null;
    }
}

我在 Google App Engine 上运行这个应用程序。

但是,我无法上传我传递给它的所有文件。例如,如果我传递大约 12-15 个文件,则只有 10-11 个文件被上传,有时所有文件都被上传,即使所有参数都正确。当它无法创建文件时,我发现了异常,这表示它无法为未上传的文件创建文件。我在应用引擎的日志中没有看到任何警告或错误。

我错过了什么吗?有人可以指出我应该在哪里更正此问题并使其足够可靠以上传给它的所有文件吗?

我尝试上传 30 个文件时收到的 HTTP 响应是这样的:

PHP Fatal error:  The request was aborted because it exceeded the maximum execution time

【问题讨论】:

    标签: google-app-engine file-upload google-drive-api


    【解决方案1】:

    检查 http 响应以查看详细原因。可能是您正在达到油门限制并获得 403 速率限制响应。

    【讨论】:

    • 日志中似乎没有任何内容表明已达到油门限制,如果问题是油门限制,我不确定最后几个文件是否会出现这种情况每次都不上传,给5个就上传4个,给15个就上传13个。
    • 限制使用令牌桶工作。在令牌用尽之前,一系列插入工作正常,根据我的经验,大约 20 个。之后,代币似乎以大约每 2 秒一个的速度被补充。任何使用空桶的插入尝试都会导致 403 异常。检查 http 响应。
    • 这看起来像是一个 http 超时。你能改变超时吗?我还建议 (1) 查看实际的 http 流量,不要仅仅依赖日志,(2) 尝试将插入速率降低到每 3 秒一次,看看是否可以解决问题。见stackoverflow.com/questions/18529524/…
    • 您能告诉我如何更改超时时间吗?我尝试过每秒 1 次,但没有任何区别,仍然不可靠。
    • 每秒一个太快了。每 3 秒尝试一次,至少要确定这是否是原因。我不知道 PHP - 对不起。我建议提出一个新问题“如何在 appengine php 上设置 urlfetch 的超时时间”。文档说默认是 5s,这太低了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    相关资源
    最近更新 更多