ThinkPHP 阿里云图片上传

public function abing(Request $request){
        if($request->isPost()){
      //composer require aliyuncs/oss-sdk-php $file = $request->file('filed'); $accessKeyId = "LTAI4FpYGu9bgnf"; $accessKeySecret = "K9rN5hPMYu4RW3VyKxNg"; // Endpoint以杭州为例,其它Region请按实际情况填写。 $endpoint = "http://oss-cn-shanghai.aliyuncs.com"; // 存储空间名称 $bucket= "abing9"; // 上传文件到OSS时需要指定包含文件后缀在内的完整路径,例如abc/efg/123.jpg $info = $file->move(ROOT_PATH.'public'.DS.'uploads'); $path = $info->getSaveName(); $content = ROOT_PATH.'public'.DS.'uploads/'.$path; $object = $info->getFilename(); try { $ossClinet = new OssClient($accessKeyId,$accessKeySecret,$endpoint); $res = $ossClinet->uploadFile($bucket,$object,$content); print_r($res['info']['url']);//这里可以单独处理下异常 }catch (OssException $exception){ print_r($exception); } }else{ echo 3333; return $this->fetch(); } }

  图片处理文档:https://help.aliyun.com/document_detail/47735.html?spm=a2c4g.11186623.6.1090.29fa1329s237Sx

以下不经过本地保存

public function abing(Request $request){
        if($request->isPost()){
           $file = $request->file('filed');
            $accessKeyId = "LTAI4FpYnf";
            $accessKeySecret = "K9rN5h";
            // Endpoint以杭州为例,其它Region请按实际情况填写。
            $endpoint = "http://oss-cn-shanghai.aliyuncs.com";
            // 存储空间名称
            //https://help.aliyun.com/document_detail/47735.html?spm=a2c4g.11186623.6.1090.29fa1329s237Sx
            $bucket= "abing9";
            $info = $file->getInfo();
            $object = rand(1000,9999).'.png';  //文件名
            $content = $info['tmp_name'];//文件内容
            try {
                $ossClinet = new OssClient($accessKeyId,$accessKeySecret,$endpoint);
                $res = $ossClinet->uploadFile($bucket,$object,$content);
                print_r($res['info']['url']);
            }catch (OssException $exception){
                print_r($exception);
            }


        }else{
            return $this->fetch();
        }
    }

 阿里云视频点播上传视频文件如下:

public function video(Request $request){
        //if($request->isPost()){
            require_once ROOT_PATH.'vendor\aliyuncs\aliyun-php-sdk-core\Config.php';
            require_once ROOT_PATH."vendor\aliyuncs\Autoloader.php";

            $accessKeyId = "LTAI49bgnf";
            $accessKeySecret = "K9rN55uyKxNg";
            $regionId = 'cn-shanghai';

            try {
                $uploader = new \AliyunVodUploader($accessKeyId,$accessKeySecret);
                $uploadVideoRequest = new \UploadVideoRequest('D:\test\sample.mp4','estUploadWebVideo via PHP-SDK');
                $uploadVideoRequest->setTitle('mytitle');//会生成视频的名称

                $res = $uploader->uploadLocalVideo($uploadVideoRequest);
                var_dump('生成的ID编号为'.$res); //返回一大片.生成视频唯一ID的字符串

            }catch (Exception $e){
                print_r($e->getMessage());
            }
    }

 

你可能感兴趣的:(ThinkPHP 阿里云图片上传)