thinkphp5集成阿里云oss块存储过程github下载

1 配置说明

$config['KeyId']='11';  //access keyid
$config['KeySecret']='222';//access secret
$config['Bucket']='tyjyvideo';//相当于子域名
$config['Endpoint']='http://oss-cn-shenzhen.aliyuncs.com';//根据你的位置 选择临近的域名即可

 在这里创建的

2 下载aliyun oss的PHP接口 git下载

去这里下载https://github.com/aliyun/aliyun-oss-php-sdk?spm=a2c4g.11186623.2.11.33f9c839ZloB2d   zip包  解压

3 安装 

解压后的文件改名为 aliyun_oss

然后放到thinkphp5的这个目录

4 使用

我是封装在common.php文件里面 封装的 方便任何一个位置调用

在common.php的顶部加上如下代码

顶部  加载这个扩展

//加载阿里云OSS
require_once '../extend/aliyun_oss/autoload.php';
use OSSOssClient;
use OSSCoreOssException;

然后在这个文件的底部 加上如下函数

if (!function_exists('aliyun_oss_upload')) {
    function aliyun_oss_upload($file)
    {
        //阿里云需要的配置
        $config['KeyId']='122';
        $config['KeySecret']='22';
        $config['Bucket']='55';
        $config['Endpoint']='http://oss-cn-shenzhen.aliyuncs.com';
        
        //文件名字获取和判断
        $name = $file['name'];
        $format = strrchr($name, '.');//截取文件后缀名如 (.mp4)
        /*判断文件格式*/
        $allow_type = ['.mp4', '.flv', '.wmv', '.avi'];
        if (! in_array($format, $allow_type)) {
            return 1;exit;
        }
 
        // 尝试执行
        try {
            //实例化对象 将配置传入
            $ossClient = new OssClient($config['KeyId'], $config['KeySecret'], $config['Endpoint']);
            //这里是有sha1加密 生成文件名 之后连接上后缀
            $fileName = 'uplaod/video/' . date("Ymd") . '/' . sha1(date('YmdHis', time()) . uniqid()) . $format;
            //执行阿里云上传
            $result = $ossClient->uploadFile($config['Bucket'], $fileName, $file['tmp_name']);
             
            /*组合返回数据*/
            $arr = [
                'oss_url' => $result['info']['url'],  //上传资源地址
                'relative_path' => $fileName     //数据库保存名称(相对路径)
            ];
        } catch (OssException $e) {
            return $e->getMessage();
        }
        
        return $arr['oss_url'];
    }
}

 5 上传测试

代码比较简单 PHP部分

if($this->request->isPost()) {
            $result=aliyun_oss_upload($_FILES['file']);
//
$aa=request()->file('file');$b=$aa->getInfo());

// $result=aliyun_oss_upload($b);
var_dump($result); } else { return $this->view->fetch(); }

html部分

 <form action="" method="post"  enctype="multipart/form-data" >
                               <input type="file" name="file" id="file">
                               <input type="submit" value="提交">
                            </form>

6 如果是composer按照 就开业省去引用了   

原文地址:https://www.cnblogs.com/baker95935/p/12663179.html