使用PHP API可恢复上传到Google云存储
我可以使用Google PHP API客户端成功地将小文件上传到Google云存储,但无法上传300MB的文件。返回内存错误。 这是我的密码使用PHP API可恢复上传到Google云存储,php,google-cloud-storage,google-api-php-client,resume-upload,Php,Google Cloud Storage,Google Api Php Client,Resume Upload,我可以使用Google PHP API客户端成功地将小文件上传到Google云存储,但无法上传300MB的文件。返回内存错误。 这是我的密码 $storage = new Google_Service_Storage($client); $file_name = "filenameo.zip"; $obj = new Google_Service_Storage_StorageObject(); $obj->setName($file_name); $
$storage = new Google_Service_Storage($client);
$file_name = "filenameo.zip";
$obj = new Google_Service_Storage_StorageObject();
$obj->setName($file_name);
$resp = $storage->objects->insert(
"bucketname",
$obj,
array('name' => $file_name, 'data' => file_get_contents("300mb-file.zip"), 'uploadType' => 'media')
);
我试图将UploadType更改为Resubable。。但是没有运气。请帮忙
更新:使用Http作为答案
接收错误(致命错误:未捕获异常“谷歌IO异常”)
file\u get\u contents
直接将文件内容作为字符串加载到内存中。你的应用程序大概没有额外的300 MB内存可供使用
相反,我建议将文件分块读取并上传Google\u Http\u MediaFileUpload
是一个非常有用的工具。这里有一个很好的例子:
重要的部分是这个bit:
$media = new Google_Http_MediaFileUpload($client, $request, ......, $chunkSizeBytes)
$handle = fopen("path/to/file", "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
让它与下面的代码一起工作
<?php
require_once("google-api/autoload.php");
//require_once("google-api/src/Google/Client.php");
//require_once("google-api/src/Google/Service/Storage.php");
//require_once("google-api/src/Google/Http/MediaFileUpload.php");
session_start();
/**
* Connect to Google Cloud Storage API
*/
$client = new Google_Client();
$client->setApplicationName("ApplicationName");
// $stored_access_token - your cached oauth access token
if( $stored_access_token ) {
$client->setAccessToken( $stored_access_token );
}
$credential = new Google_Auth_AssertionCredentials(
"email-sdfaskjrsd@developer.gserviceaccount.com",
array('https://www.googleapis.com/auth/devstorage.read_write'),
file_get_contents("pathtokey/mykeyhere-7845b2eb92c9.p12")
);
$client->setAssertionCredentials($credential);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($credential);
// Cache the access token however you choose, getting the access token with $client->getAccessToken()
}
$storage = new Google_Service_Storage($client);
if (isset($_GET['code'])) {
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
die('The session state did not match.');
}
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: ' . $redirect);
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
$sfilename = "mfile.zip"; //filename here
$obj = new Google_Service_Storage_StorageObject();
$obj->setName($sfilename);
$obj->setBucket("myBucketS"); //bucket name here
$filen = "pathtofile/uploadthis.zip";
$mimetype = mime_content_type($filen);
$chunkSizeBytes = 1 * 1024 * 1024;
$client->setDefer(true);
$status = false;
$filetoupload = array('name' => $sfilename, 'uploadType' => 'resumable');
$request = $storage->objects->insert("myBucketS",$obj,$filetoupload);
$media = new Google_Http_MediaFileUpload($client, $request, $mimetype, null, true, $chunkSizeBytes);
$media->setFileSize(filesize($filen));
$handle = fopen($filen, "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
$result = false;
if($status != false) {
$result = $status;
}
fclose($handle);
// Reset to the client to execute requests immediately in the future.
$client->setDefer(false);
} else {
// If the user hasn't authorized the app, initiate the OAuth flow
$state = mt_rand();
$client->setState($state);
$_SESSION['state'] = $state;
$authUrl = $client->createAuthUrl();
}
$_SESSION['token'] = $client->getAccessToken();
print_r($status);
?>
对于该错误,除了致命错误:未捕获异常“Google\u IO\u exception”
之外,还有其他错误吗?
<?php
require_once("google-api/autoload.php");
//require_once("google-api/src/Google/Client.php");
//require_once("google-api/src/Google/Service/Storage.php");
//require_once("google-api/src/Google/Http/MediaFileUpload.php");
session_start();
/**
* Connect to Google Cloud Storage API
*/
$client = new Google_Client();
$client->setApplicationName("ApplicationName");
// $stored_access_token - your cached oauth access token
if( $stored_access_token ) {
$client->setAccessToken( $stored_access_token );
}
$credential = new Google_Auth_AssertionCredentials(
"email-sdfaskjrsd@developer.gserviceaccount.com",
array('https://www.googleapis.com/auth/devstorage.read_write'),
file_get_contents("pathtokey/mykeyhere-7845b2eb92c9.p12")
);
$client->setAssertionCredentials($credential);
if($client->getAuth()->isAccessTokenExpired()) {
$client->getAuth()->refreshTokenWithAssertion($credential);
// Cache the access token however you choose, getting the access token with $client->getAccessToken()
}
$storage = new Google_Service_Storage($client);
if (isset($_GET['code'])) {
if (strval($_SESSION['state']) !== strval($_GET['state'])) {
die('The session state did not match.');
}
$client->authenticate($_GET['code']);
$_SESSION['token'] = $client->getAccessToken();
header('Location: ' . $redirect);
}
if (isset($_SESSION['token'])) {
$client->setAccessToken($_SESSION['token']);
}
if ($client->getAccessToken()) {
$sfilename = "mfile.zip"; //filename here
$obj = new Google_Service_Storage_StorageObject();
$obj->setName($sfilename);
$obj->setBucket("myBucketS"); //bucket name here
$filen = "pathtofile/uploadthis.zip";
$mimetype = mime_content_type($filen);
$chunkSizeBytes = 1 * 1024 * 1024;
$client->setDefer(true);
$status = false;
$filetoupload = array('name' => $sfilename, 'uploadType' => 'resumable');
$request = $storage->objects->insert("myBucketS",$obj,$filetoupload);
$media = new Google_Http_MediaFileUpload($client, $request, $mimetype, null, true, $chunkSizeBytes);
$media->setFileSize(filesize($filen));
$handle = fopen($filen, "rb");
while (!$status && !feof($handle)) {
$chunk = fread($handle, $chunkSizeBytes);
$status = $media->nextChunk($chunk);
}
$result = false;
if($status != false) {
$result = $status;
}
fclose($handle);
// Reset to the client to execute requests immediately in the future.
$client->setDefer(false);
} else {
// If the user hasn't authorized the app, initiate the OAuth flow
$state = mt_rand();
$client->setState($state);
$_SESSION['state'] = $state;
$authUrl = $client->createAuthUrl();
}
$_SESSION['token'] = $client->getAccessToken();
print_r($status);
?>