Download the PHP package yunadmin/yunstorage without Composer

On this page you can find all versions of the php package yunadmin/yunstorage. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package yunstorage

YunStorage 对象存储扩展

一个轻量级、无第三方 SDK 依赖的多云对象存储扩展,原生 PHP 实现,支持阿里云 OSS、腾讯云 COS、七牛云 Kodo、百度云 BOS 四大厂商。


目录结构

命名空间映射(PSR-4,根 Yunadmin\YunStorage\ → src/,与 yunadmin/http-client 风格一致):

文件 命名空间
src/Storage.php Yunadmin\YunStorage\Storage
src/AbstractDriver.php Yunadmin\YunStorage\AbstractDriver
src/DriverInterface.php Yunadmin\YunStorage\DriverInterface
src/Signer.php Yunadmin\YunStorage\Signer
src/drivers/AliyunDriver.php Yunadmin\YunStorage\drivers\AliyunDriver

环境要求


安装


快速开始


API 文档

Yunadmin\YunStorage\Storage 工厂类

Storage::make(string $name, array $config): DriverInterface

创建驱动实例。

参数 类型 说明
$name string 驱动名:aliyun / qcloud / qiniu / baidu
$config array 驱动配置(详见各厂商配置说明)

异常:传入不支持的驱动名时抛 InvalidArgumentException。

Storage::extend(string $name, string $class): void

注册自定义驱动,用于扩展第三方厂商。

Storage::supportedDrivers(): array

返回所有已注册的驱动名列表。


DriverInterface 接口方法

所有 Driver 实例都实现以下 5 个方法:

upload(string $localPath, string $key): bool

上传本地文件到对象存储。

fetch(string $url, ?string $key): bool

抓取远程 URL 资源或上传本地文件到对象存储。

delete(string $key): bool

删除对象存储中的指定对象。

getObjectUrl(string $key): string

获取对象的访问 URL。

getError(): string

获取最近一次操作的错误信息(在 upload / fetch / delete 返回 false 后调用)。


各厂商配置说明

阿里云 OSS

区域 Endpoint 参考:

区域 Endpoint Region (V4 签名用)
华东1(杭州) oss-cn-hangzhou.aliyuncs.com cn-hangzhou
华东2(上海) oss-cn-shanghai.aliyuncs.com cn-shanghai
华北1(青岛) oss-cn-qingdao.aliyuncs.com cn-qingdao
华北2(北京) oss-cn-beijing.aliyuncs.com cn-beijing
华南1(深圳) oss-cn-shenzhen.aliyuncs.com cn-shenzhen
香港 oss-cn-hongkong.aliyuncs.com cn-hongkong

签名方式:V4 签名(OSS4-HMAC-SHA256),2025 年 9 月起对新 Bucket 强制要求,本扩展已默认采用 V4。

V4 关键点:


腾讯云 COS

区域 Region 参考:

区域 Region
广州 ap-guangzhou
上海 ap-shanghai
北京 ap-beijing
成都 ap-chengdu
香港 ap-hongkong

域名拼接规则(domain 留空时):{bucket}.cos.{region}.myqcloud.com

签名方式:V5 签名,q-sign-algorithm=sha1,SignKey 和 Signature 均用 hex 字符串


七牛云 Kodo

区域 Region 参考:

区域 Region
华东 z0
华北 z1
华南 z2
北美 na0
东南亚 as0

接口域名(自动按 region 选择):

接口 域名(z0 示例)
上传 upload.qiniup.com
删除 rs.qiniu.com
抓取 iovip.qbox.me

签名方式:


百度云 BOS

区域 Region 参考:

区域 Region
广州 gz
北京 bj
苏州 su

域名拼接规则(domain 留空时):{bucket}.{region}.bcebos.com

签名方式:BCE V2 签名(2019-04 起百度推荐的最新协议,性能更高、密钥更安全)。

V2 与 V1 主要差异:

V2 算法:

关键点:


接入到 ThinkPHP 项目

方式一:直接使用(推荐)

在业务逻辑中直接调用:

方式二:封装到引擎类

