Aliyun.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. namespace core\upload;
  3. use core\exception\UploadFileException;
  4. use OSS\Core\OssException;
  5. use OSS\OssClient;
  6. /**
  7. * 阿里云存储引擎 (OSS)
  8. */
  9. class Aliyun extends BaseUpload
  10. {
  11. protected function initialize(array $config = [])
  12. {
  13. parent::initialize($config);
  14. }
  15. public function client()
  16. {
  17. // true为开启CNAME。CNAME是指将自定义域名绑定到存储空间上。
  18. // $is_cname = false;
  19. $access_key_id = $this->config['access_key'];
  20. $access_key_secret = $this->config['secret_key'];
  21. $endpoint = $this->config['endpoint'];// yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
  22. return new OssClient($access_key_id, $access_key_secret, $endpoint);
  23. }
  24. /**
  25. * 执行上传
  26. * @param string $dir
  27. * @return true
  28. */
  29. public function upload(string $dir)
  30. {
  31. $this->validate();
  32. $bucket = $this->config['bucket'];
  33. try {
  34. $this->client()->uploadFile(
  35. $bucket,
  36. $this->getFullPath($dir),
  37. $this->getRealPath()
  38. );
  39. return true;
  40. } catch ( OssException $e ) {
  41. throw new UploadFileException($e->getMessage());
  42. }
  43. }
  44. /**
  45. * base64上云
  46. * @param string $base64_data
  47. * @param string|null $key
  48. * @return true
  49. */
  50. public function base64(string $base64_data, ?string $key = null)
  51. {
  52. $bucket = $this->config['bucket'];
  53. try {
  54. $base64_file = base64_decode($base64_data);
  55. if (!$base64_file) throw new UploadFileException('FILE_ERROE');
  56. $this->client()->putObject(
  57. $bucket,
  58. $key,
  59. $base64_file
  60. );
  61. return true;
  62. } catch ( OssException $e ) {
  63. throw new UploadFileException($e->getMessage());
  64. }
  65. }
  66. /**
  67. * Notes: 抓取远程资源
  68. * @param string $url
  69. * @param string|null $key
  70. * @return true
  71. */
  72. public function fetch(string $url, ?string $key = null)
  73. {
  74. $bucket = $this->config['bucket'];
  75. try {
  76. $content = file_get_contents($url);
  77. $this->client()->putObject(
  78. $bucket,
  79. $key,
  80. $content
  81. );
  82. return true;
  83. } catch ( OssException $e ) {
  84. throw new UploadFileException($e->getMessage());
  85. }
  86. }
  87. /**
  88. * 删除文件
  89. * @param string $file_name
  90. * @return true
  91. */
  92. public function delete(string $file_name)
  93. {
  94. $bucket = $this->config['bucket'];
  95. try {
  96. $this->client()->deleteObject($bucket, $file_name);
  97. return true;
  98. } catch ( OssException $e ) {
  99. throw new UploadFileException($e->getMessage());
  100. }
  101. }
  102. public function thumb($file_path, $thumb_type)
  103. {
  104. $thumb_config = config('upload.thumb.thumb_type');
  105. $thumb_data = [];
  106. foreach ($thumb_config as $k => $v) {
  107. if ($thumb_type == 'all' || $thumb_type == $k || (is_array($thumb_type) && in_array($k, $thumb_type))) {
  108. $width = $v['width'];
  109. $height = $v['height'];
  110. //拼装缩略路径
  111. $item_thumb = $file_path . '?x-oss-process=image/resize,h_' . $height . ',w_' . $width;
  112. $thumb_data[$k] = $item_thumb;
  113. }
  114. }
  115. return $thumb_data;
  116. }
  117. }