我感觉阿里云写的文档有点牛逼.这一段那一段的零零散散。看个文档跟脑筋急转弯一样目前只用过又拍云和阿里云的第三方上传.感觉又拍云的文档写的太好了!还有那在线客服就很棒啊.而阿里的还要提交工单来对话.效率很低有空的话再试试七牛云和腾讯云的.据说这方面阿里拍最后.只是店大而已.这个OSS并不怎么样
不多说了,祸从口出,但我丝毫不怕!
修改App\Lib\Aliyun\Alivod
增加获取图片上传凭证
/**
* 获取图片上传地址和凭证
* @param client 发送请求客户端
* @return CreateUploadImageResponse 获取图片上传地址和凭证响应数据
*/
public function createUploadImage($imageType, $imageExt)
{
$request = new vod\CreateUploadImageRequest();
$request->setImageType($imageType);
$request->setImageExt($imageExt);
$request->setAcceptFormat('JSON');
$res = $this->client->getAcsResponse($request);
}
修改App\Lib\Upload\AliyunUpload
增加图片OSS上传
use App\Lib\Aliyun\Alivod;
use think\Exception;
class AliyunUpload
{
/*上传本地文件到阿里云*/
static public function videoUploadFile($title, $fileName, $filePath)
{
$aliObj = new Alivod();
$res = $aliObj->createUploadVideo($title, $fileName);
$videoId = $res->VideoId;
$uploadAddress = json_decode(base64_decode($res->UploadAddress), true);
$uploadAuth = json_decode(base64_decode($res->UploadAuth), true);
$ossInit = $aliObj->initOssClient($uploadAuth, $uploadAddress);
$ossUpload = $aliObj->uploadLocalFile($uploadAddress, $filePath);
// 200=成功
if($ossUpload['info']['http_code'] != 200 && empty($videoId)){
throw new \Exception('OSS视频上传失败');
}
return [
'code' => $ossUpload['info']['http_code'],
'videoId' => $videoId,
];
}
static public function imageUploadFile($imageType, $imageExt, $filePath)
{
$aliObj = new Alivod();
$res = $aliObj->createUploadImage($imageType, $imageExt);
/*获取完凭证后拿到图片id*/
$imageUrl = $res->ImageURL;
$imageId = $res->ImageId;
$uploadAddress = json_decode(base64_decode($res->UploadAddress), true);
$uploadAuth = json_decode(base64_decode($res->UploadAuth), true);
$ossInit = $aliObj->initOssClient($uploadAuth, $uploadAddress);
$ossUpload = $aliObj->uploadLocalFile($uploadAddress, $filePath);
/*如果上传状态=200 并且 有返回图片Id*/
if($ossUpload['info']['http_code'] != 200 && empty($imageUrl)){
throw new \Exception('OSS图片上传失败');
}
return [
'code' => $ossUpload['info']['http_code'],
'imageId' => $imageId,
'imageUrl' => $imageUrl
];
}
}
调用上传
namespace App\HttpController\Api;
use App\HttpController\Api\Base;
use EasySwoole\Component\Di;
use App\Lib\Upload\Video;
use App\Lib\Upload\Image;
use think\Exception;
use App\Lib\ClassArr;
class Upload extends Base
{
/*文件上传 视频 | 图片*/
public function file()
{
try {
$request = $this->request();
$files = $request->getSwooleRequest()->files;
if(empty($files)){
return $this->writeJson(400, '获取需上传文件失败');
}
//获取传参的健 video | image
$types = array_keys($files);
$this->type = $types[0];
$obj = new ClassArr();
$uploadClass = $obj->uploadClassStat();
$uploadObj = $obj->initClass($this->type,$uploadClass,[$request,$this->type]);
$file = $uploadObj->upload();
} catch (\Exception $e) {
return $this->writeJson(400, $e->getMessage());
}
if (empty($file)) {
return $this->writeJson(400, '上传失败');
}
$data = [
'url' => $file
];
return $this->writeJson(200, 'ok', $file);
}
}
测试
图片结果
视频结果
显而易见都是成功的.但我还是要展示!谁让我这么优秀呢