小程序腾讯云视频上传

wxml :

<button bindtap="video">视频上传</button>

 js:

  video(){
       wx.chooseVideo({
         camera: 1,
         success:(res)=>{
            wx.uploadFile({
              filePath: res.tempFilePath,
              name: 'file',
              url: 'http://www.cs.com/api/demo/index',
              success:(res)=>{
                    console.log(res);
              }
            })
         }
       })
  }

php

 public function index(Request $request)
    {
        $pathName = ($request->file("file"))->getPathname();
        // SECRETID和SECRETKEY请登录访问管理控制台进行查看和管理
        $secretId = ""; //"云 API 密钥 SecretId";
        $secretKey = ""; //"云 API 密钥 SecretKey";
        $region = "ap-shanghai"; //设置一个默认的存储桶地域
        $cosClient = new \Qcloud\Cos\Client(
            array(
                'region' => $region,
                'schema' => 'https', //协议头部,默认为http
                'credentials' => array(
                    'secretId' => $secretId,
                    'secretKey' => $secretKey)));
             $name=rand(111, 999) . time() . ".mp4";
        ### 上传文件流
        try {
            $bucket = "1811-1304504601"; //存储桶名称 格式:BucketName-APPID
            $key = $name; //此处的 key 为对象键,对象键是对象在存储桶中的唯一标识
            $srcPath = $pathName;//本地文件绝对路径
            $file = fopen($srcPath, "rb");
            if ($file) {
                $result = $cosClient->putObject(array(
                    'Bucket' => $bucket,
                    'Key' => $key,
                    'Body' => $file));
//                return json_decode($result, true);
                  $video="http://cdn加速域名/{$name}";
                return json(["error_code"=>0,"data"=>$video,"msg"=>"上传成功"]);
            }
        } catch (\Exception $e) {
            return json(["error_code"=>1,"data"=>$e,"msg"=>"失败"]);
        }
    }

  返回的路径是cdn加速域名拼上视频名,加防盗链可能会有影响

猜你喜欢

转载自blog.csdn.net/weixin_45604963/article/details/120075344
今日推荐