123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- <?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小文件,更大的文件建议使用分片上传接口
- // return UCloud_PutFile($this->bucket, $key, $file);
- list($data, $err) = UCloud_PutFile($this->bucket, $key, $file);
- $url = "http://".UCloud_MakePublicUrl($this->bucket, $key);
- if ($err) {
- $result['msg'] = $err->ErrMsg;
- }else{
- $result['status'] = 1;
- $result['data'] = $data['ETag'];
- $result['url'] = $url;
- }
- 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;
- }
-
- public function putBigFile($key,$file){
- $result = array(
- 'status' => 0,
- 'msg' => '',
- 'data' => '',
- );
- //该接口适用于0-10MB小文件,更大的文件建议使用分片上传接口
- // return UCloud_PutFile($this->bucket, $key, $file);
- list($data, $err) = UCloud_MInit($this->bucket, $key);
- if ($err)
- {
- echo "error: " . $err->ErrMsg . "\n";
- echo "code: " . $err->Code . "\n";
- exit;
- }
- $uploadId = $data['UploadId'];
- $blkSize = $data['BlkSize'];
- // echo "UploadId: " . $uploadId . "\n";
- // echo "BlkSize: " . $blkSize . "\n";
- //数据上传
- list($etagList, $err) = UCloud_MUpload($this->bucket, $key, $file, $uploadId, $blkSize);
- if ($err) {
- echo "error: " . $err->ErrMsg . "\n";
- echo "code: " . $err->Code . "\n";
- exit;
- }
- //完成上传
- list($data, $err) = UCloud_MFinish($this->bucket, $key, $uploadId, $etagList);
- if ($err) {
- echo "error: " . $err->ErrMsg . "\n";
- echo "code: " . $err->Code . "\n";
- exit;
- }
- // echo "Etag: " . $data['ETag'] . "\n";
- // echo "FileSize: " . $data['FileSize'] . "\n";
- $url = "http://".UCloud_MakePublicUrl($this->bucket, $key);
- if ($err) {
- $result['msg'] = $err->ErrMsg;
- }else{
- $result['status'] = 1;
- $result['data'] = $data['ETag'];
- $result['url'] = $url;
- }
- 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;
|