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
<?php
use OSS\OssClient;
USE OSS\Core\OssException;
require_once (dirname ( __FILE__ ) . '/../../extralib/autoload.php');
Class AliUploadHandler{
var $Access_Key ;
var $Secret_Key ;
var $bucket ;
var $endpoint ;
public function __construct(){
$this->init();
}
public function init(){
$this->Access_Key = OSS_ACCESS_KEY;
$this->Secret_Key = OSS_ACCESS_SECRET;
$this->bucket = OSS_BUCKET;
$this->endpoint = OSS_END_POINT;
}
/**
* 上传接口
* @Author
* @DateTime 2017-03-08
* @param string $bucket bucket name
* @param string $object object name
* @param string $file local file path
* @param array $options
* @throws OssException [文件类型]
*/
public function uploadImg($dst,$src){
//获取对象
$auth = new OssClient($this->Access_Key,$this->Secret_Key,$this->endpoint);
try {
//上传图片
$result = $auth->uploadFile($this->bucket,$dst,$src);
return $result['info']['url'];
} catch (OssException $e) {
ErrorLogger::doOutput(__FUNCTION__ . "uploadImg: FAILED\n");
ErrorLogger::doOutput($e->getMessage() . "\n");
return $e->getMessage();
}
ErrorLogger::doOutput(__FUNCTION__ . "uploadImg: OK" . "\n");
}
/**
* @Author
* @DateTime 2018-03-08
* @return [type] [description]
*/
public function uploadVideo($dst,$src){
$ossClient = new OssClient($this->Access_Key,$this->Secret_Key,$this->endpoint);
try{
$result = $ossClient->multiuploadFile($this->bucket,$dst,$src);
return $result['info']['url'];
} catch(OssException $e) {
ErrorLogger::doOutput(__FUNCTION__ . "uploadVideo: FAILED\n");
ErrorLogger::doOutput($e->getMessage() . "\n");
return;
}
ErrorLogger::doOutput(__FUNCTION__ . "uploadVideo: OK" . "\n");
}
/**
* 删除文件
*
* @param $srcFile 文件路径
*
*/
public function delete($desPath)
{
$ossClient = new OssClient($this->Access_Key,$this->Secret_Key,$this->endpoint);
try{
$ossClient->deleteObject($this->bucket, $desPath);
} catch(OssException $e) {
ErrorLogger::doOutput(__FUNCTION__ . "delete: FAILED\n");
ErrorLogger::doOutput($e->getMessage() . "\n");
return;
}
ErrorLogger::doOutput(__FUNCTION__ . "delete: OK" . "\n");
}
}