quanwei
3 days ago 04102f7237efefa744090ed7c25f7b5d0807b679
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
<?php
 
namespace app\common\library\storage\engine;
 
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;
use Qiniu\Storage\BucketManager;
 
/**
 * 七牛云存储引擎
 */
class Qiniu extends Server
{
    private $config;
 
    /**
     * 构造方法
     */
    public function __construct($config)
    {
        parent::__construct();
        $this->config = $config;
    }
 
    /**
     * 执行上传
     */
    public function upload()
    {
        // 要上传图片的本地路径
        $realPath = $this->getRealPath();
 
        // 构建鉴权对象
        $auth = new Auth($this->config['access_key'], $this->config['secret_key']);
 
        // 要上传的空间
        $token = $auth->uploadToken($this->config['bucket']);
 
        // 初始化 UploadManager 对象并进行文件的上传
        $uploadMgr = new UploadManager();
 
        // 调用 UploadManager 的 putFile 方法进行文件的上传
        list(, $error) = $uploadMgr->putFile($token, $this->fileName, $realPath);
 
        if ($error !== null) {
            $this->error = $error->message();
            return false;
        }
        return true;
    }
 
    /**
     * 删除文件
     */
    public function delete($fileName)
    {
        // 构建鉴权对象
        $auth = new Auth($this->config['access_key'], $this->config['secret_key']);
        // 初始化 UploadManager 对象并进行文件的上传
        $bucketMgr = new BucketManager($auth);
        $error = $bucketMgr->delete($this->config['bucket'], $fileName);
        if ($error !== null) {
            $this->error = $error->message();
            return false;
        }
        return true;
    }
 
    /**
     * 返回文件路径
     */
    public function getFileName()
    {
        return $this->fileName;
    }
 
}