thinkphp6 swoole上传阿里云oss

  composer require aliyuncs/oss-sdk-php
namespace app\api\controller\v1;

use app\BaseController;
use OSS\OssClient;

class Upload extends BaseController
{

    public function handleUpload(Request $request)
    {

        $image = request()->file('image');
        
        $content = file_get_contents($image);

        $oss = new OssClient(config('aliyun.AccessKeyID'), config('aliyun.AccessKeySecret'), config('aliyun.Endpoint'));
        //获取后缀
        $ext = $image->getOriginalExtension();
        // 文件重命名
        $new_imag_name = substr(md5($image->getOriginalName()),0,6).rand(00000,99999).'.'.$ext;
        $type = $image->getOriginalMime();
        try{
            $oss->putObject(config('aliyun.Bucket'),$new_imag_name,$content);
            return success('上传成功',['path'=>config('aliyun.domain').$new_imag_name]);
        } catch (Exception $e) {
            return error('上传失败');
        }
        
    }

}

你可能感兴趣的:(thinkphp6,1024程序员节)