【发布时间】: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 一切正常 - 创建一个文件夹,其中包含三个文件。
为什么批量上传会崩溃?
谢谢!
【问题讨论】:
-
向 Google 提交了错误报告:github.com/google/google-api-php-client/issues/860
标签: php google-api google-drive-api google-api-php-client