- Introduzir dependências
pom.xml
Adicione as seguintes dependências no arquivo :
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.11.0</version>
</dependency>
- Arquivo de configuração
application.yml
Adicione informações de configuração do Alibaba Cloud OSS ao arquivo :
aliyun:
oss:
endpoint: <your-endpoint>
access-key-id: <your-access-key-id>
access-key-secret: <your-access-key-secret>
bucket-name: <your-bucket-name>
- Escreva uma interface para upload de arquivos
A interface para upload de arquivos pode ser usada para MultipartFile
receber arquivos carregados e, em seguida, chamar a API do Alibaba Cloud OSS para fazer upload dos arquivos para o OSS.
@RestController
public class FileUploadController {
@Autowired
private OSSClient ossClient;
@Value("${aliyun.oss.bucket-name}")
private String bucketName;
@PostMapping("/upload")
public String uploadFile(@RequestParam("file") MultipartFile file) throws IOException {
String fileName = file.getOriginalFilename();
InputStream inputStream = file.getInputStream();
ossClient.putObject(bucketName, fileName, inputStream);
return "Upload file " + fileName + " success";
}
}
- Escreva uma interface para baixar arquivos
A interface para download de arquivos pode baixar o arquivo para o local chamando a API do Alibaba Cloud OSS e, em seguida, gravar o arquivo no fluxo de saída e devolvê-lo ao cliente.
@RestController
public class FileDownloadController {
@Autowired
private OSSClient ossClient;
@Value("${aliyun.oss.bucket-name}")
private String bucketName;
@GetMapping("/download")
public void downloadFile(@RequestParam("file") String fileName, HttpServletResponse response) throws IOException {
OSSObject ossObject = ossClient.getObject(bucketName, fileName);
InputStream inputStream = ossObject.getObjectContent();
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
IOUtils.copy(inputStream, response.getOutputStream());
response.flushBuffer();
}
}
- Escreva uma interface para visualizar arquivos
A interface para visualização de arquivos pode obter a URL do arquivo chamando a API do Alibaba Cloud OSS e, em seguida, retornar a URL ao cliente.
@RestController
public class FileViewController {
@Autowired
private OSSClient ossClient;
@Value("${aliyun.oss.bucket-name}")
private String bucketName;
@GetMapping("/view")
public String viewFile(@RequestParam("file") String fileName) {
Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000);
URL url = ossClient.generatePresignedUrl(bucketName, fileName, expiration);
return url.toString();
}
}
- Escreva uma interface para excluir arquivos
A interface de exclusão de arquivos permite excluir arquivos especificados chamando a API do Alibaba Cloud OSS.
@RestController
public class FileDeleteController {
@Autowired
private OSSClient ossClient;
@Value("${aliyun.oss.bucket-name}")
private String bucketName;
@DeleteMapping("/delete")
public String deleteFile(@RequestParam("file") String fileName) {
ossClient.deleteObject(bucketName, fileName);
return "Delete file " + fileName + " success";
}
}
Nota: O código acima OSSClient
foi abandonado na nova versão do SDK e você pode usar OSS
a interface. Por exemplo:
@Autowired
private OSS oss;
oss.putObject(bucketName, fileName, inputStream);
Além disso, o código-fonte deste projeto está em gitee https://gitee.com/ZheShuCheng/case Se isso ajudar você, clique em Iniciar. Obrigado.