多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
# 阿里云OSS 本服务用于管理阿里云OSS 本服务是对阿里云SDK的封装,所以方法名与阿里的SDK一样,下面是列举出的几个使用方法,其他OSS方法都是可以使用的,请参考阿里云 [OSS参考文档](https://help.aliyun.com/document_detail/32103.html?spm=5176.doc32099.6.748.85Qz6b) 同时支持其他框架使用如Laravel 、ThinkPHP 查看 [composer安装组件](https://packagist.org/packages/houdunwang/oss) [TOC] ## PHP上传 #### 配置 | 配置 | 查看 | | --- | --- | | endPoint | 登录某个块查看 bucket概览 | | accessId | 登录阿里云后台查看右上角的 "访问控制(使用子帐号)" 或 "accesskeys(使用主帐号)" | | accessKey | 同上 accessId | ``` Config::set('oss',[ 'accessKeyId'=>'', 'accessKeySecret'=>'', //外网Endpoint OSS开通Region和Endpoint对照表: https://help.aliyun.com/document_detail/31837.html 'endpoint'=>'oss-cn-hangzhou.aliyuncs.com', //OSS块标识 'bucket'=> "houdunren" ] ); ``` #### 字符串上传 ``` $object = "hd.txt"; $content = "Hi, OSS."; $res = Oss::putObject($object, $content); if(isset($res['oss-request-url'])){ echo "文件:".$res['oss-request-url']; } ``` #### 上传本地文件 ``` $object = '1-test-upload.jpg'; $filePath = 'tests/1.jpg'; $res = Oss::uploadFile($object, $filePath); if(isset($res['oss-request-url'])){ echo "文件:".$res['oss-request-url']; } ``` #### 其他OSS管理 组件与官网SDK无缝集成,比如我们拿删除资源举例。 下面是官方删除对象代码 [官网查看](https://help.aliyun.com/document_detail/32105.html?spm=5176.doc32103.6.761.FSG7RW) ``` $ossClient->deleteObject($bucket, $object); ``` 本组件实现的删除方法 ``` Oss::deleteObject('hdphp.php'); ``` 也就是说我们在组件中调用SDK提供的方法时,不需要传递第一个块名称。 ## 前台直接上传 #### 后台签名 ``` <?php class Oss { //生成供前台使用的签名 public static function sign() { //阿里云 AccessKeyId $id = 'xxxxxx'; //阿里云 AccessKeySecret $key = 'xxxxx'; //OSS外网域名: 在阿里云后台OSS bucket中查看 $host = 'http://hdxj.oss-cn-hangzhou.aliyuncs.com'; //oss中本次上传存放文件的目录 $dir = $_GET['dir']; function gmt_iso8601($time) { $dtStr = date("c", $time); $mydatetime = new \DateTime($dtStr); $expiration = $mydatetime->format(\DateTime::ISO8601); $pos = strpos($expiration, '+'); $expiration = substr($expiration, 0, $pos); return $expiration."Z"; } $now = time(); $expire = 30; //设置该policy超时时间是10s. 即这个policy过了这个有效时间,将不能访问 $end = $now + $expire; $expiration = gmt_iso8601($end); //最大文件大小.用户可以自己设置 $condition = [0 => 'content-length-range', 1 => 0, 2 => 1048576000]; $conditions[] = $condition; //表示用户上传的数据,必须是以$dir开始, 不然上传会失败,这一步不是必须项,只是为了安全起见,防止用户通过policy上传到别人的目录 $start = [0 => 'starts-with', 1 => '$key', 2 => $dir]; $conditions[] = $start; $arr = ['expiration' => $expiration, 'conditions' => $conditions]; //return; $policy = json_encode($arr); $base64_policy = base64_encode($policy); $string_to_sign = $base64_policy; $signature = base64_encode(hash_hmac('sha1', $string_to_sign, $key, true)); $response = []; $response['accessid'] = $id; $response['host'] = $host; $response['policy'] = $base64_policy; $response['signature'] = $signature; $response['expire'] = $end; //这个参数是设置用户上传指定的前缀 $response['dir'] = $dir; return json_encode($response); } } echo Oss::sign(); ``` #### 前台 ``` <!doctype html> <html lang="en"> <head> <meta charset="UTF-8"> <title>OSS上传</title> <link href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.css" rel="stylesheet"> <script> //模块配置项 var hdjs = { //框架目录 'base': '../../', }; </script> <script src="../../app/util.js"></script> <script src="../../require.js"></script> <script src="../../config.js"></script> </head> <body> <div class="input-group"> <input type="text" class="form-control" v-model="v.path"> <span class="input-group-btn"> <button class="btn btn-default" type="button" id="pickVideo">上传文件</button> </span> </div> </div> <script> function upload() { require(['oss'], function (oss) { var id = '#pickVideo'; var uploader = oss.upload({ //获取签名 serverUrl: 'sign.php?', //上传目录 dir: 'houdunwang/', //按钮元素 pick: id, accept: { title: 'Images', // extensions: 'mp4', // mimeTypes: 'video/mp4' } }); //上传开始 uploader.on('startUpload', function () { console.log('开始上传'); }); //上传成功 uploader.on('uploadSuccess', function (file, response) { console.log('上传完成,文件名:' + oss.oss.host + '/' + oss.oss.object_name); }); //上传中 uploader.on('uploadProgress', function (file, percentage) { console.log('上传中,进度:' + parseInt(percentage * 100)); }) //上传结束 uploader.on('uploadComplete', function () { console.log('上传结束'); }) }); } upload(); </script> </body> </html> ```