侧边栏壁纸
博主头像
小城雨巷 博主等级

行动起来,活在当下

  • 累计撰写 20 篇文章
  • 累计创建 6 个标签
  • 累计收到 4 条评论

目 录CONTENT

文章目录

Spring Boot 集成腾讯cos

Administrator
2023-10-18 / 0 评论 / 0 点赞 / 10 阅读 / 0 字

引入依赖

        <dependency>
            <groupId>com.qcloud</groupId>
            <artifactId>cos_api</artifactId>
            <version>5.6.8</version>
        </dependency>

配置文件

app.sj.tencent-cos.secret-id=xxxxxxx
app.sj.tencent-cos.secret-key=xxxxxxxxx
app.sj.tencent-cos.bucket=xxxxxxxx
app.sj.tencent-cos.endpoint=ap-shanghai
app.sj.tencent-cos.file-access-prefix=https://cos-1306265825.cos.ap-shanghai.myqcloud.com/
app.sj.tencent-cos.file-path=contract/

实体类映射配置文件

package com.qyszdata.sj.integration.properties;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import lombok.Data;
import lombok.ToString;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;

import javax.annotation.PostConstruct;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.io.StringWriter;

/**
 * @author outengfei
 * @email t15294977908@163.com
 * @date 2022-02-14 11:17
 */
@Component
@ConfigurationProperties(prefix = "app.sj")
@Data
@ToString
@Validated
@Slf4j
public class SjProperties {

    private TencentCos tencentCos;

    @Data
    public static class TencentCos{
        /**
         * SecretId
         */
        private String secretId;
        /**
         * SecretKey
         */
        private String secretKey;
        /**
         * Bucket
         */
        private String bucket;
        /**
         * Endpoint
         */
        private String endpoint;
        /**
         * 文件存放的目录
         */
        private String filePath;
        /**
         * 访问前缀
         */
        private String fileAccessPrefix;
    }

}

初始化客户端

  @PostConstruct
    public void postTencentCos(){

        BasicCOSCredentials cred = new BasicCOSCredentials(appProperties.getTencentCos().getSecretId(), appProperties.getTencentCos().getSecretKey());
        ClientConfig clientConfig = new ClientConfig(new Region(appProperties.getTencentCos().getEndpoint()));
        // 生成cos客户端
        cosClient = new COSClient(cred, clientConfig);

    }

上传接口

 public String uploadFilesToCos(MultipartFile file) {

        String fileName = file.getOriginalFilename();

        String type = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentType(type);
        try {
            String substring = fileName.substring(fileName.lastIndexOf("."));
            // 指定要上传到 COS 上的路径,此处看下文
            String uploadFileName = appProperties.getTencentCos().getFilePath() + IdUtil.simpleUUID() + System.currentTimeMillis() + substring;
            PutObjectRequest putObjectRequest = new PutObjectRequest(appProperties.getTencentCos().getBucket(), uploadFileName, file.getInputStream(), objectMetadata);
            PutObjectResult putObjectResult = cosClient.putObject(putObjectRequest);
            return appProperties.getTencentCos().getFileAccessPrefix() + uploadFileName;
        } catch (Exception e) {
            log.info(e.getMessage(), e);
        } finally {
            // 关闭客户端(关闭后台线程)
            cosClient.shutdown();
        }
        return null;
    }

0

评论区