Implement real async COS image upload

This commit is contained in:
Tim
2025-07-01 10:18:53 +08:00
parent 48abc381db
commit 87b9e9acd3
8 changed files with 77 additions and 10 deletions

View File

@@ -29,7 +29,7 @@ public class UserController {
@PostMapping("/me/avatar")
public ResponseEntity<?> uploadAvatar(@RequestParam("file") MultipartFile file,
Authentication auth) throws IOException {
String url = imageUploader.upload(file.getBytes(), file.getOriginalFilename());
String url = imageUploader.upload(file.getBytes(), file.getOriginalFilename()).join();
userService.updateAvatar(auth.getName(), url);
return ResponseEntity.ok(Map.of("url", url));
}

View File

@@ -1,24 +1,66 @@
package com.openisle.service;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.model.ObjectMetadata;
import com.qcloud.cos.model.PutObjectRequest;
import com.qcloud.cos.region.Region;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
import org.springframework.stereotype.Service;
import java.io.ByteArrayInputStream;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* ImageUploader implementation using Tencent Cloud COS.
* For simplicity this demo just returns a URL composed of the base URL and file name.
*/
@Service
public class CosImageUploader extends ImageUploader {
private final COSClient cosClient;
private final String bucketName;
private final String baseUrl;
private final ExecutorService executor = Executors.newFixedThreadPool(2,
new CustomizableThreadFactory("cos-upload-"));
public CosImageUploader(@Value("${cos.base-url:https://example.com}") String baseUrl) {
@org.springframework.beans.factory.annotation.Autowired
public CosImageUploader(
@Value("${cos.secret-id:}") String secretId,
@Value("${cos.secret-key:}") String secretKey,
@Value("${cos.region:ap-guangzhou}") String region,
@Value("${cos.bucket-name:}") String bucketName,
@Value("${cos.base-url:https://example.com}") String baseUrl) {
COSCredentials cred = new BasicCOSCredentials(secretId, secretKey);
ClientConfig config = new ClientConfig(new Region(region));
this.cosClient = new COSClient(cred, config);
this.bucketName = bucketName;
this.baseUrl = baseUrl;
}
// for tests
CosImageUploader(COSClient cosClient, String bucketName, String baseUrl) {
this.cosClient = cosClient;
this.bucketName = bucketName;
this.baseUrl = baseUrl;
}
@Override
public String upload(byte[] data, String filename) {
// In a real implementation you would call COS SDK here
return baseUrl + "/" + filename;
public CompletableFuture<String> upload(byte[] data, String filename) {
return CompletableFuture.supplyAsync(() -> {
ObjectMetadata meta = new ObjectMetadata();
meta.setContentLength(data.length);
PutObjectRequest req = new PutObjectRequest(
bucketName,
filename,
new ByteArrayInputStream(data),
meta);
cosClient.putObject(req);
return baseUrl + "/" + filename;
}, executor);
}
}

View File

@@ -10,5 +10,10 @@ public abstract class ImageUploader {
* @param filename name of the file
* @return accessible URL of the uploaded file
*/
public abstract String upload(byte[] data, String filename);
/**
* Upload an image asynchronously and return a future of its accessible URL.
* Implementations should complete the future exceptionally on failures so
* callers can react accordingly.
*/
public abstract java.util.concurrent.CompletableFuture<String> upload(byte[] data, String filename);
}