Tp5 使用華為雲obs


思路:

去官網配置華為雲obs,然后在tp5填好參數,上傳成功返回路徑存入數據庫即可

 

封裝華為雲上傳配置,可以去官網查看自己的參數

namespace app\common\upload;


use Obs\ObsClient;
use Obs\ObsException;
use think\facade\App;

class UploadObs
{
    public function uploads($path, $file, $type, $bucket)
    {
        //實例化
        $obsClient = new ObsClient([
            'key' => App::config('access_key'),   //華為雲obs的access key
            'secret' => App::config('obs_secret'),  //華為雲obs的secret
            'endpoint' => 'obs.cn-south-1.myhuaweicloud.com', //華為雲obs的地區地址,在華為雲查看定義
        ]);
//創建對象,上傳到華為雲obs
        try {
            $res = $obsClient->putObject([
                'Bucket' => $bucket, //桶名
                'Key' => $path, //存儲路徑
                'Body' => $file,  //文件對象
                'ContentType' => $type, //文件類型
            ]);
        } catch (ObsException $e) {
            return $e->getMessage();
        }
//關閉對象
        $obsClient->close();
        $url = 'https://' . $bucket . '.' . App::config('endpoint') . '/' . $path;
        return $url;
    }
}

 

 

封裝文件上傳,upload方法參數路徑可以自己去掉

 

<?php


namespace app\common\upload;

use app\common\base\Base;
use think\Exception;


class FileUpload extends Base
{
    public function fileValidate($file) //自定義文件驗證類
    {
        $rule = ['BMP', 'JPG', 'JPEG', 'PNG', 'GIF'];                                   //規定格式
        $ext = strstr(strrev($file->getInfo()['name']), '.', true);  //倒序截取,獲取后綴
        $ext = strrev(strtoupper($ext));                                                //再倒序和變成大寫后綴

        if ($file->getInfo()['size'] > 1000000)                                         //文件小於10m
        {
            $this->create( '', '文件不能超過10m!', 400)->send();die;
        }
        if (!in_array($ext, $rule))                                                     //是否為這些文件類型
        {
            $this->create( '', '文件類型錯誤!', 400)->send();die;
        }
    }

    /**
     * 多文件上傳,返回數組路徑
     *
     * @param $request
     * @param $param       //請求參數名
     * @param $field       //id目錄名 user_id等
     * @param $catalog     //數據表目錄名 doctor等
     */
    public function uploads($request, $param, $field, $catalog)
    {
        $files = $request->file($param);             //前端參數名為xxx[]
        $id = $request->$field;                      //用戶id
        if (!(is_array($files)) || !(count($files))) //需要數組而且不為空
            return $this->arrays(false, '', '', '上傳錯誤!');

        //拼接保存目錄 user_id5 / diary / 20210601
        $key = $field . $id . '/' . $catalog . '/'.date("Ymd");

        //多文件上傳
        try {
            $up = new UploadObs();          //上傳華為雲obs
            foreach ($files as $file) {
                $this->fileValidate($file); //驗證文件

                $file_name = uniqid($id) . '_' . $file->getInfo()['name'];  //文件名
                $file_type = $file->getInfo()['type'];                      //文件后綴
                $file_path = $key . date("Ymd") . '/' . $file_name;  //保存的文件路徑
                $url[$file_name] = ($up->uploads($file_path, $file, $file_type, 'fits')); //返回的訪問路徑
            }
            return $this->arrays(true, $url, '多文件上傳成功'); //返回路徑
        } catch (Exception $e) {
            return $this->create('', '$e->getMessage()', 400);
        }
    }

    /**
     * 單文件上傳,返回文件路徑
     *
     * @param $request
     * @param $param       //請求參數名 photo
     * @param $field       //id目錄名 user_id等
     * @param $catalog     //數據表目錄名 diary等
     */
    public function upload($request, $param, $field, $catalog,$saveId=null)
    {
        $file = $request->file($param);      //前端參數名name = $param
        $id = $request->$field ?? $saveId;   //id
        if (!$file || !($file->getInfo()))   //不為空且為文件類型
            return $this->arrays(false, '', '', '上傳錯誤!');

        //拼接保存目錄 user_id5 / diary / 20210601
        $key = $field . $id . '/' . $catalog . '/'.date("Ymd");

        //單文件上傳
        try {
            $this->fileValidate($file);     //驗證文件
            $up = new UploadObs();          //上傳華為雲obs
            $file_name = uniqid() . '_' . $file->getInfo()['name']; //文件名
            $file_type = $file->getInfo()['type'];                  //文件后綴
            $file_path = $key . '/' . $file_name;                   //保存的文件路徑
            $d['url'] = ($up->uploads($file_path, $file, $file_type, 'fits')); //返回的訪問路徑
            return $this->arrays(true, $d, '單文件上傳成功'); //返回路徑
        } catch (Exception $e) {
            return $e->getMessage();
        }
    }

}

 

 

測試多文件上傳

public function uploads()  //多文件上傳
{
    $this->is_num($this->request->user_id);
    $res = (new FileUpload())->uploads($this->request, 'photos', 'user_id', 'diary');
    return json($res);
}

 

 

效果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM