<?php

namespace app\service;

use OSS\Core\OssException;
use OSS\OssClient;
use think\facade\Config;
use think\facade\Log;

class AliOss
{
    private static $oss = null;

    private function __construct()
    {
    }

    private function __clone()
    {
    }

    /**
     * 阿里云OSS
     *
     */
    public static function config()
    {
        Config::load('extra/alioss', 'alioss');
        return config('alioss');
    }

    // 阿里云OSS实例  单例模式
    public static function instance(): ?OssClient
    {
        if (self::$oss == null) {
            try {
                $conf = self::config();
                self::$oss =  new OssClient($conf['accessKeyId'], $conf['accessKeySecret'], $conf['endpoint']);
            } catch (OssException $e) {
                Log::error('实例化阿里云OSS失败: ' . $e->getMessage());
                return null;
            }
        }
        return self::$oss;
    }
}