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;
    }
相关推荐
胡桃夹夹子3 分钟前
存档111111111
java·开发语言
不会编程的小寒5 分钟前
C++ 中string的用法
java·开发语言
乐悠小码23 分钟前
Java设计模式精讲---02抽象工厂模式
java·设计模式·抽象工厂模式
数据的世界0126 分钟前
技术变革:为何C#与.NET是未来的开发方向
java·c#·.net
向上的车轮32 分钟前
Actix Web适合什么类型的Web应用?可以部署 Java 或 .NET 的应用程序?
java·前端·rust·.net
脸大是真的好~41 分钟前
黑马JAVAWeb-03 SpringBootWeb-分层解耦-三层架构-@SpringBootApplication注解-IOC控制反转-DI依赖注入
java
微露清风1 小时前
系统性学习C++-第十讲-stack 和 quene
java·c++·学习
一蓑烟雨任平生√1 小时前
两种上传图片的方式——91张先生
java·ossinsight
凤凰战士芭比Q1 小时前
部署我的世界-java版服务器-frp内网穿透
java·服务器
小肖爱笑不爱笑1 小时前
2025/11/5 IO流(字节流、字符流、字节缓冲流、字符缓冲流) 计算机存储规则(ASCII、GBK、Unicode)
java·开发语言·算法