我可以使用Google PHP API CLIENT将小尺寸文件成功上传到Google Cloud 端存储,但无法上传300MB文件 . 返回内存错误 . 这是我的代码 .
$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更改为可恢复..但没有运气 . 请帮忙 .
Update: Used the Http as Brandon Yarbrough answer
收到错误(致命错误:未捕获的异常'Google_IO_Exception')
$storage = new Google_Service_Storage($client);
$obj = new Google_Service_Storage_StorageObject();
$obj->setName("filenameo.zip");
$obj->setBucket("bucketname");
$filen = "300mb-file.zip";
$mimetype = mime_content_type($filen);
$chunkSizeBytes = 1 * 1024 * 1024;
$client->setDefer(true);
$status = false;
$filetoupload = array('name' => $file_name, 'data' => $filen, 'uploadType' => 'media');
$request = $storage->objects->insert("bucketname",$obj,$filetoupload);
$media = new Google_Http_MediaFileUpload($client, $request, $mimetype, $chunkSizeBytes);
$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);
2 回答
得到它使用下面的代码 .
file_get_contents
字面上将文件内容作为字符串加载到内存中 . 您的应用程序可能没有额外的300 MB内存可供使用 .相反,我建议以块的形式读取文件并上传它们 .
Google_Http_MediaFileUpload
是一个很有用的工具 . 这里有一个很好的例子:https://developers.google.com/api-client-library/php/guide/media_upload重要的部分是这一点: