【问题标题】:Google Drive V3, Google API client 2.0 - batch upload is failingGoogle Drive V3,Google API 客户端 2.0 - 批量上传失败
【发布时间】:2016-02-21 04:40:43
【问题描述】:

使用带有 API 主分支 (v2.0) 的 Google Drive V3 批量上传失败。

我已使用服务帐户凭据修改了https://github.com/google/google-api-php-client/blob/master/examples/batch.php

代码:

include_once __DIR__ . '/../vendor/autoload.php';
include_once "templates/base.php";

echo pageHeader("Batching Queries");

// USE TRUE OR FALSE TO TOGGLE BETWEEN BATCHED AND SEQUENTIAL UPLOADS.
$useBatch = true;

$client = new Google_Client();
$client->setScopes([
    'https://www.googleapis.com/auth/drive',
]);
if ($credentials_file = checkServiceAccountCredentialsFile()) {
  // set the location manually
  $client->setAuthConfig($credentials_file);
} elseif (getenv('GOOGLE_APPLICATION_CREDENTIALS')) {
  // use the application default credentials
  $client->useApplicationDefaultCredentials();
} else {
  exit;
}
$client->setSubject('some@email.com');
$service = new Google_Service_Drive($client);
$client->setUseBatch($useBatch);

if ($useBatch) {
    $batch = $service->createBatch();
}

$folder = new Google_Service_Drive_DriveFile([
  'name' => 'Invoices',
  'mimeType' => 'application/vnd.google-apps.folder'
]);

$req = $service->files->create($folder, [
  'fields' => 'id'
]);

if ($useBatch) {
    $result = $batch->add($req, 'newfolder');
    $folder = $batch->execute()['response-newfolder'];
    $newFolderId = $folder->id;
} else {
    $newFolderId = $req->id;
}

$uploadIDs = null;

if ($useBatch) {
    $batch = $service->createBatch();
}

for ($i=1;$i<=3;$i++) {
    $file = new Google_Service_Drive_DriveFile([
        'name' => $i . '.jpg',
        'mimeType' => 'image/jpeg',
        'parents' => [$newFolderId],
    ]);

    $req = $service->files->create($file, [
        'data' => file_get_contents('img/'.$i.'.jpg'),
        'mimeType' => 'image/jpeg',
        'uploadType' => 'media',
        'fields' => 'id',
    ]);

    if ($useBatch) {
        $batch->add($req, $i);
    } else {
        $uploadIDs[] = $req->id;
    }
}

if ($useBatch) {
    $results = $batch->execute();
} else {
    print_r($uploadIDs);
}

运行最后一个 $results = $batch->execute(); 后,上面的代码将失败并显示“未找到” (文件夹发票将被成功创建)。

$useBatch = false 一切正常 - 创建一个文件夹,其中包含三个文件。

为什么批量上传会崩溃?

谢谢!

【问题讨论】:

标签: php google-api google-drive-api google-api-php-client


【解决方案1】:

目前,Google Drive 不支持对媒体进行批量操作, 上传或下载

来源:https://developers.google.com/drive/api/v3/batch

【讨论】:

    【解决方案2】:

    根据Official Google Documentation,您收到“404 File not found”,因为用户没有文件的读取权限或文件不存在。建议的操作:向用户报告他们没有文件的读取权限或文件不存在。告诉他们应该向所有者请求文件的权限。

    您必须在您的request 中包含您的“$fileId”。此外,如果 '$useBatch=true',您应该设置 '$userPermission'。

    注意:您应该使用v1-branch,如下所述:https://github.com/google/google-api-php-client

    【讨论】:

    • 我没有任何“$fileId”。我正在上传文件,而不是修改它们。所以“找不到 404 文件”似乎完全随机且不合适。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-11
    • 2015-02-05
    相关资源
    最近更新 更多