---
date: 2025-03-28 03:46:49
title: Cos对象存储
permalink: /pages/89cd20
top: 2
tags:
  - Spring Boot
author:
  name: 华总
  link: https://xiaoying.org.cn
categories:
  - 后端
  - 功能扩展
  - 通用模块

---








## 引用

```java
@RestController
@Slf4j
@RequestMapping("/cos")
public class CosController {
   ...
    @Resource
    private CosService cosService;
  ...
}
```

## 文件上传

```java
@PostMapping("/upload")
@Operation(summary = "文件上传")
public BaseResponse<String> upload(MultipartFile file, String biz) {
  return cosService.uploadObject(file, biz);
}
```

## 文件下载到本地

```java
 @PostMapping("/downloadLocal")
 @Operation(summary = "文件下载到本地")
 public void downloadToLocal(String url, String localFilePath) {
   cosService.download(url, localFilePath);
}
```

## 文件下载到流

```java
 @PostMapping("/download")
 @Operation(summary = "文件下载到流")
 public byte[] download(String url) throws IOException {
   return cosService.getObject(url);
}
```

## 文件删除

```java
@PostMapping("/delete")
@Operation(summary = "文件删除")
public BaseResponse<String> delete(String url) {
   return cosService.deleteObject(url);
}
```

## 完整代码

```java
@RestController
@Slf4j
@RequestMapping("/cos")
public class CosController {

    @Resource
    private CosService cosService;

    @PostMapping("/upload")
    @Operation(summary = "文件上传")
    public BaseResponse<String> upload(MultipartFile file, String biz) {

        return cosService.uploadObject(file, biz);

    }

    @PostMapping("/downloadLocal")
    @Operation(summary = "文件下载到本地")
    public void downloadToLocal(String url, String localFilePath) {

        cosService.download(url, localFilePath);

    }

    @PostMapping("/download")
    @Operation(summary = "文件下载到流")
    public byte[] download(String url) throws IOException {

        return cosService.getObject(url);

    }

    @PostMapping("/delete")
    @Operation(summary = "文件删除")
    public BaseResponse<String> delete(String url) {

      return cosService.deleteObject(url);

    }


}

```

