我正在使用 Backblaze B2 来存储文件,并正在使用他们的文档代码通过他们的 API 上传。然而,他们的代码使用 fread 来读取文件,这会导致大于 100MB 的文件出现问题,因为它会尝试将整个文件加载到内存中。有没有更好的方法不尝试将整个文件加载到 RAM 中?
$file_name = "file.txt";
$my_file = "<path-to-file>" . $file_name;
$handle = fopen($my_file, 'r');
$read_file = fread($handle,filesize($my_file));
$upload_url = ""; // Provided by b2_get_upload_url
$upload_auth_token = ""; // Provided by b2_get_upload_url
$bucket_id = ""; // The ID of the bucket
$content_type = "text/plain";
$sha1_of_file_data = sha1_file($my_file);
$session = curl_init($upload_url);
// Add read file as post field
curl_setopt($session, CURLOPT_POSTFIELDS, $read_file);
// Add headers
$headers = array();
$headers[] = "Authorization: " . $upload_auth_token;
$headers[] = "X-Bz-File-Name: " . $file_name;
$headers[] = "Content-Type: " . $content_type;
$headers[] = "X-Bz-Content-Sha1: " . $sha1_of_file_data;
curl_setopt($session, CURLOPT_HTTPHEADER, $headers);
curl_setopt($session, CURLOPT_POST, true); // HTTP POST
curl_setopt($session, CURLOPT_RETURNTRANSFER, true); // Receive server response
$server_output = curl_exec($session); // Let's do this!
curl_close ($session); // Clean up
echo ($server_output); // Tell me about the rabbits, George!
我试过使用:
curl_setopt($session, CURLOPT_POSTFIELDS, array('file' => '@'.realpath('file.txt')));
但是我得到一个错误响应:读取上传数据时出错:SocketTimeoutException(读取超时)
编辑:使用 CURL 流式传输文件名似乎也不起作用。