七牛云 composer 文件上传、删除、请除缓存操作

class QiniuUp extends ModelBasic
{
    private $AccessKey;
    private $SecretKey;
    private $bucket;
    private $auth;
    function __construct()
    {
        parent::__construct();
        $this->AccessKey = config('qiniu.AccessKey');
        $this->SecretKey = config('qiniu.SecretKey');
        $this->bucket = config('qiniu.bucket');
        vendor('qiniu.php-sdk.autoload');
        $this->auth = new Auth($this->AccessKey, $this->SecretKey);
    }

    /**
       * Author: MrLi (602952118@qq.com)
       * Desc: 七牛云上传
       * Param:
       * Return:
       * Data: 2019/11/20 9:42
     */
    public function upload($fileName = '', $bucket = '')
    {

        //文件获取、处理
        $file = request()->file($fileName);
        $info = $file->getInfo();
        $s_name = $info["name"];
        // 上传文件的本地路径
        $filePath = $file->getRealPath();

        //文件后缀
        $extension = pathinfo($s_name, PATHINFO_EXTENSION);

        //获取七牛token
        $bucket = empty($bucket) ? $this->bucket : $bucket;
        $token = $this->auth->uploadToken($bucket);

        //上传到七牛后保存的文件名
        $folder = date('Ymd')."/";
        $name = time() . rand(0, 9999) . '.' . $extension;
        $key = config('qiniu.folder').$folder.$name;

        //初始化UploadManager对象
        $uploadManager = new UploadManager();

        //文件上传
        list($result, $error) = $uploadManager->putFile($token, $key, $filePath);
        if ($error !== null) {
            return ['errNo' => 1, 'errMsg' => $error, 'data' => $this->SecretKey];
        } else {
            return ['errNo' => 0, 'data' => ['s_name' => $s_name,'up_name' => $key,"size"=>$info["size"], 'url' => config('qiniu.head').config('qiniu.domain').$key]];
        }
    }
    /**
     * 获取私有空间或使用了原图保护功能的图片文件地址
     * @param string $url 格式:http://domain/key[文件名]?e=时间戳
     * @return string 可访问的url地址:http://domain/key[文件名]?e=时间戳&token='token'
     */
    public function getSignedUrl($url)
    {
        $signedUrl = $this->auth->privateDownloadUrl($url);
        //该url地址需要验证是否可访问。
        return $signedUrl;
    }
    /**
       * Author: MrLi (602952118@qq.com)
       * Desc: 删除七牛云存储的数据
       * Param: 
       * Return: 
       * Data: 2020/3/18 13:53
     */
    function delete($filename){
        $bm = new BucketManager($this->auth,new Config());
        $err = $bm->delete($this->bucket,$filename);
        return $err;
    }
    /**
       * Author: MrLi (602952118@qq.com)
       * Desc: 刷新文件缓存
       * Param: 
       * Return: 
       * Data: 2020/3/18 13:53
     */
    function refreshBuffer($urls){
        $cdn = new CdnManager($this->auth);
        $re = $cdn->refreshUrls($urls);
        return $re;
    }
}

注意:前提是引入七牛云的基础包 composer require qiniu/php-sdk

配置文件:

//七牛云空间配置
    'qiniu' => [
        'AccessKey'     =>      '你的KEY',
        'SecretKey'     =>      '你的Secret',
        'bucket'        =>      '你的bucket',
        'domain'        =>      '你的网址/',
        'head'          =>      'http://',
        'folder'        =>      'videos/'
    ]

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李了凡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值