例如:我只能复制一个文件,但由于应用程序速度存在问题,因此我需要机会复制几个文件。
$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'));
答案 0 :(得分:0)
以下是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();
更多详细信息here
答案 1 :(得分:0)
阿卜杜勒·卡里姆·阿梅尔(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
}
}
答案 2 :(得分:0)
通过其他方式,我们可以将defer
的{{1}}设置为true
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