SimpleOss.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. <?php
  2. namespace app\common\components;
  3. define('__BOS_CLIENT_ROOT', dirname(__DIR__));
  4. include BASE_PATH.'components'.DIRECTORY_SEPARATOR.'bce'.DIRECTORY_SEPARATOR.'BaiduBce.phar';
  5. use OSS\OssClient;
  6. use OSS\Core\OssException;
  7. use Aws\S3\S3Client;
  8. use Aws\S3\MultipartUploader;
  9. use Aws\S3\Exception\S3Exception;
  10. use Aws\Exception\AwsException;
  11. use Aws\Exception\MultipartUploadException;
  12. use Aws\S3\ObjectUploader;
  13. use \BaiduBce\BceClientConfigOptions;
  14. use \BaiduBce\Util\MimeTypes;
  15. use \BaiduBce\Http\HttpHeaders;
  16. use \BaiduBce\Services\Bos\BosClient;
  17. use Yii;
  18. class SimpleOss
  19. {
  20. public $ossClient;
  21. public $ossUrl;
  22. public $ossInerUrl;
  23. public $accessKeyId;
  24. public $accessKeySecret;
  25. public $endpoint;
  26. public $bucket;
  27. public $region;
  28. public $type;
  29. public $cname;
  30. public $args;
  31. public function init($args,$type='tencent')
  32. {
  33. $this->args = $args;
  34. $this->ossUrl = SITE_PROTOCOL.$args['OSS_BUCKET'].'.'.$args['OSS_ENDPOINT'].'/';
  35. $this->ossInerUrl = SITE_PROTOCOL.$args['OSS_BUCKET'].'.'.$args['OSS_INTERNAL_ENDPOINT'].'/';
  36. $this->accessKeyId = $args['OSS_ACCESS_KEY'];
  37. $this->accessKeySecret = $args['OSS_ACCESS_SECRET'];
  38. $this->endpoint = $args['OPEN_INTERNAL']?$args['OSS_INTERNAL_ENDPOINT']:$args['OSS_ENDPOINT'];;
  39. $this->bucket = $args['OSS_BUCKET'];
  40. $this->region = $args['OSS_REGION'];
  41. $this->type = $args['OSS_TYPE']?$args['OSS_TYPE']:$type;
  42. $this->cname = FALSE;
  43. $result = [];
  44. if($this->type=='ali')
  45. {
  46. try {
  47. $this->ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint,$this->cname);
  48. $result = array('error'=>0);
  49. } catch (OssException $e) {
  50. $result = array('error'=>1,'msg'=>$e->getMessage());
  51. }
  52. }
  53. else if($this->type=='tencent')
  54. {
  55. $protocol = str_replace("://","",SITE_PROTOCOL);
  56. $this->ossClient = new \Qcloud\Cos\Client(
  57. array(
  58. 'region' => $this->region,
  59. 'schema' => $protocol, //协议头部,默认为http
  60. 'credentials'=> array(
  61. 'secretId' => $this->accessKeyId ,
  62. 'secretKey' => $this->accessKeySecret)));
  63. $result = array('error'=>0);
  64. }
  65. else if($this->type=='baidu')
  66. {
  67. $this->ossClient = new BosClient(array(
  68. 'credentials' => array(
  69. 'accessKeyId' => $this->accessKeyId,
  70. 'secretAccessKey' => $this->accessKeySecret,
  71. ),
  72. 'endpoint' => $this->endpoint
  73. ));
  74. $result = array('error'=>0);
  75. }
  76. else if($this->type=='s3')
  77. {
  78. $this->ossClient = new S3Client([
  79. 'version' => 'latest',
  80. 'region' => $this->region,
  81. 'endpoint' => 'https://' . str_replace(array('https://','http://'),'',$this->endpoint),
  82. 'credentials' => [
  83. 'key' => $this->accessKeyId,
  84. 'secret' => $this->accessKeySecret,
  85. ]
  86. ]);
  87. }
  88. return $result;
  89. }
  90. /*
  91. * $file:完整文件路径
  92. * $object:文件存放到OSS的相对路径
  93. */
  94. public function Upload($file,$object)
  95. {
  96. $result = [];
  97. $data = [];
  98. if($this->type=='ali')
  99. {
  100. try{
  101. $ossresult = $this->ossClient->uploadFile($this->bucket, $object, $file);
  102. $ossresult['info']['url'] = str_replace($this->args['OSS_INTERNAL_ENDPOINT'],$this->args['OSS_ENDPOINT'],$ossresult['info']['url']);
  103. $data['url'] = str_replace(array('https://','http://'),SITE_PROTOCOL,$ossresult['info']['url']);
  104. $data['path'] = $object;
  105. $data['size'] = $ossresult['info']['upload_content_length'];
  106. $result = array('error'=>0,'data'=>$data);
  107. } catch(OssException $e) {
  108. $result = array('error'=>1,'msg'=>$e->getMessage());
  109. }
  110. }
  111. else if($this->type=='tencent')
  112. {
  113. try {
  114. $fileContent = fopen($file, "rb");
  115. if ($fileContent) {
  116. $ossresult = $this->ossClient->putObject(array(
  117. 'Bucket' => $this->bucket,
  118. 'Key' => $object,
  119. 'Body' => $fileContent));
  120. $data['url'] = SITE_PROTOCOL.$ossresult['Location'];
  121. $data['path'] = $object;
  122. $data['size'] = filesize($file);
  123. $result = array('error'=>0,'data'=>$data);
  124. }
  125. } catch (\Exception $e) {
  126. $result = array('error'=>1,'msg'=>$e->getMessage());
  127. }
  128. }
  129. else if($this->type=='baidu')
  130. {
  131. try {
  132. $this->ossClient->putObjectFromFile($this->bucket,$object,$file);
  133. $data['url'] = SITE_PROTOCOL.$this->bucket.'.'.$this->endpoint.'/'.$object;
  134. $data['path'] = $object;
  135. $data['size'] = filesize($file);
  136. $result = array('error'=>0,'data'=>$data);
  137. } catch (\Exception $e) {
  138. $result = array('error'=>1,'msg'=>$e->getMessage());
  139. }
  140. }
  141. else if($this->type=='s3')
  142. {
  143. try {
  144. $key = $object;
  145. $source = fopen($file, 'rb');
  146. $acl = 'public-read';
  147. $uploader = new ObjectUploader(
  148. $this->ossClient,
  149. $this->bucket,
  150. $key,
  151. $source,
  152. $acl
  153. );
  154. $ossresult = $uploader->upload();
  155. if ($ossresult["@metadata"]["statusCode"] == '200') {
  156. $data['url'] = $ossresult["ObjectURL"];
  157. $data['path'] = $object;
  158. $data['size'] = filesize($file);
  159. $result = array('error'=>0,'data'=>$data);
  160. }
  161. } catch (\Exception $e) {
  162. $result = array('error'=>1,'msg'=>$e->getMessage());
  163. }
  164. }
  165. return $result;
  166. }
  167. //删除单个文件
  168. public function delete($object){
  169. $object = str_replace($this->ossUrl,'',$object);
  170. $object= str_replace($this->ossInerUrl,'',$object);
  171. if($this->type=='ali')
  172. {
  173. try{
  174. $ossresult = $this->ossClient->deleteObject($this->bucket, $object);
  175. $result = array('error'=>0,'data'=>'删除成功');
  176. } catch(OssException $e) {
  177. $result = array('error'=>1,'msg'=>$e->getMessage());
  178. }
  179. }
  180. else if($this->type=='tencent')
  181. {
  182. try {
  183. $ossresult = $this->ossClient->deleteObject(array(
  184. 'Bucket' => $this->bucket, //存储桶名称
  185. 'Key' => $object
  186. ));
  187. $result = array('error'=>0,'data'=>'删除成功');
  188. } catch (\Exception $e) {
  189. $result = array('error'=>1,'msg'=>$e->getMessage());
  190. }
  191. }
  192. else if($this->type=='baidu')
  193. {
  194. try {
  195. $ossresult = $this->ossClient->deleteObject($this->bucket, $object);
  196. $result = array('error'=>0,'data'=>'删除成功');
  197. } catch (\Exception $e) {
  198. $result = array('error'=>1,'msg'=>$e->getMessage());
  199. }
  200. }
  201. else if($this->type=='s3')
  202. {
  203. try {
  204. $ossresult = $this->ossClient->deleteObject([
  205. 'Bucket' => $this->bucket,
  206. 'Key' => $object
  207. ]);
  208. $result = array('error'=>0,'data'=>'删除成功');
  209. } catch (S3Exception $e) {
  210. $result = array('error'=>1,'msg'=>$e->getMessage());
  211. }
  212. }
  213. return $result;
  214. }
  215. //转换图片
  216. public function previewImg($remoteFileUrl,$i)
  217. {
  218. if($this->type=='tencent')
  219. {
  220. return $remoteFileUrl.'?ci-process=doc-preview&page='.$i;
  221. }
  222. }
  223. }
  224. ?>