Oss.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 Oss
  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 function init($internal=0)
  31. {
  32. $this->ossUrl = getOssUrl();
  33. $this->ossInerUrl = getOssInterUrl();
  34. $this->accessKeyId = Yii::$app->params['oss']['OSS_ACCESS_KEY'];
  35. $this->accessKeySecret = Yii::$app->params['oss']['OSS_ACCESS_SECRET'];
  36. $this->endpoint = $internal?Yii::$app->params['oss']['OSS_INTERNAL_ENDPOINT']:Yii::$app->params['oss']['OSS_ENDPOINT'];
  37. $this->bucket = Yii::$app->params['oss']['OSS_BUCKET'];
  38. $this->region = Yii::$app->params['oss']['OSS_REGION'];
  39. $this->type = Yii::$app->params['oss']['OSS_TYPE'];
  40. $this->cname = FALSE;
  41. $result = [];
  42. if($this->type=='ali')
  43. {
  44. try {
  45. $this->ossClient = new OssClient($this->accessKeyId, $this->accessKeySecret, $this->endpoint,$this->cname);
  46. $result = array('error'=>0);
  47. } catch (OssException $e) {
  48. $result = array('error'=>1,'msg'=>$e->getMessage());
  49. }
  50. }
  51. else if($this->type=='tencent')
  52. {
  53. $protocol = str_replace("://","",SITE_PROTOCOL);
  54. $this->ossClient = new \Qcloud\Cos\Client(
  55. array(
  56. 'region' => $this->region,
  57. 'schema' => $protocol, //协议头部,默认为http
  58. 'credentials'=> array(
  59. 'secretId' => $this->accessKeyId ,
  60. 'secretKey' => $this->accessKeySecret)));
  61. $result = array('error'=>0);
  62. }
  63. else if($this->type=='baidu')
  64. {
  65. $this->ossClient = new BosClient(array(
  66. 'credentials' => array(
  67. 'accessKeyId' => $this->accessKeyId,
  68. 'secretAccessKey' => $this->accessKeySecret,
  69. ),
  70. 'endpoint' => $this->endpoint
  71. ));
  72. $result = array('error'=>0);
  73. }
  74. else if($this->type=='s3')
  75. {
  76. $this->ossClient = new S3Client([
  77. 'version' => 'latest',
  78. 'region' => $this->region,
  79. 'endpoint' => 'https://' . str_replace(array('https://','http://'),'',$this->endpoint),
  80. 'credentials' => [
  81. 'key' => $this->accessKeyId,
  82. 'secret' => $this->accessKeySecret,
  83. ]
  84. ]);
  85. }
  86. return $result;
  87. }
  88. /*
  89. * $file:完整文件路径
  90. * $object:文件存放到OSS的相对路径
  91. */
  92. public function Upload($file,$object)
  93. {
  94. $result = [];
  95. $data = [];
  96. if($this->type=='ali')
  97. {
  98. try{
  99. $ossresult = $this->ossClient->uploadFile($this->bucket, $object, $file);
  100. $ossresult['info']['url'] = str_replace(Yii::$app->params['oss']['OSS_INTERNAL_ENDPOINT'],Yii::$app->params['oss']['OSS_ENDPOINT'],$ossresult['info']['url']);
  101. $data['url'] = str_replace(array('https://','http://'),SITE_PROTOCOL,$ossresult['info']['url']);
  102. $data['path'] = $object;
  103. $data['size'] = $ossresult['info']['upload_content_length'];
  104. $result = array('error'=>0,'data'=>$data);
  105. } catch(OssException $e) {
  106. $result = array('error'=>1,'msg'=>$e->getMessage());
  107. }
  108. }
  109. else if($this->type=='tencent')
  110. {
  111. try {
  112. $fileContent = fopen($file, "rb");
  113. if ($fileContent) {
  114. $ossresult = $this->ossClient->putObject(array(
  115. 'Bucket' => $this->bucket,
  116. 'Key' => $object,
  117. 'Body' => $fileContent));
  118. $data['url'] = SITE_PROTOCOL.$ossresult['Location'];
  119. $data['path'] = $object;
  120. $data['size'] = filesize($file);
  121. $result = array('error'=>0,'data'=>$data);
  122. }
  123. } catch (\Exception $e) {
  124. $result = array('error'=>1,'msg'=>$e->getMessage());
  125. }
  126. }
  127. else if($this->type=='baidu')
  128. {
  129. try {
  130. $this->ossClient->putObjectFromFile($this->bucket,$object,$file);
  131. $data['url'] = SITE_PROTOCOL.$this->bucket.'.'.$this->endpoint.'/'.$object;
  132. $data['path'] = $object;
  133. $data['size'] = filesize($file);
  134. $result = array('error'=>0,'data'=>$data);
  135. } catch (\Exception $e) {
  136. $result = array('error'=>1,'msg'=>$e->getMessage());
  137. }
  138. }
  139. else if($this->type=='s3')
  140. {
  141. try {
  142. $key = $object;
  143. $source = fopen($file, 'rb');
  144. $acl = 'public-read';
  145. $uploader = new ObjectUploader(
  146. $this->ossClient,
  147. $this->bucket,
  148. $key,
  149. $source,
  150. $acl
  151. );
  152. $ossresult = $uploader->upload();
  153. if ($ossresult["@metadata"]["statusCode"] == '200') {
  154. $data['url'] = $ossresult["ObjectURL"];
  155. $data['path'] = $object;
  156. $data['size'] = filesize($file);
  157. $result = array('error'=>0,'data'=>$data);
  158. }
  159. } catch (\Exception $e) {
  160. $result = array('error'=>1,'msg'=>$e->getMessage());
  161. }
  162. }
  163. return $result;
  164. }
  165. /**
  166. * @param $localfile
  167. * @param $object
  168. */
  169. public function downLoad($localfile,$object)
  170. {
  171. $object = str_replace($this->ossUrl,'',$object);
  172. $object= str_replace($this->ossInerUrl,'',$object);
  173. $result = [];
  174. if($this->type=='ali')
  175. {
  176. try{
  177. $options = array(
  178. OssClient::OSS_FILE_DOWNLOAD => $localfile
  179. );
  180. $this->ossClient->getObject($this->bucket, $object, $options);
  181. $result = array('error'=>0);
  182. } catch(OssException $e) {
  183. $result = array('error'=>1,'msg'=>$e->getMessage());
  184. }
  185. }
  186. else if($this->type=='tencent')
  187. {
  188. try {
  189. $key = $object; //此处的 key 为对象键,对象键是对象在存储桶中的唯一标识
  190. $this->ossClient->getObject(array('Bucket' => $this->bucket,'Key' => $key,'SaveAs' => $localfile));
  191. $result = array('error'=>0);
  192. } catch (\Exception $e) {
  193. $result = array('error'=>1,'msg'=>$e->getMessage());
  194. }
  195. }
  196. else if($this->type=='baidu')
  197. {
  198. try {
  199. $this->ossClient->getObjectToFile($this->bucket, $object, $localfile);
  200. $result = array('error'=>0);
  201. } catch (\Exception $e) {
  202. $result = array('error'=>1,'msg'=>$e->getMessage());
  203. }
  204. }
  205. else if($this->type=='s3')
  206. {
  207. try {
  208. $result = $this->ossClient->getObject([
  209. 'Bucket' => $this->bucket,
  210. 'Key' => $object
  211. ]);
  212. //header("Content-Type: {$result['ContentType']}");
  213. file_put_contents($localfile,$result['Body']);
  214. $result = array('error'=>0);
  215. } catch (S3Exception $e) {
  216. $result = array('error'=>1,'msg'=>$e->getMessage());
  217. }
  218. }
  219. return $result;
  220. }
  221. //删除单个文件
  222. public function delete($object){
  223. $object = str_replace($this->ossUrl,'',$object);
  224. $object= str_replace($this->ossInerUrl,'',$object);
  225. if($this->type=='ali')
  226. {
  227. try{
  228. $ossresult = $this->ossClient->deleteObject($this->bucket, $object);
  229. $result = array('error'=>0,'data'=>'删除成功');
  230. } catch(OssException $e) {
  231. $result = array('error'=>1,'msg'=>$e->getMessage());
  232. }
  233. }
  234. else if($this->type=='tencent')
  235. {
  236. try {
  237. $ossresult = $this->ossClient->deleteObject(array(
  238. 'Bucket' => $this->bucket, //存储桶名称
  239. 'Key' => $object
  240. ));
  241. $result = array('error'=>0,'data'=>'删除成功');
  242. } catch (\Exception $e) {
  243. $result = array('error'=>1,'msg'=>$e->getMessage());
  244. }
  245. }
  246. else if($this->type=='baidu')
  247. {
  248. try {
  249. $ossresult = $this->ossClient->deleteObject($this->bucket, $object);
  250. $result = array('error'=>0,'data'=>'删除成功');
  251. } catch (\Exception $e) {
  252. $result = array('error'=>1,'msg'=>$e->getMessage());
  253. }
  254. }
  255. else if($this->type=='s3')
  256. {
  257. try {
  258. $ossresult = $this->ossClient->deleteObject([
  259. 'Bucket' => $this->bucket,
  260. 'Key' => $object
  261. ]);
  262. $result = array('error'=>0,'data'=>'删除成功');
  263. } catch (S3Exception $e) {
  264. $result = array('error'=>1,'msg'=>$e->getMessage());
  265. }
  266. }
  267. return $result;
  268. }
  269. }
  270. ?>