-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathBucketCORS.php
108 lines (92 loc) · 3.07 KB
/
BucketCORS.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
<?php
use Qcloud\Cos\Client;
use Qcloud\Cos\Exception\ServiceResponseException;
class BucketCORS
{
private $cosClient;
private $uploadId;
private $eTag;
private $versionId;
// 设置存储桶跨域规则
protected function putBucketCors() {
$cosClient = $this->cosClient;
//.cssg-snippet-body-start:[put-bucket-cors]
try {
$result = $cosClient->putBucketCors(array(
'Bucket' => 'examplebucket-1250000000', //格式:BucketName-APPID
'CORSRules' => array(
array(
'AllowedHeaders' => array('*',),
'AllowedMethods' => array('PUT', ),
'AllowedOrigins' => array('*', ),
'ExposeHeaders' => array('*', ),
'MaxAgeSeconds' => 1,
),
// ... repeated
)
));
// 请求成功
print_r($result);
} catch (\Exception $e) {
// 请求失败
echo "$e\n";
}
//.cssg-snippet-body-end
}
// 获取存储桶跨域规则
protected function getBucketCors() {
$cosClient = $this->cosClient;
//.cssg-snippet-body-start:[get-bucket-cors]
try {
$result = $cosClient->getBucketCors(array(
'Bucket' => 'examplebucket-1250000000' //格式:BucketName-APPID
));
// 请求成功
print_r($result);
} catch (\Exception $e) {
// 请求失败
echo($e);
}
//.cssg-snippet-body-end
}
// 删除存储桶跨域规则
protected function deleteBucketCors() {
$cosClient = $this->cosClient;
//.cssg-snippet-body-start:[delete-bucket-cors]
try {
$result = $cosClient->deleteBucketCors(array(
'Bucket' => 'examplebucket-1250000000' //格式:BucketName-APPID
));
// 请求成功
print_r($result);
} catch (\Exception $e) {
// 请求失败
echo($e);
}
//.cssg-snippet-body-end
}
//.cssg-methods-pragma
protected function init() {
$secretId = "COS_SECRETID"; //"云 API 密钥 SecretId";
$secretKey = "COS_SECRETKEY"; //"云 API 密钥 SecretKey";
$region = "COS_REGION"; //设置一个默认的存储桶地域
$this->cosClient = new Qcloud\Cos\Client(
array(
'region' => $region,
'schema' => 'https', //协议头部,默认为http
'credentials'=> array(
'secretId' => $secretId ,
'secretKey' => $secretKey)));
}
public function mBucketCORS() {
$this->init();
// 设置存储桶跨域规则
$this->putBucketCors();
// 获取存储桶跨域规则
$this->getBucketCors();
// 删除存储桶跨域规则
$this->deleteBucketCors();
//.cssg-methods-pragma
}
}
?>