问题
Batch upload fails using Google Drive V3 with master branch of API (v2.0).
I've modified https://github.com/google/google-api-php-client/blob/master/examples/batch.php with service account credentials.
The code:
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);
}
The code above will fail with "Not Found" after running last $results = $batch->execute(); (folder Invoices will be created successfully).
With $useBatch = false
everything works as expected - a folder is created with three files in it.
Why is it crashing on batch upload?
Thanks!
回答1:
Currently, Google Drive does not support batch operations for media, either for upload or download
Source: https://developers.google.com/drive/api/v3/batch
回答2:
Based on the Official Google Documentation, you received '404 File not found' due to user does not have read access to a file or the file does not exist. Suggested action: Report to users that they do not have read access to the file or that the file does not exist. Tell them that they should ask the owner for permission to the file.
You have to include your '$fileId' in your request. Also, you should set '$userPermission' if '$useBatch=true'.
Note: You should use the v1-branch as it is stated here: https://github.com/google/google-api-php-client
来源:https://stackoverflow.com/questions/35532402/google-drive-v3-google-api-client-2-0-batch-upload-is-failing