我可以使用 Google PHP API CLIENT 成功地将小文件上传到 Google Cloud Storage,但无法上传 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 更改为可恢复的 .. 但没有运气。请帮忙。
更新:使用 Http 作为Brandon Yarbrough的答案
接收错误(致命错误:未捕获的异常“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);