How to increase the performance of a file upload using native sftp functions and fwrite in PHP(如何使用原生 sftp 函数和 PHP 中的 fwrite 提高文件上传的性能)
问题描述
您好,我正在使用以下代码将一个大文件 (500MB) 上传到 sftp 服务器.
Hi I am using the following code to upload a huge file (500MB) to a sftp server.
<?php
$connection = ssh2_connect($this->host, $this->port, null);
$sftp = ssh2_sftp($connection);
$connection_string = ((int) $sftp) . $remotePath . $remoteFilename;
$stream = fopen('ssh2.sftp://' . $connection_string, 'w');
$source = fopen($localFilepath, 'r');
if (!$stream) {
throw new Exception('Could not create file: ' . $connection_string);
}
while (!feof($source)) {
// Chunk size 32 MB
if (fwrite($stream, fread($source, 33554432)) === false) {
throw new Exception('Could not send data: ' . $connection_string);
}
}
fclose($source);
fclose($stream);
但上传速度非常慢.代码在 Google Cloud Run 上运行.上传速度约为 8 MiB/s.
But the upload is very slow. The code is running on Google Cloud Run. The upload speed is around 8 MiB/s.
我也尝试通过 shell_exec 使用 lftp,但由于 Cloud Run,这会导致更多问题.
I also tried to use lftp via shell_exec but this lead to even more issues due to Cloud Run.
上行链路不是问题,因为我可以通过 CURL post 发送文件而没有任何问题.
The uplink can't be the problem as I can send files via CURL post without any issues.
有人可以帮忙吗?
非常感谢和最好的,intxcc
Many thanks and best, intxcc
推荐答案
问题是即使 32MB 被读取然后写入 sftp 流,fwrite 也会以不同的大小分块.我想只有几 KB.
The issue is that even though 32MB are read and then written to the sftp stream, fwrite will chunk at a different size. I think just a few KB.
对于文件系统(这是 fwrite 的常见情况),这很好,但由于 fwrite 到远程服务器而导致高延迟.
For filesystems (which is the usual case with fwrite) this is fine, but not with high latency due to fwriting to a remote server.
所以解决方法是增加sftp流的chunk大小
So the solution is to increase the chunk size of the sftp stream with
stream_set_chunk_size($stream, 1024 * 1024);
所以最终的工作代码是:
So the final working code is:
<?php
$connection = ssh2_connect($this->host, $this->port, null);
$sftp = ssh2_sftp($connection);
$connection_string = ((int) $sftp) . $remotePath . $remoteFilename;
$stream = fopen('ssh2.sftp://' . $connection_string, 'w');
$source = fopen($localFilepath, 'r');
// Stream chunk size 1 MB
stream_set_chunk_size($stream, 1024 * 1024);
if (!$stream) {
throw new Exception('Could not create file: ' . $connection_string);
}
while (!feof($source)) {
// Chunk size 32 MB
if (fwrite($stream, fread($source, 33554432)) === false) {
throw new Exception('Could not send data: ' . $connection_string);
}
}
fclose($source);
fclose($stream);
希望这有助于下一个白发的人试图弄清楚这一点;)
Hope this helps the next person that is getting gray hair trying to figure that out ;)
这篇关于如何使用原生 sftp 函数和 PHP 中的 fwrite 提高文件上传的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用原生 sftp 函数和 PHP 中的 fwrite 提高文件上传的性能
基础教程推荐
- 在多维数组中查找最大值 2021-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01