HuaweiCloud.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. require dirname(__FILE__) . '/huaweicloud/obs-autoloader.php';
  3. require dirname(__FILE__) . '/huaweicloud/vendor/autoload.php';
  4. use Obs\ObsClient;
  5. class HuaweiCloud{
  6. private $cosClient;
  7. private $bucket;
  8. private $accessKey;
  9. private $endpoint;
  10. private $secretKey ; //"云 API 密钥 SecretKey";
  11. private $timeOut;
  12. public function __construct($bucket='')
  13. {
  14. require_once(Yii::app()->basePath."/config/huaweicloud_".YII_ENV."_conf.php");
  15. global $BUCKET;
  16. global $ENDPOINT;
  17. global $ACCESSKEY;
  18. global $SECRETKEY;
  19. global $TIMEOUT;
  20. $this->bucket=$BUCKET;
  21. $this->accessKey=$ACCESSKEY;
  22. $this->secretKey=$SECRETKEY;
  23. $this->endpoint=$ENDPOINT;
  24. $this->timeOut=$TIMEOUT;
  25. $this->cosClient = new ObsClient ( [
  26. 'key' => $this->accessKey,
  27. 'secret' => $this->secretKey,
  28. 'endpoint' => $this->endpoint
  29. ] );
  30. }
  31. public function putFile($key,$localFile,$storageClass=''){
  32. $data=array(
  33. 'status'=>0
  34. );
  35. if(!file_exists($localFile)){
  36. $data=array(
  37. 'msg'=>"文件不存在"
  38. );
  39. return $data;
  40. }
  41. try {
  42. $option=array();
  43. if($storageClass){
  44. $option['StorageClass']=$storageClass;
  45. }
  46. $result = $this->cosClient->putObject([
  47. 'Bucket' => $this->bucket,
  48. 'Key' => $key,
  49. 'SourceFile' => $localFile
  50. ]);
  51. if($result && isset($result['HttpStatusCode']) && $result['HttpStatusCode']=='200'){
  52. $data['status']=1;
  53. $data['url']=$result['ObjectURL'];
  54. }
  55. } catch (\Exception $e) {
  56. // 请求失败
  57. //echo($e);
  58. $data['msg']=$e;
  59. }
  60. return $data;
  61. }
  62. //删除 key 就是url去掉前面的域名部分
  63. public function deleteFile($key){
  64. $object=array(
  65. 'Bucket'=>$this->bucket,
  66. 'Key'=>$key
  67. );
  68. $result = array(
  69. 'status' => 0,
  70. 'msg' => '',
  71. 'data' => '',
  72. );
  73. try {
  74. $result = $this->cosClient->deleteObject($object);
  75. // 请求成功
  76. $result['status'] = 1;
  77. $result['data'] = '';
  78. $result['msg'] = "delete $this->bucket/$key success";
  79. } catch (\Exception $e) {
  80. // 请求失败
  81. $result['msg'] = '删除失败';
  82. }
  83. return $result;
  84. }
  85. //修改文档属性
  86. public function updateFile($bucket,$key,$source){
  87. $result['status'] = 0;
  88. try {
  89. $rs = $this->cosClient->Copy($bucket,$key,$source,array('StorageClass'=>'STANDARD_IA'));
  90. if($rs=='1001'){
  91. $result['status'] = -1;
  92. $result['msg'] = '文件已是低频存储';
  93. }else{
  94. // 请求成功
  95. $result['status'] = 1;
  96. $result['data'] = '';
  97. $result['msg'] = "copy $this->bucket/$key success";
  98. }
  99. } catch (\Exception $e) {
  100. // 请求失败
  101. $result['msg'] = '更新失败';
  102. }
  103. return $result;
  104. }
  105. }