适配原有 ThinkPHP app/common/service/storage/engine/ 目录的引擎类风格:


自定义 Driver 扩展

实现一个新的对象存储厂商只需 3 步:

步骤 1:实现 DriverInterface

步骤 2:注册驱动

步骤 3:使用


设计说明

为什么不使用官方 SDK?

对比项 官方 SDK YunStorage
包体积 阿里 SDK ~3MB + 腾讯 SDK ~2MB + 七牛 SDK ~1MB + 百度 SDK ~2MB = ~8MB ~30KB
依赖 各厂商依赖 Guzzle/PSR 等库,版本冲突风险 零依赖,仅 PHP 原生
功能 大而全(分片上传/生命周期/桶管理等) 精简核心(上传/抓取/删除/URL)
学习成本 各家 API 风格不一致 统一接口,切换厂商零成本
维护 升级 SDK 可能破坏兼容 签名算法稳定,几乎无需升级

大文件流式上传

所有 Driver 上传时通过 AbstractDriver::httpUpload() 委托 yunadmin/http-client,底层走 cURL 的 CURLOPT_INFILE(raw PUT)或 CURLFile(multipart),按 chunk 读取文件,内存占用恒为 O(1),上传 1GB 文件与 1KB 文件内存占用相同。

错误处理规范

所有方法失败时返回 false,并可通过 getError() 获取人类可读的错误信息:

错误信息来源优先级:

  1. 云厂商返回的错误描述(自动解析 XML 或 JSON)
  2. 本地校验错误(如 key 为空、文件不存在)
  3. HTTP 状态码兜底(如 OSS PUT 失败, HTTP 403)

签名算法正确性

各厂商签名均按官方文档实现,关键点:

厂商 算法 关键细节
阿里 OSS V4: HMAC-SHA256 + Hex 派生密钥链 4 步;必签 host/x-oss-date/x-oss-content-sha256;流式上传用 UNSIGNED-PAYLOAD;region 从 endpoint 自动解析
腾讯 COS HMAC-SHA1 + Hex SignKey 和 Signature 均用 hex 字符串(非 base64)
百度 BOS V2: HMAC-SHA256 + Hex 派生密钥链含 date/region/service;signingKey 用 hex 字符串(非二进制)作为下一步 HMAC 的 key;只签 host + x-bce-date
七牛 Kodo HMAC-SHA1 + Base64URL 上传凭证 token 三段式,管理凭证 QBox 前缀

常见问题

Q1:上传时报 403 SignatureDoesNotMatch

原因:系统时间与云服务器时间偏差过大(>15 分钟)。

解决:检查服务器时间是否已通过 NTP 同步:

Q2:腾讯 COS 报 "q-signature 不匹配"

检查:region 配置是否与 bucket 实际区域一致,bucket 名是否包含 APPID 后缀(如 mybucket-1250000000)。

Q3:七牛上传成功但 getObjectUrl 返回的 URL 无法访问

原因:七牛要求必须配置 custom_domain 或 domain(绑定到 bucket 的 CDN 或测试域名)。

解决:在七牛控制台绑定加速域名后填入 custom_domain。

Q4:百度 BOS 删除报 403

原因:BCE 签名只对 host 和 x-bce-date 两个 header 签名,若 HTTP 客户端自动添加了 Content-Length 不匹配的 header 会被拒绝。

解决:本扩展已规避此问题,如仍报错请检查 cURL 是否被全局拦截器修改了 header。

Q5:如何切换存储厂商而不改业务代码?

业务层只依赖 DriverInterface,切换厂商只需改 Storage::make() 第一个参数:

Q6:如何获取上传后的文件大小或 hash?

本扩展聚焦核心功能,未返回上传响应详情。如需获取,可在 Driver 中扩展 upload() 方法的返回值,或通过 getObjectUrl() 拼接的 URL 发 HEAD 请求查询。


版本兼容


License

MIT


All versions of yunstorage with dependencies

PHP Build Version
Package Version
Requires php Version ^8.0
ext-json Version *
ext-curl Version *
yunadmin/http-client Version ^1.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package yunadmin/yunstorage contains the following files

Loading the files please wait ...