springboot整合腾讯云cos进行上传、下载、删除文件

参考腾讯官方文档

pom

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

application.yml

tencent:
  cos:
    accessKey: 你的秘钥id
    secretKey: 你的秘钥
    region: 你存储桶所在的区域
    bucket: 你的存储桶名称


spring:
  servlet:
    multipart:
      enabled: true
      file-size-threshold: 5MB
      max-file-size: 20MB

代码

  1. TencentCosProperties
@Data //lombok
@Component
@ConfigurationProperties(prefix = "tencent.cos")
public class TencentCosProperties {
   
   

    private String accessKey;
    private String secretKey;
    private String region;
    private String bucket;
}
  1. CosController
@RestController
public class CosController {
   
   

    @Autowired
    private TencentCosProperties tencentCosProperties;

    @PostMapping
### Spring Boot 整合腾讯云 COS 实现文件上传 #### 一、引入依赖 为了使Spring Boot项目能够访问并操作腾讯云COS服务,在`pom.xml`中加入如下所示的Maven依赖项[^2]: ```xml <dependencies> <!-- 腾讯云存储依赖 --> <dependency> <groupId>com.qcloud</groupId> <artifactId>cos_api</artifactId> <version>5.2.4</version> </dependency> </dependencies> ``` #### 二、配置YAML参数 接着在项目的application.yml或application.properties文件里设置好连接至腾讯云所需的认证信息其他必要选项,比如区域名称地区ID等。以下是基于YAML格式的一个例子: ```yaml tencent: cloud: secret-id: your_secret_id_here secret-key: your_secret_key_here region-name: ap-guangzhou bucket-name: examplebucket-1250000000 ``` #### 三、创建工具类用于处理文件上传逻辑 定义一个Java类来封装与COS交互的具体业务功能,这里采用的是@Configuration加上@Bean的方式来注册bean实例以便于后续调用[^3]。 ```java import com.qcloud.cos.COSClient; import org.springframework.context.annotation.Bean; import org.springframework.stereotype.Component; @Component public class CosUtil { private final String secretId; private final String secretKey; private final String regionName; private final String bucketName; public CosUtil(@Value("${tencent.cloud.secret-id}") String secretId, @Value("${tencent.cloud.secret-key}") String secretKey, @Value("${tencent.cloud.region-name}") String regionName, @Value("${tencent.cloud.bucket-name}") String bucketName){ this.secretId = secretId; this.secretKey = secretKey; this.regionName = regionName; this.bucketName = bucketName; } @Bean(name="cosClient") public COSClient getCosClient(){ // 初始化客户端... return new COSClient(secretId,secretKey,regionName); } } ``` #### 四、编写控制器接收前端传来的文件数据 最后一步是在Controller层构建API接口接受来自用户的HTTP请求并将接收到的数据转发给之前提到过的CosUtil来进行实际的操作[^1]。 ```java @RestController @RequestMapping("/api/file") public class FileUploadController { @Autowired private CosUtil cosUtil; @PostMapping("/upload") public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file) throws IOException{ try (InputStream inputStream=file.getInputStream()){ // 使用工具类完成上传过程... cosUtil.upload(inputStream,file.getOriginalFilename()); return ResponseEntity.ok().body("success"); } catch(Exception e){ throw new RuntimeException(e.getMessage(),e); } } } ``` 以上就是整个流程的大致介绍以及部分核心代码片段,具体细节还需要根据官方文档个人需求做适当调整。
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值