我创建了Google服务帐户,并成功将我的应用与谷歌硬盘连接。
$client = new Google_Client();
$credentialsFile = 'PATH TO JSON CREDENTIALS';
if (!file_exists($credentialsFile)) {
throw new RuntimeException('Service account credentials Not Found!');
}
$client->setAuthConfig($credentialsFile);
$client->setApplicationName("Reflektions Drive");
$client->setScopes(Google_Service_Drive::DRIVE);
return new Google_Service_Drive($client);
我在将视频上传到Google云端硬盘时遇到问题。由于视频大小很大,我得到最大执行错误。
$driveService = drive_get_service();
$file = new Google_Service_Drive_DriveFile();
$file->setName('vid.mp4');
$file->setDescription('A test document');
$file->setMimeType('video/mp4');
$data = file_get_contents(base_url('vid.mp4'));
$createdFile = $driveService->files->create($file, array(
'data' => $data,
'mimeType' => 'video/mp4',
'uploadType' => 'multipart'
));
echo "<pre>";
print_r($createdFile);
使用相同的代码我已成功上传图像文件。并且我也无法在浏览器中显示图像。 所以我的问题是:
以下是我尝试过的可恢复上传的片段:
我尝试了可恢复的上传
function upload(){
DEFINE("TESTFILE", 'C:\xampp\htdocs\project\vid.mp4');
$driveService = drive_get_service();
$file = new Google_Service_Drive_DriveFile();
$file->name = "Big File";
$chunkSizeBytes = 1 * 1024 * 1024;
$request = $driveService->files->create($file);
$media = new Google_Http_MediaFileUpload(
$driveService->getClient(),
$request,
'video/mp4',
null,
true,
$chunkSizeBytes
);
$media->setFileSize(filesize(TESTFILE));
$status = false;
$handle = fopen(TESTFILE, "rb");
while (!$status && !feof($handle)) {
$chunk = $this->readVideoChunk($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
$result = false;
if ($status != false) {
$result = $status;
}
fclose($handle);
echo $result;
echo "<pre>";
print_r($file);
}
function readVideoChunk($handle, $chunkSize)
{
$byteCount = 0;
$giantChunk = "";
while (!feof($handle)) {
$chunk = fread($handle, 8192);
$byteCount += strlen($chunk);
$giantChunk .= $chunk;
if ($byteCount >= $chunkSize) {
return $giantChunk;
}
}
return $giantChunk;
}
我的最大执行时间超过了30秒。