springBoot项目中,参数更改为配置文件

以下为obs的工具类更改为配置文件

1.新建一个类,存放基本的参数

java 复制代码
import lombok.Data;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

@Data
@Configuration
public class OBSProperties {
    @Value("${endpoint}")
    private   String endpoint;
    @Value("${accessKeyId}")
    private  String accessKeyId;
    @Value("${accessKeySecret}")
    private  String accessKeySecret;
    @Value("${bucketName}")
    private  String bucketName;

2.工具类改造后

java 复制代码
public class ObsUtils {

    private static ObsUtils obs;

    public static ObsUtils getInstance() {
        if (obs == null) {
            obs = new ObsUtils();
        }
        return obs;
    }
    /**
     * 抽成共有的
     * @return
     */
    public static OBSProperties getOBSClient() {
        OBSProperties obsProperties= SpringUtil.getBean(OBSProperties.class);
        return  obsProperties;
    }

   
    private ObsClient getClient() {
        OBSProperties obsProperties= getOBSClient();
        // 初始化OSSClient
        ObsClient client = new ObsClient(obsProperties.getAccessKeyId(), obsProperties.getAccessKeySecret(), obsProperties.getEndpoint());
        return client;
    }
    private ObsClient getClient(String bucketName) {
        OBSProperties obsProperties= getOBSClient();
        // 初始化OSSClient
        ObsClient clientOut = new ObsClient(obsProperties.getAccessKeyId(), obsProperties.getAccessKeySecret(), obsProperties.getEndpoint());
        clientOut.createBucket(bucketName);
        return clientOut;
    }
  /**
     * 上传文件
     *
     * @param file     文件
     * @param fileName 文件存储名称
     * @return
     */
    public static PutObjectResult uploadFile(MultipartFile file, String key, String fileName)
            throws IOException {
        OBSProperties obsProperties= getOBSClient();
        // 初始化OSSClient
        ObsClient obsClient = new ObsClient(obsProperties.getAccessKeyId(), obsProperties.getAccessKeySecret(), obsProperties.getEndpoint());

        InputStream content = file.getInputStream();

        String objectKey= key+ "/"+ fileName;
        PutObjectRequest request =new  PutObjectRequest(obsProperties.getBucketName(),  objectKey, content);

        try {
            PutObjectResult result = obsClient.putObject(request);
            return  result;
        } catch (ObsException e) {
            logger.error("Error uploading file: " + e.getErrorCode() + " - " + e.getErrorMessage());
        } finally {
            obsClient.close();
        }
        return  null;
    }
相关推荐
SimonKing23 分钟前
AI大模型中转平台,无需科学上网就可以使用国外模型
java·后端·程序员
johnrui24 分钟前
WebFlux 与普通HTTP(Spring MVC)详细对比
spring·http·mvc
自我意识的多元宇宙25 分钟前
二叉树遍历方式代码解读(1递归)
java·数据结构·算法
逻辑驱动的ken34 分钟前
Java高频面试考点04
java·开发语言·算法·哈希算法·散列表
代码漫谈41 分钟前
Spring Boot 配置指南:约定大于配置的应用
java·spring boot·后端
黎雁·泠崖42 分钟前
二叉树遍历:LeetCode 144 / 94 / 145 之递归 + 分治 + 非递归
java·数据结构·算法·leetcode
凌波粒43 分钟前
LeetCode--347.前 K 个高频元素(栈和队列)
java·数据结构·算法·leetcode
GLPerryHsu43 分钟前
jar包的快速修改和重新发布
java·jar
程序员老邢1 小时前
【技术底稿 14】通用文件存储组件:SpringBoot 自动装配 + 多存储适配
java·spring boot·后端·阿里云·微服务·策略模式
zjneymar1 小时前
苍穹外卖中一些知识点和问题
java·linux·服务器