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;
    }
相关推荐
bing_15818 分钟前
简单工厂模式 (Simple Factory Pattern) 在Spring Boot 中的应用
spring boot·后端·简单工厂模式
逸狼35 分钟前
【JavaEE进阶】Spring MVC(3)
spring·java-ee·mvc
天上掉下来个程小白42 分钟前
案例-14.文件上传-简介
数据库·spring boot·后端·mybatis·状态模式
风与沙的较量丶1 小时前
Java中的局部变量和成员变量在内存中的位置
java·开发语言
m0_748251721 小时前
SpringBoot3 升级介绍
java
极客先躯2 小时前
说说高级java每日一道面试题-2025年2月13日-数据库篇-请说说 MySQL 数据库的锁 ?
java·数据库·mysql·数据库的锁·模式分·粒度分·属性分
程序员侠客行2 小时前
Spring事务原理 二
java·后端·spring
小猫猫猫◍˃ᵕ˂◍3 小时前
备忘录模式:快速恢复原始数据
android·java·备忘录模式
liuyuzhongcc3 小时前
List 接口中的 sort 和 forEach 方法
java·数据结构·python·list
小天努力学java3 小时前
AI赋能传统系统:Spring AI Alibaba如何用大模型重构机票预订系统?
人工智能·spring