Cos.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace common\components\uploaddrive;
  3. use Overtrue\Flysystem\Cos\CosAdapter;
  4. /**
  5. * Class Cos
  6. * @package common\components\uploaddrive
  7. */
  8. class Cos extends DriveInterface
  9. {
  10. /**
  11. * @param $baseInfo
  12. * @param $fullPath
  13. * @return mixed
  14. */
  15. protected function baseUrl($baseInfo, $fullPath)
  16. {
  17. $config = $this->config;
  18. if (empty($config['read_from_cdn'])) {
  19. $bucket = $config['bucket'] ?? '';
  20. $appid = $this->getNumber($config['bucket']) ?? '';
  21. $region = $config['area'] ?? '';
  22. $baseInfo['url'] = 'https://' . $bucket . '.cos.' . $region . '.myqcloud.com/' . $baseInfo['url'];
  23. } else {
  24. $baseInfo['url'] = $config['storage_cos_cdn'] . $baseInfo['url'];
  25. }
  26. return $baseInfo;
  27. }
  28. /**
  29. * @param $baseInfo
  30. * @param $fullPath
  31. * @return mixed
  32. */
  33. protected function domainName($baseInfo, $fullPath)
  34. {
  35. $config = $this->config;
  36. if (empty($config['read_from_cdn'])) {
  37. $bucket = $config['bucket'] ?? '';
  38. $appid = $this->getNumber($config['bucket']) ?? '';
  39. $region = $config['area'] ?? '';
  40. $baseInfo['domain_name'] = 'https://' . $bucket . '.cos.' . $region . '.myqcloud.com/';
  41. } else {
  42. $baseInfo['domain_name'] = $config['storage_cos_cdn'] ?? '';
  43. }
  44. return $baseInfo;
  45. }
  46. /**
  47. * @return mixed|void
  48. */
  49. protected function create()
  50. {
  51. $config = $this->config;
  52. $this->adapter = new CosAdapter([
  53. 'region' => $config['area'], // 'ap-guangzhou'
  54. 'credentials' => [
  55. 'appId' => $this->getNumber($config['bucket']), // 域名中数字部分
  56. 'secretId' => $config['secret_id'],
  57. 'secretKey' => $config['secret_key'],
  58. ],
  59. 'bucket' => $config['bucket'],
  60. 'timeout' => 60,
  61. 'connect_timeout' => 60,
  62. 'cdn' => $config['storage_cos_cdn'] ?? '',
  63. 'scheme' => 'https',
  64. 'read_from_cdn' => $config['read_from_cdn'] ?? '',
  65. ]);
  66. }
  67. /**
  68. * 获取字符串中的数字
  69. * @Author: lun
  70. * @DateTime: 2023/6/14 0014 18:46
  71. * @Copyright: copyright (c) 2021 广东七件事集团
  72. * @param $str
  73. * @return mixed
  74. */
  75. protected function getNumber($str)
  76. {
  77. if (preg_match('/\d+/', $str, $arr)) return $arr[0];
  78. }
  79. }