news 2026/5/26 8:23:42

thinkcmf改存储CloudflareR2

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
thinkcmf改存储CloudflareR2

先建一个服务类

<?php namespace app\portal\service; class CloudflareR2 { private $accessKey="42098b06dbxxxxxxxx48fad96f4f"; private $secretKey="02d3f18c4ac204d35xxxxxxxxxxxx73bcab3e4c01c982b16fe56c7a7951"; private $accountId="e94bae5daxxxxxx3abbd269e5e"; private $bucketName="suxxxxxoda"; private $region="auto"; public function __construct() { } /** * 获取存储桶 URL */ public function getBucketUrl() { return "{$this->accountId}.r2.cloudflarestorage.com/{$this->bucketName}"; } /** * 上传文件到 Cloudflare R2 */ public function uploadFile($filePath, $objectKey) { if (!file_exists($filePath)) { return ['success' => false, 'error' => 'File not found']; } // 使用同一个时间戳确保所有日期一致 $now = time(); $amzDate = gmdate('Ymd\THis\Z', $now); $dateStamp = gmdate('Ymd', $now); $fileSize = filesize($filePath); $contentType = mime_content_type($filePath); $fileContent = file_get_contents($filePath); // 1. 创建规范请求 $canonicalHeaders = [ 'content-length' => $fileSize, 'content-type' => $contentType, 'host' => "{$this->accountId}.r2.cloudflarestorage.com", 'x-amz-content-sha256' => hash('sha256', $fileContent), 'x-amz-date' => $amzDate ]; // 按字母顺序排序头 ksort($canonicalHeaders); $signedHeaders = implode(';', array_map('strtolower', array_keys($canonicalHeaders))); $canonicalRequest = "PUT\n" . '/' . $this->bucketName . '/' . str_replace('%2F', '/', rawurlencode($objectKey)) . "\n" . "\n" . implode("\n", array_map(function($k, $v) { return strtolower($k) . ':' . $v; }, array_keys($canonicalHeaders), $canonicalHeaders)) . "\n" . "\n" . $signedHeaders . "\n" . $canonicalHeaders['x-amz-content-sha256']; // 2. 创建待签字符串 $algorithm = "AWS4-HMAC-SHA256"; $credentialScope = "{$dateStamp}/{$this->region}/s3/aws4_request"; $stringToSign = "{$algorithm}\n{$amzDate}\n{$credentialScope}\n" . hash('sha256', $canonicalRequest); // 3. 计算签名 $signingKey = $this->getSignatureKey($dateStamp); $signature = hash_hmac('sha256', $stringToSign, $signingKey); // 4. 构建授权头 $authorizationHeader = "{$algorithm} " . "Credential={$this->accessKey}/{$credentialScope}, " . "SignedHeaders={$signedHeaders}, " . "Signature={$signature}"; // 5. 构造请求URL和头 $url = "https://{$this->accountId}.r2.cloudflarestorage.com/{$this->bucketName}/" . rawurlencode($objectKey); $headers = [ "Authorization: {$authorizationHeader}", "Content-Length: {$fileSize}", "Content-Type: {$contentType}", "Host: {$this->accountId}.r2.cloudflarestorage.com", "x-amz-content-sha256: {$canonicalHeaders['x-amz-content-sha256']}", "x-amz-date: {$amzDate}" ]; // 6. 发送请求 $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); curl_setopt($ch, CURLOPT_POSTFIELDS, $fileContent); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); //var_dump($response); die(); curl_close($ch); if ($httpCode === 200) { $url2 = env('CLOUDFLARE_URL') . rawurlencode($objectKey); return ['success' => true, 'url' => $url2]; } else { return [ 'success' => false, 'error' => 'Upload failed', 'http_code' => $httpCode, 'response' => $response, 'debug' => [ 'canonicalRequest' => $canonicalRequest, 'stringToSign' => $stringToSign, 'signingKey' => bin2hex($this->getSignatureKey($dateStamp)), 'signature' => $signature, 'headers' => $headers ] ]; } } /** * 生成签名密钥 */ private function getSignatureKey($dateStamp) { $kSecret = 'AWS4' . $this->secretKey; $kDate = hash_hmac('sha256', $dateStamp, $kSecret, true); $kRegion = hash_hmac('sha256', $this->region, $kDate, true); $kService = hash_hmac('sha256', 's3', $kRegion, true); $kSigning = hash_hmac('sha256', 'aws4_request', $kService, true); return $kSigning; } }

再来个调用的

<?php namespace app\portal\service; use cmf\lib\Storage; class CloudflareR2Storage extends Storage { /** * 上传文件到 Cloudflare R2 * @param string $file 文件路径 * @param string $filePath 本地文件路径 * @param string $fileType 文件类型 * @param array $param 其他参数 * @return array|bool */ public function upload($file, $filePath, $fileType = 'image', $param = null) { $cloudflareR2 = new CloudflareR2(); $objectKey = ltrim($file, '/'); $result = $cloudflareR2->uploadFile($filePath, $objectKey); if ($result['success']) { // 删除本地文件 // @unlink($filePath); return [ 'preview_url' => $result['url'], 'url' => $result['url'], ]; } else { return false; } } /** * 获取图片预览 URL * @param string $file * @param string $style * @return mixed */ public function getPreviewUrl($file, $style = '') { return "https://" . (new CloudflareR2())->getBucketUrl() . "/" . ltrim($file, '/'); } /** * 获取图片 URL * @param string $file * @param string $style * @return mixed */ public function getImageUrl($file, $style = '') { return "https://" . (new CloudflareR2())->getBucketUrl() . "/" . ltrim($file, '/'); } /** * 获取文件下载 URL * @param string $file * @param int $expires * @return mixed|string */ public function getFileDownloadUrl($file, $expires = 3600) { return "https://" . (new CloudflareR2())->getBucketUrl() . "/" . ltrim($file, '/'); } }

然后找到cmf的扩展,找到对应的上传方法

在他上传的方法前面插入个自己的上传判断

// 检查是否配置了 Cloudflare R2 存储 $storage = cmf_get_option('storage'); if ( env('CLOUDFLARE_STATUS') == 1) { // 使用 Cloudflare R2 存储 $cloudflareR2Storage = new \app\portal\service\CloudflareR2Storage(); $result = $cloudflareR2Storage->upload($arrInfo["file_path"], $uploadPath . $arrInfo["file_path"], $fileType); if ($result) { // 删除本地文件 // @unlink($uploadPath . $arrInfo["file_path"]); return array_merge([ 'filepath' => $arrInfo["file_path"], "name" => $arrInfo["filename"], 'id' => $strId, 'preview_url' => $result['preview_url'], 'url' => $result['url'], ], $result); } }

我是直接写在配置文件读取的,要不要写数据库看自己了.

这样就能实现上传到Cloundflare,接着还要处理回显的问题.找到这函数

,改写他

function cmf_get_image_url($file, $style = 'watermark') { if (empty($file)) { return ''; } if (strpos($file, "http") === 0) { return $file; } else { // 获取云存储配置 $CLOUDFLARE_STATUS = env("CLOUDFLARE_STATUS"); $cloudStorageUrl = env("CLOUDFLARE_URL"); // 如果配置了云存储URL,则直接使用 if ($CLOUDFLARE_STATUS == 1) { // 处理路径 $file = ltrim(str_replace('\\', '/', $file), '/'); //如果不是thems开头的需要加上upload前缀 // if (!preg_match('/^themes/', $file) && !preg_match('/^static/', $file)) { // $file = 'upload/' . $file; // } return rtrim($cloudStorageUrl, '/') . '/' . $file; } else { // 使用默认的存储处理 $storage = Storage::instance(); return $storage->getImageUrl($file, $style); } } }

具体匹配规则看自己实际目录结构了.

这样就完成了上传到Cloudflare.

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/25 20:16:29

JConsole 中 GC 时间统计的含义

要理解 JConsole 中 GC 时间统计的含义,需结合 垃圾收集器类型​ 和 统计维度​ 拆解: 1. 关于 PS MarkSweep 上的 12.575 秒 (16 收集) PS MarkSweep:是 JVM 中用于清理 老年代(PS Old Gen)​ 的垃圾收集器(属于 Full GC 收集器,触发时会暂停所有应用线程,即 STW)。…

作者头像 李华
网站建设 2026/5/25 14:57:24

自由职业与咨询:测试工程师的另一种活法

在数字经济的浪潮中&#xff0c;软件测试工程师的角色正从传统的全职雇佣模式&#xff0c;向更灵活的自由职业与咨询路径扩展。随着人工智能、云计算和敏捷开发的普及&#xff0c;测试行业对专业化、独立服务的需求日益增长。本文基于2025年的行业现状&#xff0c;探讨测试工程…

作者头像 李华
网站建设 2026/5/26 5:10:14

告别手动更新烦恼:Latest让你的macOS应用始终保持最新状态

告别手动更新烦恼&#xff1a;Latest让你的macOS应用始终保持最新状态 【免费下载链接】Latest A small utility app for macOS that makes sure you know about all the latest updates to the apps you use. 项目地址: https://gitcode.com/gh_mirrors/la/Latest 你是…

作者头像 李华
网站建设 2026/5/26 5:21:18

谈判技巧:在offer阶段如何为自己争取更好的薪酬包?

软件测试从业者的薪酬谈判机遇与挑战 在2025年底的软件测试行业中&#xff0c;随着人工智能和自动化测试工具的普及&#xff0c;企业对高素质测试人才的需求持续增长&#xff0c;这为从业者争取更优薪酬包创造了有利条件。然而&#xff0c;许多测试工程师在offer阶段因缺乏谈判…

作者头像 李华
网站建设 2026/5/26 5:30:44

Open-XiaoAI:如何让小爱音箱真正听懂你的声音,解锁无限可能

Open-XiaoAI&#xff1a;如何让小爱音箱真正听懂你的声音&#xff0c;解锁无限可能 【免费下载链接】open-xiaoai 让小爱音箱「听见你的声音」&#xff0c;解锁无限可能。 项目地址: https://gitcode.com/gh_mirrors/op/open-xiaoai 在智能音箱普及的今天&#xff0c;你…

作者头像 李华
网站建设 2026/5/25 18:47:29

在线字体编辑器终极指南:轻松编辑转换字体文件

在数字化设计时代&#xff0c;字体已成为提升作品质量的关键元素。在线字体编辑器让每个人都能轻松编辑、转换和优化字体文件&#xff0c;无需安装复杂软件。fonteditor作为一款功能全面的开源工具&#xff0c;支持TTF、WOFF、WOFF2、OTF、SVG、EOT等多种格式&#xff0c;让字体…

作者头像 李华