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;
    }
相关推荐
梁云亮14 分钟前
SpringBoot中缓存@Cacheable出错
spring boot·缓存
天天摸鱼的java工程师17 分钟前
CTO新项目直接上MySQL 8.0,老系统仍是5.7
java·后端·mysql
bxlj_jcj18 分钟前
解锁Java多级缓存:性能飞升的秘密武器
java·缓存·面试·架构
未来并未来20 分钟前
Redis 缓存问题及其解决方案
java·redis·缓存
快乐肚皮40 分钟前
MySQL集群模式详解:架构、优缺点与生产环境选型指南
java·mysql
季鸢1 小时前
Java设计模式之备忘录模式详解
java·设计模式·备忘录模式
异常君1 小时前
Java 逃逸分析:让你的代码性能飙升的秘密
java·面试·代码规范
迢迢星万里灬1 小时前
Java求职者面试:Spring、Spring Boot、Spring MVC与MyBatis技术深度解析
java·spring boot·spring·面试·mybatis·spring mvc
天天摸鱼的java工程师1 小时前
Nacos 2.0 + 为啥非要三个端口?一次踩坑实录
java·后端
SimonKing1 小时前
5分钟了解,Mysql事务隔离级别
java·后端·架构