PHPackages                             cdcchen/yii2-cloud-storage - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. cdcchen/yii2-cloud-storage

ActiveYii2-extension[File &amp; Storage](/categories/file-storage)

cdcchen/yii2-cloud-storage
==========================

yii2 cloud storage component

1.0.4(10y ago)12251BSD-4-ClausePHPPHP &gt;=5.4.0

Since Apr 28Pushed 9y ago1 watchersCompare

[ Source](https://github.com/cdcchen/yii2-cloud-storage)[ Packagist](https://packagist.org/packages/cdcchen/yii2-cloud-storage)[ Docs](https://github.com/cdcchen/yii2-cloud-storage)[ RSS](/packages/cdcchen-yii2-cloud-storage/feed)WikiDiscussions master Synced 1mo ago

READMEChangelogDependencies (4)Versions (7)Used By (0)

yii2-cloud-storage 组件使用说明
=========================

[](#yii2-cloud-storage-组件使用说明)

此组件主要功能为两部分：

- 将目前主流云存储平台的接口封闭，以更加方便的使用。
- 添加云存储版的`UploadFile`，`saveAs`方法将上传文件保存到云存储中。

使用步骤
----

[](#使用步骤)

### 第一步：在components中添加组件配置

[](#第一步在components中添加组件配置)

```
'storage' => [
    'class' => 'cdcchen\yii\cloudstorage\UpYunStorage',
    'isImageBucket' => true,
    'endpoint' => 'v2.api.upyun.com',
    'bucket' => 'test',
    'username' => 'test',
    'password' => '123123',
    'domain' => 'http://test.b0.upaiyun.com',
    'autoGenerateFilename' => true,
    'pathFormat' => '{year}/{month}/{day}',
    'filenameFormat' => '{timestamp}-{uniqid}',
]
```

### 第二步：在action中调用storage组件进行文件操作

[](#第二步在action中调用storage组件进行文件操作)

#### 获取storage组件实例

[](#获取storage组件实例)

```
/* @var \cdcchen\yii\cloudstorage\Storage $storage */
$storage = Yii::$app->get('storage');
```

云存储文件操作方法说明
-----------

[](#云存储文件操作方法说明)

### 上传文件

[](#上传文件)

> write方法第一个参数$filename可以接受一个本地文件路径，也可以接受一个文件的内容。
>
> 若第二个参数为`null`，则组件会按照`pathFormat`和`filenameFormat`的配置来自动生成文件路径及文件名。

**自动生成文件路径**

```
$filename = Yii::getAlias('@runtime/chen.jpg');
$info = $storage->write($filename);
```

**手动指定文件路径**

```
$filename = Yii::getAlias('@runtime/chen.jpg');
$filepath = '/test/a.jpg';
$info = $storage->write($filename, $filepath);
```

**返回数据结构如下：**

图片文件

```
Array
(
    [url] => http://test.b0.upaiyun.com/2016/05/04/1462332949-57296e153ab67.jpeg
    [file] => /avatar/chendong/2016/05/04/1462332949-57296e153ab67.jpeg
    [path] => /avatar/chendong/2016/05/04
    [name] => 1462332949-57296e153ab67.jpeg
    [width] => 479
    [height] => 555
    [type] => JPEG
    [frames] => 1
)
```

非图片文件

```
Array
(
    [url] => http://test.b0.upaiyun.com/avatar/2016/05/04/1462333056-57296e80bbfa2.doc
    [file] => /avatar/chendong/2016/05/04/1462333056-57296e80bbfa2.doc
    [path] => /avatar/chendong/2016/05/04
    [name] => 1462333056-57296e80bbfa2.doc
)
```

### 读取文件(下载文件)

[](#读取文件下载文件)

```
$filename = '/test/a.jpg';
$content = $storage->read($filename);
```

返回值`$content`为`$filename`的内容。

### 删除文件

[](#删除文件)

```
$filename = '/test/a.jpg';
$result = $storage->delete($filename);
```

删除成功返回true。

UploadFile使用说明
--------------

[](#uploadfile使用说明)

`cdcchen\yii\cloudstorage\UploadFile`继承自`yii\web\UploadFile，在此基础上又添加了`upload`方法。

使用方法同`yii\web\UploadFile`，如果自动生成文件路径及文件名，直接使用`upload`方法。这样做是为了保证saveAs的兼容性和一致性。

###  Health Score

29

—

LowBetter than 60% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity14

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~0 days

Total

5

Last Release

3663d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/e0d05ba8b4e28f787a22322fdf0e3f0888231997cf6adc4ee5d187dee7bddf19?d=identicon)[cdcchen](/maintainers/cdcchen)

---

Top Contributors

[![cdcchen](https://avatars.githubusercontent.com/u/748891?v=4)](https://github.com/cdcchen "cdcchen (24 commits)")

---

Tags

cloud-storageyii2 cloud storage

### Embed Badge

![Health badge](/badges/cdcchen-yii2-cloud-storage/health.svg)

```
[![Health](https://phpackages.com/badges/cdcchen-yii2-cloud-storage/health.svg)](https://phpackages.com/packages/cdcchen-yii2-cloud-storage)
```

###  Alternatives

[mihaildev/yii2-elfinder

Yii2 ElFinder

169658.8k52](/packages/mihaildev-yii2-elfinder)[gliterd/backblaze-b2

PHP SDK for working with backblaze B2 cloud storage.

84263.3k8](/packages/gliterd-backblaze-b2)[torann/mediasort

Attachment manager for Laravel.

2012.5k](/packages/torann-mediasort)[liyunfang/yii2-upload-behavior

Upload behavior for Yii 2

161.7k](/packages/liyunfang-yii2-upload-behavior)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
