123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123 |
- <?php
- require dirname(__FILE__) . '/huaweicloud/obs-autoloader.php';
- require dirname(__FILE__) . '/huaweicloud/vendor/autoload.php';
- use Obs\ObsClient;
- class HuaweiCloud{
- private $cosClient;
- private $bucket;
- private $accessKey;
- private $endpoint;
- private $secretKey ; //"云 API 密钥 SecretKey";
- private $timeOut;
- public function __construct($bucket='')
- {
- require_once(Yii::app()->basePath."/config/huaweicloud_".YII_ENV."_conf.php");
- global $BUCKET;
- global $ENDPOINT;
- global $ACCESSKEY;
- global $SECRETKEY;
- global $TIMEOUT;
- $this->bucket=$BUCKET;
- $this->accessKey=$ACCESSKEY;
- $this->secretKey=$SECRETKEY;
- $this->endpoint=$ENDPOINT;
- $this->timeOut=$TIMEOUT;
- $this->cosClient = new ObsClient ( [
- 'key' => $this->accessKey,
- 'secret' => $this->secretKey,
- 'endpoint' => $this->endpoint
- ] );
- }
- public function putFile($key,$localFile,$storageClass=''){
- $data=array(
- 'status'=>0
- );
- if(!file_exists($localFile)){
- $data=array(
- 'msg'=>"文件不存在"
- );
- return $data;
- }
- try {
- $option=array();
- if($storageClass){
- $option['StorageClass']=$storageClass;
- }
- $result = $this->cosClient->putObject([
- 'Bucket' => $this->bucket,
- 'Key' => $key,
- 'SourceFile' => $localFile
- ]);
- if($result && isset($result['HttpStatusCode']) && $result['HttpStatusCode']=='200'){
- $data['status']=1;
- $data['url']=$result['ObjectURL'];
- }
- } catch (\Exception $e) {
- // 请求失败
- //echo($e);
- $data['msg']=$e;
- }
- return $data;
- }
- //删除 key 就是url去掉前面的域名部分
- public function deleteFile($key){
- $object=array(
- 'Bucket'=>$this->bucket,
- 'Key'=>$key
- );
- $result = array(
- 'status' => 0,
- 'msg' => '',
- 'data' => '',
- );
- try {
- $result = $this->cosClient->deleteObject($object);
- // 请求成功
- $result['status'] = 1;
- $result['data'] = '';
- $result['msg'] = "delete $this->bucket/$key success";
- } catch (\Exception $e) {
- // 请求失败
- $result['msg'] = '删除失败';
- }
- return $result;
- }
- //修改文档属性
- public function updateFile($bucket,$key,$source){
- $result['status'] = 0;
- try {
- $rs = $this->cosClient->Copy($bucket,$key,$source,array('StorageClass'=>'STANDARD_IA'));
- if($rs=='1001'){
- $result['status'] = -1;
- $result['msg'] = '文件已是低频存储';
- }else{
- // 请求成功
- $result['status'] = 1;
- $result['data'] = '';
- $result['msg'] = "copy $this->bucket/$key success";
- }
- } catch (\Exception $e) {
- // 请求失败
- $result['msg'] = '更新失败';
- }
- return $result;
- }
- }
|