123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2018/8/24
- * Time: 16:07
- */
- require_once(Yii::app()->basePath.'/..'."/lib/core/ucloud/ucloud/proxy.php");
- require_once(Yii::app()->basePath.'/..'."/protected/config/ucloud_".YII_ENV."_conf.php");
- class Ucloud
- {
- //存储空间名
- public $bucket;
- public $image_domain;
- public function __construct()
- {
- global $BUCKET;
- global $IMAGE_DOMAIN;
- $this->bucket = $BUCKET;
- $this->image_domain = $IMAGE_DOMAIN;
- }
- /**
- * 上传文件
- * @param $key 上传至存储空间后的文件名称(请不要和API公私钥混淆)
- * @param $file 待上传文件的本地路径
- * @return array
- */
- public function putFile($key,$file){
- $result = array(
- 'status' => 0,
- 'msg' => '',
- 'data' => '',
- );
- //该接口适用于0-10MB小文件,更大的文件建议使用分片上传接口
- list($data, $err) = UCloud_PutFile($this->bucket, $key, $file);
- if ($err) {
- $result['msg'] = $err->ErrMsg;
- }else{
- $result['status'] = 1;
- $result['data'] = $data['ETag'];
- }
- return $result;
- }
- /**
- * 获取文件
- * @param $key
- * @param bool $isPrivate
- * @return array|string
- */
- public function getFileUrl($key,$isPrivate = false){
- if($isPrivate){
- //访问私有Bucket的例子
- $url = UCloud_MakePrivateUrl($this->bucket, $key);
- }else{
- //访问公有Bucket的例子
- $url = UCloud_MakePublicUrl($this->bucket, $key);
- }
- return $url;
- }
- /**
- * 删除文件
- * @param $key
- * @return array
- */
- public function deleteFile($key){
- $result = array(
- 'status' => 0,
- 'msg' => '',
- 'data' => '',
- );
- list($data, $err) = UCloud_Delete($this->bucket, $key);
- if ($err) {
- $result['msg'] = $err->ErrMsg;
- }else{
- $result['status'] = 1;
- $result['data'] = '';
- $result['msg'] = "delete $this->bucket/$key success";
- }
- return $result;
- }
- }
- //$ucloud = new Ucloud();
- //$key = "test.png";
- //$file = "e:/1.png";
- ////$rs = $ucloud->putFile();
- //
- //$url = $ucloud->getFileUrl($key);
- //var_dump($url);die;
- //$key = "/test/test.png";
- //$bool = $ucloud->deleteFile($key);
- //
- //var_dump($bool);die;
|