Например: я могу скопировать только один файл, но мне нужна возможность скопировать несколько файлов, потому что у меня проблемы со скоростью работы приложения.
$fileMimeType = $this->service->files->get($googleFileId, array('fields' => 'mimeType'));
$metadata = new \Google_Service_Drive_DriveFile(
array(
'name' => uniqid(),
'uploadType' => 'multipart',
'mimeType' => isset($fileMimeType->mimeType) ? $fileMimeType->mimeType : false
)
);
$file = $this->service->files->copy($googleFileId, $metadata, array('fields' => 'id'));






Вот пример из документации google-api-client:
<?php
$client = new Google_Client();
$client->setApplicationName("Client_Library_Examples");
// Warn if the API key isn't set.
if (!$apiKey = getApiKey()) {
echo missingApiKeyWarning();
return;
}
$client->setDeveloperKey($apiKey);
$service = new Google_Service_Books($client);
$client->setUseBatch(true);
$batch = $service->createBatch();
$optParams = array('filter' => 'free-ebooks');
$req1 = $service->volumes->listVolumes('Henry David Thoreau', $optParams);
$batch->add($req1, "thoreau");
$req2 = $service->volumes->listVolumes('George Bernard Shaw', $optParams);
$batch->add($req2, "shaw");
$results = $batch->execute();
В вашем случае, я думаю, это будет выглядеть так:
P.S. Попробуйте включить пакетную обработку при создании клиента, как показано выше ($client->setUseBatch(true);)
$batch = $this->service->createBatch();
$fileMimeType = $this->service->files->get($googleFileId, array('fields' => 'mimeType'));
$metadata = new \Google_Service_Drive_DriveFile(
array(
'name' => uniqid(),
'uploadType' => 'multipart',
'mimeType' => isset($fileMimeType->mimeType) ? $fileMimeType->mimeType : false
)
);
$fileCopy = $this->service->files->copy($googleFileId, $metadata, array('fields' => 'id'));
$batch->add($fileCopy, "copy");
$results = $batch->execute();
Подробнее здесь
Abdelkarim EL AMEL: Спасибо, вы перенаправляете меня к одному из решений :)
Мы не можем использовать этот код:
$fileCopy = $this->service->files->copy($googleFileId, $metadata, array('fields' => 'id'));
$batch->add($fileCopy, "copy");
Поскольку метод $batch->add принимает Psr\Http\Message\RequestInterface, $this->service->files->copy возвращает Google_Service_Drive_DriveFile
Но мы можем создать GuzzleHttp\Psr7\Request так же, как копию метода создания из $this->service->files->copy.
Этот код решил мою проблему:
private function copyFileRequest($googleFileId)
{
// build the service uri
$url = $this->service->files->createRequestUri('files/{fileId}/copy', [
'fileId' => [
'location' => 'path',
'type' => 'string',
'value' => $googleFileId,
],
]);
$request = new Request('POST', $url, ['content-type' => 'application/json']);
return $request;
}
public function batchCopy()
{
$googleFileIdFirst = 'First file';
$googleFileIdSecond = 'Second file';
$batch = $this->service->createBatch();
$batch->add($this->copyFileRequest($googleFileIdFirst));
$batch->add($this->copyFileRequest($googleFileIdSecond));
$results = $batch->execute();
/** @var Response $result */
foreach ($results as $result) {
/** @var Stream $body */
$body = (string)$result->getBody(); // Returned json body with copy file
}
}
В противном случае мы можем установить defer как истинное для Google_Client
$client = new \Google_Client();
$client->useApplicationDefaultCredentials();
$client->setScopes([\Google_Service_Drive::DRIVE]);
// Calls should returned request not be executed
$client->setDefer(true)
$service = new \Google_Service_Drive($this->client);
И все методы из сервиса возвращаются Psr\Http\Message\RequestInterface
$batch = $service->createBatch();
$googleServiceDriveFile = new \Google_Service_Drive_DriveFile(['name' => uniqid()]);
$request = $service->files->copy($googleFileId, $googleServiceDriveFile, ['fields' => 'id']);
$batch->add($request);
$results = $batch->execute();
Спасибо, вы перенаправляете меня к одному из решений :)