Ucloud.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2018/8/24
  6. * Time: 16:07
  7. */
  8. require_once(Yii::app()->basePath.'/..'."/lib/core/ucloud/ucloud/proxy.php");
  9. require_once(Yii::app()->basePath.'/..'."/protected/config/ucloud_".YII_ENV."_conf.php");
  10. class Ucloud
  11. {
  12. //存储空间名
  13. public $bucket;
  14. public $image_domain;
  15. public function __construct()
  16. {
  17. global $BUCKET;
  18. global $IMAGE_DOMAIN;
  19. $this->bucket = $BUCKET;
  20. $this->image_domain = $IMAGE_DOMAIN;
  21. }
  22. /**
  23. * 上传文件
  24. * @param $key 上传至存储空间后的文件名称(请不要和API公私钥混淆)
  25. * @param $file 待上传文件的本地路径
  26. * @return array
  27. */
  28. public function putFile($key,$file){
  29. $result = array(
  30. 'status' => 0,
  31. 'msg' => '',
  32. 'data' => '',
  33. );
  34. //该接口适用于0-10MB小文件,更大的文件建议使用分片上传接口
  35. list($data, $err) = UCloud_PutFile($this->bucket, $key, $file);
  36. if ($err) {
  37. $result['msg'] = $err->ErrMsg;
  38. }else{
  39. $result['status'] = 1;
  40. $result['data'] = $data['ETag'];
  41. }
  42. return $result;
  43. }
  44. /**
  45. * 获取文件
  46. * @param $key
  47. * @param bool $isPrivate
  48. * @return array|string
  49. */
  50. public function getFileUrl($key,$isPrivate = false){
  51. if($isPrivate){
  52. //访问私有Bucket的例子
  53. $url = UCloud_MakePrivateUrl($this->bucket, $key);
  54. }else{
  55. //访问公有Bucket的例子
  56. $url = UCloud_MakePublicUrl($this->bucket, $key);
  57. }
  58. return $url;
  59. }
  60. /**
  61. * 删除文件
  62. * @param $key
  63. * @return array
  64. */
  65. public function deleteFile($key){
  66. $result = array(
  67. 'status' => 0,
  68. 'msg' => '',
  69. 'data' => '',
  70. );
  71. list($data, $err) = UCloud_Delete($this->bucket, $key);
  72. if ($err) {
  73. $result['msg'] = $err->ErrMsg;
  74. }else{
  75. $result['status'] = 1;
  76. $result['data'] = '';
  77. $result['msg'] = "delete $this->bucket/$key success";
  78. }
  79. return $result;
  80. }
  81. }
  82. //$ucloud = new Ucloud();
  83. //$key = "test.png";
  84. //$file = "e:/1.png";
  85. ////$rs = $ucloud->putFile();
  86. //
  87. //$url = $ucloud->getFileUrl($key);
  88. //var_dump($url);die;
  89. //$key = "/test/test.png";
  90. //$bool = $ucloud->deleteFile($key);
  91. //
  92. //var_dump($bool);die;