使用PHP客户端库将大文件上传到谷歌驱动器 [英] Upload large file to google drive with PHP Client Library

查看:107
本文介绍了使用PHP客户端库将大文件上传到谷歌驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过其PHP客户端库使用Google Drive API上传大文件。目前它失败了,因为唯一可用的方法是简单上传方法。不幸的是,这需要你加载整个文件作为数据,它符合我的PHP内存限制。我想知道是否有可能并看到如何完成的例子。我知道有一种可恢复和分块上传的方法,但没有关于如何使用它的文档。

 < php 
require_once'google-api-php-client / src / Google_Client.php';
require_once'google-api-php-client / src / contrib / Google_DriveService.php';

$ client = new Google_Client();
//从API控制台获取您的凭证
$ client-> setClientId('YOUR_CLIENT_ID');
$ client-> setClientSecret('YOUR_CLIENT_SECRET');
$ client-> setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$ client-> setScopes(array('https://www.googleapis.com/auth/drive'));

$ service = new Google_DriveService($ client);

$ authUrl = $ client-> createAuthUrl();

//请求授权
打印请访问:\ $ $ authUrl\\\
\\\
;
print请输入验证码:\\\
;
$ authCode = trim(fgets(STDIN));

//访问令牌的交换授权码
$ accessToken = $ client-> authenticate($ authCode);
$ client-> setAccessToken($ accessToken);

//插入文件
$ file = new Google_DriveFile();
$ file-> setTitle('我的文档');
$ file-> setDescription('测试文档');
$ file-> setMimeType('text / plain');

$ data = file_get_contents('document.txt');
$ b $ createdFile = $ service-> files-> insert($ file,array(
'data'=> $ data,
'mimeType'=> 'text / plain',
));

print_r($ createdFile);
?>


解决方案

看起来这已经被回答了吗?以文件流的形式读取文件,每次一个文件块并将其添加到缓冲区中/直接写入套接字:

Google云端硬盘API - PHP客户端库 - 将uploadType设置为可继续上传


I am trying to use the Google Drive API via their PHP Client Library to upload a large file. Currently it fails because the only method seemingly available is the "simple" upload method. Unfortunately that requires you to load the entire file as data and it hits my php memory limit. I would like to know if it is possible and see an example of how it is done. I know there is a method for resumable and chuncked uploads, but there is no documentation on how to use it.

<?php
require_once 'google-api-php-client/src/Google_Client.php';
require_once 'google-api-php-client/src/contrib/Google_DriveService.php';

$client = new Google_Client();
// Get your credentials from the APIs Console
$client->setClientId('YOUR_CLIENT_ID');
$client->setClientSecret('YOUR_CLIENT_SECRET');
$client->setRedirectUri('urn:ietf:wg:oauth:2.0:oob');
$client->setScopes(array('https://www.googleapis.com/auth/drive'));

$service = new Google_DriveService($client);

$authUrl = $client->createAuthUrl();

//Request authorization
print "Please visit:\n$authUrl\n\n";
print "Please enter the auth code:\n";
$authCode = trim(fgets(STDIN));

// Exchange authorization code for access token
$accessToken = $client->authenticate($authCode);
$client->setAccessToken($accessToken);

//Insert a file
$file = new Google_DriveFile();
$file->setTitle('My document');
$file->setDescription('A test document');
$file->setMimeType('text/plain');

$data = file_get_contents('document.txt');

$createdFile = $service->files->insert($file, array(
      'data' => $data,
      'mimeType' => 'text/plain',
    ));

print_r($createdFile);
?>

解决方案

Looks like this has been answered already? Read the file as a stream, one chunk at a time and add that to a buffer/write it straight to the socket:

Google Drive API - PHP Client Library - setting uploadType to resumable upload

这篇关于使用PHP客户端库将大文件上传到谷歌驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