批量上傳失敗使用谷歌驅動器V3與API的主分支(v2.0)。谷歌驅動V3,谷歌API客戶端2.0 - 批量上傳失敗
我修改了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('[email protected]');
$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);
}
上面的代碼將在運行最後$結果= $分批>執行後失敗,出現 「未找到」(); (文件夾發票將成功創建)。
隨着$useBatch = false
一切都按預期工作 - 一個文件夾中創建三個文件。
爲什麼在批量上傳時崩潰了?
謝謝!
提起與谷歌的錯誤報告:https://github.com/google/google-api-php-client/issues/860 – temuri