/
BosService.php
72 lines (59 loc) · 1.47 KB
/
BosService.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
<?php
/*
*
*/
include 'bce-php-sdk/BaiduBce.phar';
use BaiduBce\Services\Bos\BosClient;
class BosService
{
private $client;
private $bucket;
private $domain;
private $endpoint = 'http://bj.bcebos.com';
public function __construct($accessKey, $secretKey, $bucket, $domain)
{
$BOS_CONFIG = array(
'credentials' => array(
'ak' => $accessKey,
'sk' => $secretKey,
),
'endpoint' => $this->endpoint,
);
$this->client = new BosClient($BOS_CONFIG);
$this->bucket = $bucket;
$this->domain = $domain;
$this->checkBucket($this->bucket);
}
protected function checkBucket($bucketName)
{
$exist = $this->client->doesBucketExist($bucketName);
if (! $exist) {
$this->client->createBucket($bucketName);
}
}
public function uploadFile($filename, $path)
{
return $this->client->putObjectFromFile($this->bucket, $path, $filename);
}
public function uploadFileWithData($data, $path)
{
$this->client->putObjectFromString($this->bucket, $path, $data);
}
public function removeFile($path)
{
$this->client->deleteObject($this->bucket, $path);
}
public function getObject($path)
{
return $this->client->getObjectAsString($this->bucket, $path);
}
public function getObjectMeta($path)
{
$meta = $this->client->getObjectMetadata($this->bucket, $path);
return $meta;
}
public function getObjectUrl($path)
{
return Typecho_Common::url($path, empty($this->domain) ? ($this->endpoint . '/' . $this->bucket) : ('http://' . $this->domain));
}
}