ServiceProviderOss.php
2.61 KB
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
<?php namespace Resource;
use OSS\Core\OssException;
use OSS\OssClient;
/**
* Class ServiceProviderOss
* @package resource
*/
class ServiceProviderOss extends ServiceProviderAbstract
{
/**
* @var OssClient
*/
private $client;
/**
* @var
*/
private $bucket;
/**
* @var
*/
private $styleUrl;
/**
* @var
*/
private $url;
/**
* ServiceProviderOss constructor.
* @param $options
*/
public function __construct($options)
{
$accessKeyId = $options['accessKeyId'];
$accessKeySecret = $options['accessKeySecret'];
$endpoint = $options['endpoint'];
$this->bucket = $options['bucket'];
$this->styleUrl = $options['styleUrl'];
$this->url = $options['url'];
try {
$this->client= new OssClient($accessKeyId, $accessKeySecret, $endpoint ,true);
} catch (OssException $e) {
throw $e;
}
}
/**
* @param $path
* @param $destPath
* @return null
*/
public function add($path, $destPath)
{
return $this->client->uploadFile($this->bucket, $destPath, $path);
}
/**
* @param $path
* @param null $style
* @return string
*/
public function getUrl($path, $style = null)
{
if ($style) {
return $path ? $this->styleUrl . '/' . $path . '!' . $style : $this->styleUrl;
} else {
return $path ? $this->url . '/' . $path : $this->url;
}
}
/**
* @param $path
*/
public function delete($path)
{
$this->client->deleteObject($this->bucket, $path);
}
/**
* @param $oldPath
* @param $newPath
* @return bool|null
*/
public function replace($oldPath, $newPath, $tmpPath)
{
// 这两个函数是否成功都返回null, By LCN
$this->client->deleteObject($this->bucket, $oldPath);
$this->client->uploadFile($this->bucket, $newPath, $tmpPath);
}
/**
* @param $path
* @return bool
*/
public function exists($path)
{
return $this->client->doesObjectExist($this->bucket, $path);
}
/**
* @param $path
* @return bool
*/
public function move($oldPath, $newPath)
{
$this->client->copyObject($this->bucket, $oldPath, $this->bucket, $newPath);
$this->delete($oldPath);
}
/**
* @param Array $paths
*/
public function batchDelete($paths)
{
$this->client->deleteObjects($this->bucket, $paths);
}
public function getClient()
{
return $this->client;
}
}