123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- namespace app\common\service\Oss;
- use Exception;
- use OSS\OssClient;
- use RuntimeException;
- use think\Config;
- class Common
- {
- protected $_ossClient = null;
- protected $_endpoint = '';
- protected $_accessKeyId = '';
- protected $_accessKeySecret = '';
- protected $_bucket = '';
-
- public function __construct($bucket = ''){
- $this->_endpoint = Config::get('OSS_ENDPOINT');
- $this->_accessKeyId = Config::get('OSS_ACCESS_KEY_ID');
- $this->_accessKeySecret = Config::get('ACCESS_KEY_SECRET');
- $this->setBucketName(!empty($bucket) ? $bucket : Config::get('ALIYUN_OSS_BUCKET'));
-
- if (is_file(EXTEND_PATH.'OSS/autoload.php')) {
- require_once EXTEND_PATH.'OSS/autoload.php';
- }
- $this->getOssClient();
- }
-
- /**
- * 切换OSS Bucket
- * @param string $bucketName
- */
- public function setBucketName(string $bucketName) {
- $this->_bucket = $bucketName;
- }
-
- public function getBucketName() {
- return $this->_bucket;
- }
- /**
- * 根据Config配置,得到一个OssClient实例
- * @return OssClient
- */
- public function getOssClient() {
- if (empty($this->_ossClient)) {
- try {
- $this->_ossClient = new OssClient($this->_accessKeyId, $this->_accessKeySecret, $this->_endpoint, false);
- } catch (Exception $e) {
- throw new RuntimeException($e->getMessage());
- }
- }
- return $this->_ossClient;
- }
-
- }
|