-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit bf6f8e9
Showing
3 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,186 @@ | ||
<?php | ||
/** | ||
* author : forecho <[email protected]> | ||
* createTime : 2016/3/16 18:56 | ||
* description: | ||
*/ | ||
|
||
namespace yiier\AliyunOSS; | ||
|
||
use OSS\OssClient; | ||
use yii\base\Component; | ||
use Yii; | ||
use yii\base\InvalidConfigException; | ||
|
||
class OSS extends Component | ||
{ | ||
/** | ||
* @var string 阿里云OSS AccessKeyID | ||
*/ | ||
public $accessKeyId; | ||
|
||
/** | ||
* @var string 阿里云OSS AccessKeySecret | ||
*/ | ||
public $accessKeySecret; | ||
|
||
/** | ||
* @var string 阿里云的bucket空间 | ||
*/ | ||
public $bucket; | ||
|
||
/** | ||
* @var string OSS内网地址, 如:oss-cn-hangzhou-internal.aliyuncs.com | ||
*/ | ||
public $lanDomain; | ||
|
||
/** | ||
* @var string OSS外网地址, 如:oss-cn-hangzhou.aliyuncs.com | ||
*/ | ||
public $wanDomain; | ||
|
||
/** | ||
* @var OssClient | ||
*/ | ||
private $_ossClient; | ||
|
||
/** | ||
* 从lanDomain和wanDomain中选取, 默认走外网 | ||
* @var string 最终操作域名 | ||
*/ | ||
protected $baseUrl; | ||
|
||
/** | ||
* @var bool 是否私有空间, 默认公开空间 | ||
*/ | ||
public $isPrivate = false; | ||
|
||
/** | ||
* @var bool 上传文件是否使用内网,免流量费 | ||
*/ | ||
public $isInternal = false; | ||
|
||
public function init() | ||
{ | ||
if ($this->accessKeyId === null) { | ||
throw new InvalidConfigException('The "accessKeyId" property must be set.'); | ||
} elseif ($this->accessKeySecret === null) { | ||
throw new InvalidConfigException('The "accessKeySecret" property must be set.'); | ||
} elseif ($this->bucket === null) { | ||
throw new InvalidConfigException('The "bucket" property must be set.'); | ||
} elseif ($this->lanDomain === null) { | ||
throw new InvalidConfigException('The "lanDomain" property must be set.'); | ||
} elseif ($this->wanDomain === null) { | ||
throw new InvalidConfigException('The "wanDomain" property must be set.'); | ||
} | ||
|
||
$this->baseUrl = $this->isInternal ? $this->lanDomain : $this->wanDomain; | ||
} | ||
|
||
/** | ||
* @return \OSS\OssClient | ||
*/ | ||
public function getClient() | ||
{ | ||
if ($this->_ossClient === null) { | ||
$this->setClient(new OssClient($this->accessKeyId, $this->accessKeySecret, $this->baseUrl)); | ||
} | ||
return $this->_ossClient; | ||
} | ||
|
||
/** | ||
* @param \OSS\OssClient $ossClient | ||
*/ | ||
public function setClient(OssClient $ossClient) | ||
{ | ||
$this->_ossClient = $ossClient; | ||
} | ||
|
||
/** | ||
* @param $path | ||
* @return bool | ||
*/ | ||
public function has($path) | ||
{ | ||
return $this->getClient()->doesObjectExist($this->bucket, $path); | ||
} | ||
|
||
/** | ||
* @param $path | ||
* @return bool | ||
*/ | ||
public function read($path) | ||
{ | ||
if (!($resource = $this->readStream($path))) { | ||
return false; | ||
} | ||
$resource['contents'] = stream_get_contents($resource['stream']); | ||
fclose($resource['stream']); | ||
unset($resource['stream']); | ||
return $resource; | ||
} | ||
|
||
/** | ||
* @param $path | ||
* @return array|bool | ||
* @throws \OSS\Core\OssException | ||
*/ | ||
public function readStream($path) | ||
{ | ||
$url = $this->getClient()->signUrl($this->bucket, $path, 3600); | ||
$stream = fopen($url, 'r'); | ||
if (!$stream) { | ||
return false; | ||
} | ||
return compact('stream', 'path'); | ||
} | ||
|
||
/** | ||
* @param $fileName string 文件名 eg: '824edb4e295892aedb8c49e4706606d6.png' | ||
* @param $filePath string 要上传的文件绝对路径 eg: '/storage/image/824edb4e295892aedb8c49e4706606d6.png' | ||
* @return null | ||
* @throws \OSS\Core\OssException | ||
*/ | ||
public function upload($fileName, $filePath) | ||
{ | ||
return $this->getClient()->uploadFile($this->bucket, $fileName, $filePath); | ||
} | ||
|
||
/** | ||
* 删除文件 | ||
* @param $path | ||
* @return bool | ||
*/ | ||
public function delete($path) | ||
{ | ||
return $this->getClient()->deleteObject($this->bucket, $path) === null; | ||
} | ||
|
||
/** | ||
* 创建文件夹 | ||
* @param $dirName | ||
* @return array|bool | ||
*/ | ||
public function createDir($dirName) | ||
{ | ||
$result = $this->getClient()->createObjectDir($this->bucket, rtrim($dirName, '/')); | ||
if ($result !== null) { | ||
return false; | ||
} | ||
return ['path' => $dirName]; | ||
} | ||
|
||
/** | ||
* 获取 Bucket 中所有文件的文件名,返回 Array。 | ||
* @return array | ||
*/ | ||
public function getAllObjectKey() | ||
{ | ||
$objectListing = $this->getClient()->listObjects(['Bucket' => $this->bucket]); | ||
$objectKeys = []; | ||
foreach ($objectListing->getObjectList() as $objectSummary) { | ||
$objectKeys[] = $objectSummary->getKey(); | ||
} | ||
return $objectKeys; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
Yii2 Aliyun OSS | ||
=============== | ||
Yii2 阿里云 OSS | ||
|
||
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 | ||
----- | ||
|
||
配置文件添加组件 : | ||
|
||
```php | ||
components => [ | ||
'oss' => [ | ||
'class' => 'yiier\AliyunOSS\OSS', | ||
'accessKeyId' => '阿里云OSS AccessKeyID', | ||
'accessKeySecret' => '阿里云OSS AccessKeySecret', | ||
'bucket' => '阿里云的bucket空间', | ||
'lanDomain' => 'OSS内网地址, 如:oss-cn-hangzhou-internal.aliyuncs.com', | ||
'wanDomain' => 'OSS外网地址, 如:oss-cn-hangzhou.aliyuncs.com' | ||
], | ||
] | ||
``` | ||
|
||
```php | ||
$oss = \Yii::$app->get('oss'); | ||
$fh = '/vagrant/php/baseapi/web/storage/image/824edb4e295892aedb8c49e4706606d6.png'; | ||
$oss->upload('824edb4e295892aedb8c49e4706606d6.png', $fh); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "yiier/yii2-aliyun-oss", | ||
"description": "Yii2 阿里云 OSS", | ||
"type": "yii2-extension", | ||
"keywords": ["yii2","extension","AliyunOSS","OSS"], | ||
"license": "BSD-3-Clause", | ||
"authors": [ | ||
{ | ||
"name": "forecho", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"require": { | ||
"yiisoft/yii2": "*", | ||
"aliyuncs/oss-sdk-php": ">2.0" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"yiier\\AliyunOSS\\": "" | ||
} | ||
} | ||
} |