使用PHP客户端库将大文件上传到Google云端硬盘

时间:2012-12-10 20:17:47

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

我正在尝试通过其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();
// 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);
?>

1 个答案:

答案 0 :(得分:0)

看起来这已经得到了解答?将文件作为流读取,一次一个块,并将其添加到缓冲区/直接写入套接字:

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

相关问题