-
Notifications
You must be signed in to change notification settings - Fork 186
/
OSS.php
187 lines (159 loc) · 5.19 KB
/
OSS.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
namespace App\Services;
use JohnLui\AliyunOSS;
use Exception;
use DateTime;
class OSS {
/* 城市名称:
*
* 经典网络下可选:杭州、上海、青岛、北京、张家口、深圳、香港、硅谷、弗吉尼亚、新加坡、悉尼、日本、法兰克福、迪拜
* VPC 网络下可选:杭州、上海、青岛、北京、张家口、深圳、硅谷、弗吉尼亚、新加坡、悉尼、日本、法兰克福、迪拜
*/
private $city = '青岛';
// 经典网络 or VPC
private $networkType = '经典网络';
private $AccessKeyId = '';
private $AccessKeySecret = '';
private $ossClient;
/**
* 私有初始化 API,非 API,不用关注
* @param boolean 是否使用内网
*/
public function __construct($isInternal = false)
{
if ($this->networkType == 'VPC' && !$isInternal) {
throw new Exception("VPC 网络下不提供外网上传、下载等功能");
}
$this->ossClient = AliyunOSS::boot(
$this->city,
$this->networkType,
$isInternal,
$this->AccessKeyId,
$this->AccessKeySecret
);
}
/**
* 使用外网上传文件
* @param string bucket名称
* @param string 上传之后的 OSS object 名称
* @param string 上传文件路径
* @return boolean 上传是否成功
*/
public static function publicUpload($bucketName, $ossKey, $filePath, $options = [])
{
$oss = new OSS();
$oss->ossClient->setBucket($bucketName);
return $oss->ossClient->uploadFile($ossKey, $filePath, $options);
}
/**
* 使用阿里云内网上传文件
* @param string bucket名称
* @param string 上传之后的 OSS object 名称
* @param string 上传文件路径
* @return boolean 上传是否成功
*/
public static function privateUpload($bucketName, $ossKey, $filePath, $options = [])
{
$oss = new OSS(true);
$oss->ossClient->setBucket($bucketName);
return $oss->ossClient->uploadFile($ossKey, $filePath, $options);
}
/**
* 使用外网直接上传变量内容
* @param string bucket名称
* @param string 上传之后的 OSS object 名称
* @param string 上传的变量
* @return boolean 上传是否成功
*/
public static function publicUploadContent($bucketName, $ossKey, $content, $options = [])
{
$oss = new OSS();
$oss->ossClient->setBucket($bucketName);
return $oss->ossClient->uploadContent($ossKey, $content, $options);
}
/**
* 使用阿里云内网直接上传变量内容
* @param string bucket名称
* @param string 上传之后的 OSS object 名称
* @param string 上传的变量
* @return boolean 上传是否成功
*/
public static function privateUploadContent($bucketName, $ossKey, $content, $options = [])
{
$oss = new OSS(true);
$oss->ossClient->setBucket($bucketName);
return $oss->ossClient->uploadContent($ossKey, $content, $options);
}
/**
* 使用外网删除文件
* @param string bucket名称
* @param string 目标 OSS object 名称
* @return boolean 删除是否成功
*/
public static function publicDeleteObject($bucketName, $ossKey)
{
$oss = new OSS();
$oss->ossClient->setBucket($bucketName);
return $oss->ossClient->deleteObject($bucketName, $ossKey);
}
/**
* 使用阿里云内网删除文件
* @param string bucket名称
* @param string 目标 OSS object 名称
* @return boolean 删除是否成功
*/
public static function privateDeleteObject($bucketName, $ossKey)
{
$oss = new OSS(true);
$oss->ossClient->setBucket($bucketName);
return $oss->ossClient->deleteObject($bucketName, $ossKey);
}
/**
* -------------------------------------------------
*
*
* 下面不再分公网内网出 API,也不注释了,大家自行体会吧。。。
*
*
* -------------------------------------------------
*/
public function copyObject($sourceBuckt, $sourceKey, $destBucket, $destKey)
{
$oss = new OSS();
return $oss->ossClient->copyObject($sourceBuckt, $sourceKey, $destBucket, $destKey);
}
public function moveObject($sourceBuckt, $sourceKey, $destBucket, $destKey)
{
$oss = new OSS();
return $oss->ossClient->moveObject($sourceBuckt, $sourceKey, $destBucket, $destKey);
}
// 获取公开文件的 URL
public static function getPublicObjectURL($bucketName, $ossKey)
{
$oss = new OSS();
$oss->ossClient->setBucket($bucketName);
return $oss->ossClient->getPublicUrl($ossKey);
}
// 获取私有文件的URL,并设定过期时间,如 \DateTime('+1 day')
public static function getPrivateObjectURLWithExpireTime($bucketName, $ossKey, DateTime $expire_time)
{
$oss = new OSS();
$oss->ossClient->setBucket($bucketName);
return $oss->ossClient->getUrl($ossKey, $expire_time);
}
public static function createBucket($bucketName)
{
$oss = new OSS();
return $oss->ossClient->createBucket($bucketName);
}
public static function getAllObjectKey($bucketName)
{
$oss = new OSS();
return $oss->ossClient->getAllObjectKey($bucketName);
}
public static function getObjectMeta($bucketName, $ossKey)
{
$oss = new OSS();
return $oss->ossClient->getObjectMeta($bucketName, $ossKey);
}
}