Skip to content

StorageService — 存储服务

  • 文件位置: kernel/Service/StorageService.php
  • 命名空间: kernel\Service
  • 继承: extends Foundation\Service
  • 是否可继承: 是

文件存储门面,管理多存储平台(local/oss/cos)的注册、切换、文件路由、文件键生成、归属绑定、访问控制与签名 URL 生成。所有成员与属性均为静态。

属性

属性类型默认可见性说明
$routePrefixstring"files"protected static文件路由前缀
$usePlatformAbstractStoragenullprotected static当前使用的平台实例
$usePlatformNamestringnullprotected static当前使用的平台名称
$platformInstancesarray<string, AbstractStorage>nullprotected static平台实例注册表,键为平台名、值为平台实例
$fileNameMatchPatternstring"[\w\/]+?\.\w+"protected static文件名匹配正则表达式

方法速查

方法说明
combinedFileKey($filePath, $fileName, $encode)组合文件路径与名称为文件键
bootstrap($usePlatforms, $routePrefix)初始化平台并注册文件路由
getPlatform($name)获取平台实例
getPlatformName()当前平台名称
setPlatform($name, $platformInstance)注册平台实例
switchPlatform($name)切换当前平台
hasPlatform($name)平台是否存在
switchToLocal() / switchToCOS() / switchToOSS()快捷切换指定平台
registerRoute($Method, $Controller, $URI, $WithFileKey)注册文件路由
setFilesBelongs($FileKeys, $BelongsId, $BelongsType)绑定文件归属
deleteBelongsFiles($BelongsId, $BelongsType)删除某归属的全部文件
setFileAccessControl($FileKeys, $AccessControlTag)设置文件访问控制权限
setFileAccessControlToPrivate($FileKey)设为私有
setFileAccessControlToAuthenticatedRead($FileKey)设为授权读
setFileAccessControlToAuthenticatedReadWrite($FileKey)设为授权读写
setFileAccessControlToPublicReadWrite($FileKey)设为公共读写
setFileAccessControlToPublicRead($FileKey)设为公共读
getFileAuth($FileKey, $Expires, $URLParams, $Headers, $HTTPMethod)获取文件访问授权
getFileSign()获取文件签名(转发)
getFileTransferAuth($FileKey, $Expires, $URLParams, $Headers, $HTTPMethod)获取传输授权
getFilePreviewURL($FileKey, $URLParams, $Expires, $WithSignature)获取预览 URL
getFileTransferPreviewURL($FileKey, $URLParams, $Expires, $WithSignature)获取传输预览 URL
getFileDownloadURL($FileKey, $URLParams, $Expires, $WithSignature)获取下载 URL
getFileTransferDownloadURL($FileKey, $URLParams, $Expires, $WithSignature)获取传输下载 URL

方法

combinedFileKey($filePath, $fileName, $encode = false) — 组合文件键

统一反斜杠为 / 后用 / 拼接文件路径与名称,去除首部 /;可选 rawurlencode 编码。

参数

参数类型默认说明
$filePathstring文件所在路径
$fileNamestring文件名称
$encodeboolfalse是否对组合后的文件键进行 URL 编码

返回值

  • string:组合后的文件键名。

示例

php
$key = StorageService::combinedFileKey("user/avatar", "avatar.jpg", false);
// user/avatar/avatar.jpg

bootstrap($usePlatforms = null, $routePrefix = "files") — 初始化平台并注册文件路由

注册平台实例列表,根据当前请求 URI 判断是否为文件请求并切换到对应平台,随后注册 get/delete/post/patch/preview/download 六类文件路由。缺省平台时自动使用 LocalStorage

参数

参数类型默认说明
$usePlatformsarray<string, AbstractStorage>null平台实例关联数组,键为平台名、值为平台实例;缺省使用 local 本地存储
$routePrefixstring"files"文件路由前缀

返回值

  • void:无返回值。

示例

php
StorageService::bootstrap([
    "local" => new LocalStorage("key"),
    "oss"   => new AliyunOSSStorage("key"),
], "files");

getPlatform($name = null) — 获取平台实例

参数

参数类型默认说明
$namestringnull平台名;传入返回对应注册实例,缺省返回当前使用的平台实例

返回值

  • AbstractStorage:平台实例。

示例

php
$platform = StorageService::getPlatform("oss");

getPlatformName() — 当前平台名称

参数

无参数。

返回值

  • string:当前使用的平台名称。

setPlatform($name, $platformInstance) — 注册平台实例

参数

参数类型默认说明
$namestring平台名
$platformInstanceAbstractStorage平台实例

返回值

  • string:返回 StorageService::class(支持链式)。

switchPlatform($name) — 切换当前平台

目标平台不存在时抛出 Exception(500)。

参数

参数类型默认说明
$namestring要切换到的平台名

返回值

  • string:返回 StorageService::class(支持链式)。

hasPlatform($name) — 平台是否存在

参数

参数类型默认说明
$namestring平台名

返回值

  • bool:平台已注册返回 true

switchToLocal() / switchToCOS() / switchToOSS() — 快捷切换

分别切换当前平台为 local / cos / oss,内部均调用 switchPlatform()

参数

无参数。

返回值

  • string:返回 StorageService::class(支持链式)。

registerRoute($Method, $Controller, $URI = null, $WithFileKey = true) — 注册文件路由

common 类型注册文件访问路由,URI 形如 {routePrefix}/{fileKey}/{可选URI}

参数

参数类型默认说明
$MethodstringHTTP 请求方法(如 getpost
$Controllerstring|array控制器;数组时第一项为控制器实例、第二项为要执行的方法名
$URIstringnull附加的 URI 段(如 previewdownload
$WithFileKeybooltrue注册的 URI 中是否包含 {fileKey}

返回值

  • string:返回 StorageService::class(支持链式)。

setFilesBelongs($FileKeys, $BelongsId, $BelongsType) — 绑定文件归属

为当前平台的文件模型批量写入归属信息(belongsIdbelongsType)。

参数

参数类型默认说明
$FileKeysarray|string文件键或文件键数组
$BelongsIdint归属对象的 ID
$BelongsTypestring归属对象类型

返回值

  • int:受影响记录数。

deleteBelongsFiles($BelongsId, $BelongsType) — 删除某归属的全部文件

参数

参数类型默认说明
$BelongsIdint归属对象的 ID
$BelongsTypestring归属对象类型

返回值

  • int:删除的记录数。

setFileAccessControl($FileKeys, $AccessControlTag) — 设置文件访问控制权限

参数

参数类型默认说明
$FileKeysarray|string文件键或文件键数组
$AccessControlTagstring访问控制标签(如 AbstractStorage::PUBLIC_READ

返回值

  • int:受影响记录数。

setFileAccessControlToPrivate($FileKey) — 设为私有

参数

参数类型默认说明
$FileKeystring文件键

返回值

  • int:受影响记录数。

setFileAccessControlToAuthenticatedRead($FileKey) — 设为授权读

参数

参数类型默认说明
$FileKeystring文件键

返回值

  • int:受影响记录数。

setFileAccessControlToAuthenticatedReadWrite($FileKey) — 设为授权读写

参数

参数类型默认说明
$FileKeystring文件键

返回值

  • int:受影响记录数。

setFileAccessControlToPublicReadWrite($FileKey) — 设为公共读写

参数

参数类型默认说明
$FileKeystring文件键

返回值

  • int:受影响记录数。

setFileAccessControlToPublicRead($FileKey) — 设为公共读

参数

参数类型默认说明
$FileKeystring文件键

返回值

  • int:受影响记录数。

getFileAuth($FileKey, $Expires = 1800, $URLParams = [], $Headers = [], $HTTPMethod = "get") — 获取文件访问授权

转发给当前平台实例的 getFileAuth()

参数

参数类型默认说明
$FileKeystring文件键
$Expiresint1800授权有效期(秒)
$URLParamsarray[]附加的 URL 参数
$Headersarray[]附加的请求头
$HTTPMethodstring"get"HTTP 请求方法

返回值

  • 与平台 getFileAuth() 返回值一致(授权数据)。

getFileSign() — 获取文件签名

call_user_func_array 把全部实参转发给当前平台实例的 getFileSign()

参数

  • ...:透传任意参数给平台 getFileSign()

返回值

  • 与平台 getFileSign() 返回值一致。

getFileTransferAuth($FileKey, $Expires = 1800, $URLParams = [], $Headers = [], $HTTPMethod = "get") — 获取传输授权

转发给当前平台实例的 getFileTransferAuth()

参数

参数类型默认说明
$FileKeystring文件键
$Expiresint1800授权有效期(秒)
$URLParamsarray[]附加的 URL 参数
$Headersarray[]附加的请求头
$HTTPMethodstring"get"HTTP 请求方法

返回值

  • 与平台 getFileTransferAuth() 返回值一致。

getFilePreviewURL($FileKey, $URLParams = [], $Expires = 1800, $WithSignature = TRUE) — 获取预览 URL

参数

参数类型默认说明
$FileKeystring文件键
$URLParamsarray[]附加的 URL 参数
$Expiresint1800URL 有效期(秒)
$WithSignaturebooltrueURL 是否携带签名

返回值

  • string:预览 URL。

getFileTransferPreviewURL($FileKey, $URLParams = [], $Expires = 1800, $WithSignature = TRUE) — 获取传输预览 URL

参数

参数类型默认说明
$FileKeystring文件键
$URLParamsarray[]附加的 URL 参数
$Expiresint1800URL 有效期(秒)
$WithSignaturebooltrueURL 是否携带签名

返回值

  • string:传输预览 URL。

getFileDownloadURL($FileKey, $URLParams = [], $Expires = 1800, $WithSignature = TRUE) — 获取下载 URL

参数

参数类型默认说明
$FileKeystring文件键
$URLParamsarray[]附加的 URL 参数
$Expiresint1800URL 有效期(秒)
$WithSignaturebooltrueURL 是否携带签名

返回值

  • string:下载 URL。

getFileTransferDownloadURL($FileKey, $URLParams = [], $Expires = 1800, $WithSignature = TRUE) — 获取传输下载 URL

参数

参数类型默认说明
$FileKeystring文件键
$URLParamsarray[]附加的 URL 参数
$Expiresint1800URL 有效期(秒)
$WithSignaturebooltrueURL 是否携带签名

返回值

  • string:传输下载 URL。