1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
// +---------------------------------------------------------------------- |
4
|
|
|
// | ThinkPHP6阿里云存储 for ThinkLibrary 6.0 |
5
|
|
|
// +---------------------------------------------------------------------- |
6
|
|
|
// | 版权所有 2017~2020 [ https://www.dtapp.net ] |
7
|
|
|
// +---------------------------------------------------------------------- |
8
|
|
|
// | 官方网站: https://www.dtapp.net |
9
|
|
|
// +---------------------------------------------------------------------- |
10
|
|
|
// | 开源协议 ( https://mit-license.org ) |
11
|
|
|
// +---------------------------------------------------------------------- |
12
|
|
|
// | 国内仓库地址 :https://gitee.com/dtapps/aliyun-oss |
13
|
|
|
// | 国外仓库地址 :https://github.com/dtapps/aliyun-oss |
14
|
|
|
// | Packagist 地址 :https://packagist.org/packages/dtapps/aliyun-oss |
15
|
|
|
// +---------------------------------------------------------------------- |
16
|
|
|
|
17
|
|
|
namespace DtApp\AliYun\aliyun; |
18
|
|
|
|
19
|
|
|
use DtApp\ThinkLibrary\Service; |
20
|
|
|
use OSS\Core\OssException; |
21
|
|
|
use OSS\OssClient; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* 定义当前版本 |
25
|
|
|
*/ |
26
|
|
|
const VERSION = '1.0.0'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* 阿里云对象存储 |
30
|
|
|
* https://www.aliyun.com/product/oss |
31
|
|
|
* Class OssService |
32
|
|
|
* @package DtApp\AliYun\aliyun |
33
|
|
|
*/ |
34
|
|
|
class OssService extends Service |
35
|
|
|
{ |
36
|
|
|
private $accessKeyId, $accessKeySecret, $endpoint, $bucket; |
37
|
|
|
|
38
|
|
|
public function accessKeyId(string $accessKeyId) |
39
|
|
|
{ |
40
|
|
|
$this->accessKeyId = $accessKeyId; |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function accessKeySecret(string $accessKeySecret) |
45
|
|
|
{ |
46
|
|
|
$this->accessKeySecret = $accessKeySecret; |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function endpoint(string $endpoint) |
51
|
|
|
{ |
52
|
|
|
$this->endpoint = $endpoint; |
53
|
|
|
return $this; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function bucket(string $bucket) |
57
|
|
|
{ |
58
|
|
|
$this->bucket = $bucket; |
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* 获取配置信息 |
64
|
|
|
* @return \DtApp\ThinkLibrary\service\aliyun\OssService |
65
|
|
|
*/ |
66
|
|
|
private function getConfig() |
67
|
|
|
{ |
68
|
|
|
$this->accessKeyId = config('dtapp_aliyun.oss.access_key_id'); |
69
|
|
|
$this->accessKeySecret = config('dtapp_aliyun.oss.access_key_secret'); |
70
|
|
|
$this->endpoint = config('dtapp_aliyun.oss.endpoint'); |
71
|
|
|
$this->bucket = config('dtapp_aliyun.oss.bucket'); |
72
|
|
|
return $this; |
|
|
|
|
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* 上传文件 |
77
|
|
|
* @param $object |
78
|
|
|
* @param $filePath |
79
|
|
|
* @return bool|string |
80
|
|
|
*/ |
81
|
|
|
public function upload(string $object, string $filePath) |
82
|
|
|
{ |
83
|
|
|
if (empty($this->accessKeySecret) || empty($this->accessKeySecret) || empty($this->endpoint) || empty($this->bucket)) { |
84
|
|
|
$this->getConfig(); |
85
|
|
|
} |
86
|
|
|
try { |
87
|
|
|
$ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint); |
88
|
|
|
$ossClient->uploadFile($this->bucket, $object, $filePath); |
89
|
|
|
return config('dtapp_aliyun.oss.url', '') . $object; |
90
|
|
|
} catch (OssException $e) { |
91
|
|
|
return $e->getMessage(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |