$config['accessKeyId'], 'endpoint' => $config['endpoint'], 'bucket' => $config['bucket'], 'region' => str_replace('-internal', '', substr(explode('.', $config['endpoint'])[0], 4)), 'provider' => new StaticCredentialsProvider($config['accessKeyId'], $config['accessKeySecret']), 'signatureVersion' => OssClient::OSS_SIGNATURE_VERSION_V4, ]); } /** * CDN 域名(带协议,末尾无斜杠),形如 https://bucket.oss-cn-xxx.aliyuncs.com * @return string */ public static function cdnUrl() { $config = self::config(); return rtrim((string)($config['cdnurl'] ?? ''), '/'); } /** * 将已落盘的本地文件推送到 OSS * 对象键 = 去掉首斜杠的相对路径(/uploads/xxx → uploads/xxx),与附件表 url 口径一致 * @param string $filePath 本地绝对路径 * @param string $relativeUrl 相对URL(形如 /uploads/20261119/xxx.jpg) * @return bool 是否成功(失败写日志并返回 false,由调用方回落本地) */ public static function push($filePath, $relativeUrl) { if (!is_file($filePath)) { Log::write('[OssStorage] 推送OSS失败:本地文件不存在 path=' . $filePath); return false; } try { $config = self::config(); self::client()->uploadFile($config['bucket'], ltrim($relativeUrl, '/'), $filePath); return true; } catch (\Exception $e) { Log::write('[OssStorage] 推送OSS失败 url=' . $relativeUrl . ' err=' . $e->getMessage()); return false; } } /** * 是否保留本地备份(对应插件配置「服务器中转模式备份」serverbackup) * @return bool */ public static function keepLocalBackup() { $config = self::config(); return !$config || !empty($config['serverbackup']); } /** * 删除 OSS 端对象(失败仅记日志,不抛异常) * @param string $relativeUrl 相对URL(形如 /uploads/20261119/xxx.jpg) * @return bool */ public static function delete($relativeUrl) { try { $config = self::config(); self::client()->deleteObject($config['bucket'], ltrim($relativeUrl, '/')); return true; } catch (\Exception $e) { Log::write('[OssStorage] 删除OSS对象失败 url=' . $relativeUrl . ' err=' . $e->getMessage()); return false; } } }