Ucloud.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. // return UCloud_PutFile($this->bucket, $key, $file);
  36. list($data, $err) = UCloud_PutFile($this->bucket, $key, $file);
  37. $url = "http://".UCloud_MakePublicUrl($this->bucket, $key);
  38. if ($err) {
  39. $result['msg'] = $err->ErrMsg;
  40. }else{
  41. $result['status'] = 1;
  42. $result['data'] = $data['ETag'];
  43. $result['url'] = $url;
  44. }
  45. return $result;
  46. }
  47. /**
  48. * 获取文件
  49. * @param $key
  50. * @param bool $isPrivate
  51. * @return array|string
  52. */
  53. public function getFileUrl($key,$isPrivate = false){
  54. if($isPrivate){
  55. //访问私有Bucket的例子
  56. $url = UCloud_MakePrivateUrl($this->bucket, $key);
  57. }else{
  58. //访问公有Bucket的例子
  59. $url = UCloud_MakePublicUrl($this->bucket, $key);
  60. }
  61. return $url;
  62. }
  63. /**
  64. * 删除文件
  65. * @param $key
  66. * @return array
  67. */
  68. public function deleteFile($key){
  69. $result = array(
  70. 'status' => 0,
  71. 'msg' => '',
  72. 'data' => '',
  73. );
  74. list($data, $err) = UCloud_Delete($this->bucket, $key);
  75. if ($err) {
  76. $result['msg'] = $err->ErrMsg;
  77. }else{
  78. $result['status'] = 1;
  79. $result['data'] = '';
  80. $result['msg'] = "delete $this->bucket/$key success";
  81. }
  82. return $result;
  83. }
  84. public function putBigFile($key,$file){
  85. $result = array(
  86. 'status' => 0,
  87. 'msg' => '',
  88. 'data' => '',
  89. );
  90. //该接口适用于0-10MB小文件,更大的文件建议使用分片上传接口
  91. // return UCloud_PutFile($this->bucket, $key, $file);
  92. list($data, $err) = UCloud_MInit($this->bucket, $key);
  93. if ($err)
  94. {
  95. echo "error: " . $err->ErrMsg . "\n";
  96. echo "code: " . $err->Code . "\n";
  97. exit;
  98. }
  99. $uploadId = $data['UploadId'];
  100. $blkSize = $data['BlkSize'];
  101. // echo "UploadId: " . $uploadId . "\n";
  102. // echo "BlkSize: " . $blkSize . "\n";
  103. //数据上传
  104. list($etagList, $err) = UCloud_MUpload($this->bucket, $key, $file, $uploadId, $blkSize);
  105. if ($err) {
  106. echo "error: " . $err->ErrMsg . "\n";
  107. echo "code: " . $err->Code . "\n";
  108. exit;
  109. }
  110. //完成上传
  111. list($data, $err) = UCloud_MFinish($this->bucket, $key, $uploadId, $etagList);
  112. if ($err) {
  113. echo "error: " . $err->ErrMsg . "\n";
  114. echo "code: " . $err->Code . "\n";
  115. exit;
  116. }
  117. // echo "Etag: " . $data['ETag'] . "\n";
  118. // echo "FileSize: " . $data['FileSize'] . "\n";
  119. $url = "http://".UCloud_MakePublicUrl($this->bucket, $key);
  120. if ($err) {
  121. $result['msg'] = $err->ErrMsg;
  122. }else{
  123. $result['status'] = 1;
  124. $result['data'] = $data['ETag'];
  125. $result['url'] = $url;
  126. }
  127. return $result;
  128. }
  129. }
  130. //$ucloud = new Ucloud();
  131. //$key = "test.png";
  132. //$file = "e:/1.png";
  133. ////$rs = $ucloud->putFile();
  134. //
  135. //$url = $ucloud->getFileUrl($key);
  136. //var_dump($url);die;
  137. //$key = "/test/test.png";
  138. //$bool = $ucloud->deleteFile($key);
  139. //
  140. //var_dump($bool);die;