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

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

问题描述

我正在尝试通过他们的 PHP 客户端库使用 Google Drive API 来上传一个大文件.目前它失败了,因为似乎唯一可用的方法是简单"上传方法.不幸的是,这需要您将整个文件作为数据加载,并且它达到了我的 php 内存限制.我想知道它是否可能,并看一个例子来说明它是如何完成的.我知道有一种可恢复和分块上传的方法,但没有关于如何使用它的文档.

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:
$authUrl

";
print "Please enter the auth code:
";
$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 客户端库- 将uploadType设置为可恢复上传

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

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