Skip to content

Commit 79a0b06

Browse files
committed
feat:yii版腾讯云cos-filesystem
1 parent 4c28d30 commit 79a0b06

File tree

5 files changed

+145
-30
lines changed

5 files changed

+145
-30
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ composer.lock
88
.subsplit
99
.php_cs.cache
1010
cghooks.lock
11+
.phpunit.result.cache
1112

README.md

+57-22
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,63 @@
1-
# PHP Package Template
1+
<div style="text-align: center"> <h1>yii-filesystem-cos</h1> </div>
22

3-
A template repository for PHP package.
3+
<a href="https://packagist.org/packages/vartruexuan/yii-filesystem-cos" rel="nofollow"><img src="https://badgen.net/github/tag/vartruexuan/yii-filesystem-cos" alt="Latest Version" data-canonical-src="https://badgen.net/github/tag/vartruexuan/yii-filesystem-cos" style="max-width: 100%;"></a>
4+
<a href="https://www.php.net" rel="nofollow"><img src="https://img.shields.io/badge/php-%3E=7.4-brightgreen.svg?maxAge=2592000" alt="Php Version" data-canonical-src="https://img.shields.io/badge/php-%3E=7.4-brightgreen.svg?maxAge=2592000" style="max-width: 100%;"></a>
45

