PHPackages                             yiier/yii2-aliyun-oss - 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. yiier/yii2-aliyun-oss

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

yiier/yii2-aliyun-oss
=====================

Yii2 阿里云 OSS

1.0(6y ago)445.3k62BSD-3-ClausePHP

Since Mar 17Pushed 6y ago3 watchersCompare

[ Source](https://github.com/yiier/yii2-aliyun-oss)[ Packagist](https://packagist.org/packages/yiier/yii2-aliyun-oss)[ RSS](/packages/yiier-yii2-aliyun-oss/feed)WikiDiscussions master Synced yesterday

READMEChangelog (2)Dependencies (2)Versions (3)Used By (2)

Yii2 Aliyun OSS
===============

[](#yii2-aliyun-oss)

Yii2 阿里云 OSS

[![Latest Stable Version](https://camo.githubusercontent.com/110059040ae0fb35b67cccfd22aad75dedc7b34c179d6cc64e67921b988ef429/68747470733a2f2f706f7365722e707567782e6f72672f79696965722f796969322d616c6979756e2d6f73732f762f737461626c65)](https://packagist.org/packages/yiier/yii2-aliyun-oss)[![Total Downloads](https://camo.githubusercontent.com/452369c01602cefd5575fd27df349e566aba0a7efb514e448d8965f75d76e72d/68747470733a2f2f706f7365722e707567782e6f72672f79696965722f796969322d616c6979756e2d6f73732f646f776e6c6f616473)](https://packagist.org/packages/yiier/yii2-aliyun-oss)[![Latest Unstable Version](https://camo.githubusercontent.com/d2c66b0a7c5ffb7fa1c7a8bcbba3977d8729f0024d175dea1725b31879ee1351/68747470733a2f2f706f7365722e707567782e6f72672f79696965722f796969322d616c6979756e2d6f73732f762f756e737461626c65)](https://packagist.org/packages/yiier/yii2-aliyun-oss)[![License](https://camo.githubusercontent.com/5cb90cb20045b35614b1d7244ac961c489f629207424426349ef0511706f1d08/68747470733a2f2f706f7365722e707567782e6f72672f79696965722f796969322d616c6979756e2d6f73732f6c6963656e7365)](https://packagist.org/packages/yiier/yii2-aliyun-oss)

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist yiier/yii2-aliyun-oss "*"

```

or add

```
"yiier/yii2-aliyun-oss": "*"

```

to the require section of your `composer.json` file.

Usage
-----

[](#usage)

配置文件添加组件 :

```
components => [
    'oss' => [
        'class' => 'yiier\AliyunOSS\OSS',
        'accessKeyId' => 'xxxxx', // 阿里云OSS AccessKeyID
        'accessKeySecret' => 'xxxx', // 阿里云OSS AccessKeySecret
        'bucket' => 'xxx', // 阿里云的bucket空间
        'lanDomain' => 'oss-cn-hangzhou-internal.aliyuncs.com', // OSS内网地址
        'wanDomain' => 'oss-cn-hangzhou.aliyuncs.com', //OSS外网地址
        'isInternal' => true // 上传文件是否使用内网，免流量费（选填，默认 false 是外网）
    ],
]
```

```
/** @var \yiier\AliyunOSS\OSS $oss */
$oss = \Yii::$app->get('oss');
$fh = '/vagrant/php/baseapi/web/storage/image/824edb4e295892aedb8c49e4706606d6.png';
$oss->upload('824edb4e295892aedb8c49e4706606d6.png', $fh);

或者

$oss->upload('storage/image/824edb4e295892aedb8c49e4706606d6.png', $fh); // 会自动创建文件夹

其他用法

$oss->createDir('storage/image/'); //创建文件夹
$oss->delete('824edb4e295892aedb8c49e4706606d6.png'); // 删除文件
$oss->delete('storage/image/824edb4e295892aedb8c49e4706606d6.png'); // 删除文件，如果这个文件是此文件夹的最后一个文件，则会把文件夹一起删除
$oss->delete('storage/image/'); // 删除文件夹，但是要确保是空文件夹
$oss->getAllObject(); // 获取根目录下的所有文件名，默认是100个
$oss->getAllObject(['prefix' => 'storage/image/']); // 获取 `storage/image/` 目录下的所有文件名，默认是100个
```

### Upload action

[](#upload-action)

视图文件：

```
