报错信息:
WARNING Port_onRead_http() (ERRNO 7102): Request Entity Too Large: header-length (351) + content-length (2986258) is greater than the package_max_length(2097152) from session#3 on 0.0.0.0:9501
这里主要看的swoole Swoole4 文档
GET/POST 请求的最大尺寸
GET 请求最大 8192
GET 请求只有一个 Http 头,Swoole 底层使用固定大小的内存缓存区 8K,并且不可修改。如果请求不是正确的 Http 请求,将会出现错误。底层会抛出以下错误:
WARN swReactorThread_onReceive_http_request: http header is too long.Copy to clipboardErrorCopied
POST 文件上传
最大尺寸受到 package_max_length 配置项限制,默认为 2M,可以调用 Server->set 传入新的值修改尺寸。Swoole 底层是全内存的,因此如果设置过大可能会导致大量并发请求将服务器资源耗尽。
计算方法:最大内存占用
= 最大并发请求数
* package_max_length
解决方案很简单 , 就是在config / autoload / server.php中的setting设置 , 添加一行代码, 并增大一点缓冲区大小即可
Constant::OPTION_PACKAGE_MAX_LENGTH => 20 * 1024 * 1024, 这里如果你像我一样手写的工厂模式上传 , 可以使用OSS , 千万别用华为云的就对了 , 不是很好用 ,这里如果服务器和OSS在同一个机房 , 可以使用内网地址达到加速的作用
下面是我的阿里云上传OSS代码 , 没有存储到本地进行中转 , 2M文件大概是0.2s左右
<?php
/**
* 华为云
*
* @authors Msy
* @Created-Time: 2022/12/7 10:39
*/
namespace App\Services\Oss;
use App\Exception\ApiException;
use OSS\OssClient;
class AliOss implements OssBase
{
public function uploadToOss($file): string
{
//上传文件最大大小,单位M
$maxSize = config('oss.aliyun.maxSize');
//支持的上传文件类型
$allowed_extensions = ["png", "jpg", "jpeg", "gif", "webp"];
//检测文件类型
$ext = $file->getExtension();
if (!in_array(strtolower($ext), $allowed_extensions)) {
throw new ApiException(
message: config('statuscode.err_msg.not_supported_format_file'),
code: config('statuscode.err_code.not_supported_format_file'),
remind: 1,
);
}
//检测文件大小
if ($file->getSize() > $maxSize * 1024 * 1024) {
throw new ApiException(
message: config('statuscode.err_msg.file_too_large'),
code: config('statuscode.err_code.file_too_large'),
remind: 1,
);
}
$accessKeyId = config('thirtparty.aliyun.accessKeyID');
$accessKeySecret = config('thirtparty.aliyun.accessKeySecret');
$endpoint = config('oss.aliyun.endpoint');
$bucket = config('oss.aliyun.bucket');
$newFile = 'ddgcjx' . "/" . date('Ym') . "/" . uniqid() . rand(10, 99) . "." . $file->getExtension();
$filePath = $file->getPathname();;
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
$style = "image/watermark,g_nw,t_90,size_100,type_ZmFuZ3poZW5naGVpdGk,x_10,y_10,P_10,image_bG9nby5wbmc_eC1vc3MtcHJvY2Vzcz1pbWFnZS9yZXNpemUsUF8xNQ==";
// 上传时可以设置相关的headers,例如设置访问权限为private、自定义元信息等。
$options = array(
OssClient::OSS_HEADERS => array(
'x-oss-object-acl' => 'public-read',
),
OssClient::OSS_PROCESS => $style,
);
$resp = $ossClient->uploadFile($bucket, $newFile, $filePath, $options);
if ($resp['info']['http_code'] == 200) { //$resp ['HttpStatusCode']状态码 200就是成功
$res = $resp['info']['url'];
// OSS 访问域名
$url = config('oss.aliyun.url');
if (!$url) {
return $res;
} else {
return str_ireplace($bucket . '.' . $endpoint, $url, $res);
}
} else {
throw new ApiException(
message: config('statuscode.err_msg.fail'),
code: config('statuscode.err_code.fail'),
remind: 1,
);
}
}
}
———————————————— 版权声明:本文为CSDN博主「苗先生的PHP记录」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 原文链接:blog.csdn.net/s1095622320…