5-
## Installing
6+
# 概述
7+
[creocoder/yii2-flysystem](https://github.com/creocoder/yii2-flysystem)
8+
9+
# 安装
610

711
```shell
8-
$ composer require vendor/package -vvv
12+
composer require vartruexuan/yii-filesystem-cos
913
```
1014

11-
## Usage
12-
13-
TODO
14-
15-
16-
## Contributing
17-
18-
You can contribute in one of three ways:
19-
20-
1. File bug reports using the [issue tracker](https://github.com/vendor/package/issues).
21-
2. Answer questions or fix bugs on the [issue tracker](https://github.com/vendor/package/issues).
22-
3. Contribute new features or update the wiki.
23-
24-
_The code contribution process is not very formal. You just need to make sure that you follow the PSR-0, PSR-1, and PSR-2 coding guidelines. Any new code contributions must be accompanied by unit tests where applicable._
25-
26-
## License
27-
28-
MIT
15+
# 使用
16+
17+
## 配置组件
18+
19+
```php
20+
// 加入配置组件
21+
'components' => [
22+
'cos' => [
23+
'class' => 'Vartruexuan\Yii2\Filesystem\Cos\Filesystem',
24+
// 配置
25+
'region' => 'gz',
26+
'appId' => '1272757004',
27+
'secretId' => 'AKIDd5HCSPDanlU1ILnzwc2sSmpcIqTY',
28+
'secretKey' => '58kxufPxEiiSWIbeuUqxsYzDB3gAPVLwI',
29+
'token' => null,
30+
'timeout' => 60,
31+
'connect_timeout' => 60,
32+
'bucket' => 'bucket-1272757004',
33+
'cdn' => 'https://bucket-1272757004.file.myqcloud.com',
34+
'scheme' => 'https',
35+
'read_from_cdn' => false,
36+
'cdn_key' => '',
37+
'encrypt' => false,
38+
],
39+
],
40+
```
41+
## 使用
42+
```php
43+
bool Yii::$app->cos->->write('file.md', 'contents');
44+
bool Yii::$app->cos->->writeStream('file.md', fopen('path/to/your/local/file.jpg', 'r'));
45+
bool Yii::$app->cos->->update('file.md', 'new contents');
46+
bool Yii::$app->cos->->updateStram('file.md', fopen('path/to/your/local/file.jpg', 'r'));
47+
bool Yii::$app->cos->->rename('foo.md', 'bar.md');
48+
bool Yii::$app->cos->->copy('foo.md', 'foo2.md');
49+
bool Yii::$app->cos->->delete('file.md');
50+
bool Yii::$app->cos->->has('file.md');
51+
string|false Yii::$app->cos->->read('file.md');
52+
array Yii::$app->cos->->listContents();
53+
array Yii::$app->cos->->getMetadata('file.md');
54+
int Yii::$app->cos->->getSize('file.md');
55+
string Yii::$app->cos->->getUrl('file.md');
56+
string Yii::$app->cos->->getTemporaryUrl('file.md', date_create('2018-12-31 18:12:31'));
57+
string Yii::$app->cos->->getMimetype('file.md');
58+
int Yii::$app->cos->->getTimestamp('file.md');
59+
string Yii::$app->cos->->getVisibility('file.md');
60+
bool Yii::$app->cos->->setVisibility('file.md', 'public'); //or 'private', 'default'
61+
// ...
62+
```
63+
[详细文档](https://flysystem.thephpleague.com/api/)

composer.json

+14-8
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
{
2-
"name": "vartruexuan/php-package",
3-
"description": "A PHP package template repository.",
2+
"name": "vartruexuan/yii-filesystem-cos",
3+
"description": "yii2 filesystem 腾讯云cos",
44
"license": "MIT",
55
"authors": [
66
{
77
"name": "vendor",
88
"email": "name@domain.com"
99
}
1010
],
11+
"keywords": [
12+
"yii2",
13+
"filesystem",
14+
"files",
15+
"cos"
16+
],
1117
"require": {
12-
"php": ">=7.4"
18+
"php": ">=7.4",
19+
"creocoder/yii2-flysystem": "^1.1",
20+
"freyo/flysystem-qcloud-cos-v5": "^2.2"
1321
},
1422
"require-dev": {
1523
"brainmaestro/composer-git-hooks": "^2.8",
@@ -21,18 +29,16 @@
2129
},
2230
"autoload": {
2331
"psr-4": {
24-
"Vendor\\Package\\": "src"
32+
"Vartruexuan\\Yii\\Filesystem\\Cos\\": "src"
2533
}
2634
},
2735
"extra": {
2836
"hooks": {
2937
"pre-commit": [
30-
"composer check-style",
31-
"composer test"
38+
3239
],
3340
"pre-push": [
34-
"composer test",
35-
"composer check-style"
41+
3642
]
3743
}
3844
},

src/.gitkeep

Whitespace-only changes.

src/Filesystem.php

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Vartruexuan\Yii\Filesystem\Cos;
4+
5+
use Freyo\Flysystem\QcloudCOSv5\Adapter;
6+
use Qcloud\Cos\Client;
7+
8+
class Filesystem extends \creocoder\flysystem\Filesystem
9+
{
10+
public $region;
11+
public $appId;
12+
public $secretId;
13+
public $secretKey;
14+
public $token;
15+
public $timeout = 60;
16+
public $connect_timeout = 60;
17+
public $scheme = 'https';
18+
public $bucket;
19+
public $encrypt = false;
20+
public $cdn = '';
21+
public $cdn_key = '';
22+
public $read_from_cdn = '';
23+
24+
public function init()
25+
{
26+
parent::init();
27+
}
28+
29+
30+
/**
31+
* 设置适配器
32+
*
33+
* @return \League\Flysystem\AdapterInterface|void
34+
*
35+
* @date 2023/3/9
36+
* @author vartruexuan
37+
*/
38+
protected function prepareAdapter()
39+
{
40+
$client = new Client($this->getConfig());
41+
return new Adapter($client, $this->getConfig());
42+
}
43+
44+
/**
45+
* 配置信息
46+
*
47+
* @return array
48+
*
49+
* @date 2023/3/9
50+
* @author vartruexuan
51+
*/
52+
protected function getConfig()
53+
{
54+
return [
55+
'region' => $this->region,
56+
'credentials' => [
57+
'appId' => $this->appId,
58+
'secretId' => $this->secretId,
59+
'secretKey' => $this->secretKey,
60+
'token' => $this->token,
61+
],
62+
'timeout' => $this->timeout,
63+
'connect_timeout' => $this->connect_timeout,
64+
'bucket' => $this->bucket,
65+
'cdn' => $this->cdn,
66+
'scheme' => $this->scheme,
67+
'read_from_cdn' => $this->read_from_cdn,
68+
'cdn_key' => $this->cdn_key,
69+
'encrypt' => $this->encrypt,
70+
];
71+
}
72+
73+
}

0 commit comments

Comments
 (0)