如何将视频上传到Google云端硬盘?

时间:2017-02-24 13:46:44

标签: php file-upload google-api google-drive-api google-api-php-client

我创建了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);

使用相同的代码我已成功上传图像文件。并且我也无法在浏览器中显示图像。 所以我的问题是:

  1. 如何在浏览器中显示上传的图像?
  2. 如何在Google云端硬盘中上传视频(较大的文件)?
  3. 如何在我的应用中嵌入上传的视频?
  4. 以下是我尝试过的可恢复上传的片段:

    我尝试了可恢复的上传

    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秒。

0 个答案:

没有答案