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;
    }
相关推荐
间彧21 小时前
SimpleDateFormat既然不推荐使用,为什么java 8+中不删除此类
java
金銀銅鐵1 天前
Spring 中的 initializeBean 方法的内部逻辑小总结
spring
间彧1 天前
DateTimeFormatter相比SimpleDateFormat在性能上有何差异?
java
间彧1 天前
为什么说SimpleDateFormat是经典的线程不安全类
java
MacroZheng1 天前
横空出世!MyBatis-Plus 同款 ES ORM 框架,用起来够优雅!
java·后端·elasticsearch
用户0332126663671 天前
Java 查找并替换 Excel 中的数据:详细教程
java
间彧1 天前
ThreadLocal实现原理与应用实践
java
若水不如远方1 天前
Netty的四种零拷贝机制:深入原理与实战指南
java·netty
用户7493636848431 天前
【开箱即用】一分钟使用java对接海外大模型gpt等对话模型,实现打字机效果
java
SimonKing1 天前
一键开启!Spring Boot 的这些「魔法开关」@Enable*,你用对了吗?
java·后端·程序员