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;