| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- namespace common\components\uploaddrive;
- use Overtrue\Flysystem\Cos\CosAdapter;
- /**
- * Class Cos
- * @package common\components\uploaddrive
- */
- class Cos extends DriveInterface
- {
- /**
- * @param $baseInfo
- * @param $fullPath
- * @return mixed
- */
- protected function baseUrl($baseInfo, $fullPath)
- {
- $config = $this->config;
- if (empty($config['read_from_cdn'])) {
- $bucket = $config['bucket'] ?? '';
- $appid = $this->getNumber($config['bucket']) ?? '';
- $region = $config['area'] ?? '';
- $baseInfo['url'] = 'https://' . $bucket . '.cos.' . $region . '.myqcloud.com/' . $baseInfo['url'];
- } else {
- $baseInfo['url'] = $config['storage_cos_cdn'] . $baseInfo['url'];
- }
- return $baseInfo;
- }
- /**
- * @param $baseInfo
- * @param $fullPath
- * @return mixed
- */
- protected function domainName($baseInfo, $fullPath)
- {
- $config = $this->config;
- if (empty($config['read_from_cdn'])) {
- $bucket = $config['bucket'] ?? '';
- $appid = $this->getNumber($config['bucket']) ?? '';
- $region = $config['area'] ?? '';
- $baseInfo['domain_name'] = 'https://' . $bucket . '.cos.' . $region . '.myqcloud.com/';
- } else {
- $baseInfo['domain_name'] = $config['storage_cos_cdn'] ?? '';
- }
- return $baseInfo;
- }
- /**
- * @return mixed|void
- */
- protected function create()
- {
- $config = $this->config;
- $this->adapter = new CosAdapter([
- 'region' => $config['area'], // 'ap-guangzhou'
- 'credentials' => [
- 'appId' => $this->getNumber($config['bucket']), // 域名中数字部分
- 'secretId' => $config['secret_id'],
- 'secretKey' => $config['secret_key'],
- ],
- 'bucket' => $config['bucket'],
- 'timeout' => 60,
- 'connect_timeout' => 60,
- 'cdn' => $config['storage_cos_cdn'] ?? '',
- 'scheme' => 'https',
- 'read_from_cdn' => $config['read_from_cdn'] ?? '',
- ]);
- }
- /**
- * 获取字符串中的数字
- * @Author: lun
- * @DateTime: 2023/6/14 0014 18:46
- * @Copyright: copyright (c) 2021 广东七件事集团
- * @param $str
- * @return mixed
- */
- protected function getNumber($str)
- {
- if (preg_match('/\d+/', $str, $arr)) return $arr[0];
- }
- }
|