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

@@ -66,6 +66,11 @@
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<optional>true</optional> <optional>true</optional>
</dependency> </dependency>
<dependency>
<groupId>com.qcloud</groupId>
<artifactId>cos_api</artifactId>
<version>5.6.247</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>

View File

@@ -29,7 +29,7 @@ public class UserController {
@PostMapping("/me/avatar") @PostMapping("/me/avatar")
public ResponseEntity<?> uploadAvatar(@RequestParam("file") MultipartFile file, public ResponseEntity<?> uploadAvatar(@RequestParam("file") MultipartFile file,
Authentication auth) throws IOException { 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); userService.updateAvatar(auth.getName(), url);
return ResponseEntity.ok(Map.of("url", url)); return ResponseEntity.ok(Map.of("url", url));
} }

View File

@@ -1,24 +1,66 @@
package com.openisle.service; 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.beans.factory.annotation.Value;
import org.springframework.scheduling.concurrent.CustomizableThreadFactory;
import org.springframework.stereotype.Service; 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. * ImageUploader implementation using Tencent Cloud COS.
* For simplicity this demo just returns a URL composed of the base URL and file name.
*/ */
@Service @Service
public class CosImageUploader extends ImageUploader { public class CosImageUploader extends ImageUploader {
private final COSClient cosClient;
private final String bucketName;
private final String baseUrl; 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; this.baseUrl = baseUrl;
} }
@Override @Override
public String upload(byte[] data, String filename) { public CompletableFuture<String> upload(byte[] data, String filename) {
// In a real implementation you would call COS SDK here return CompletableFuture.supplyAsync(() -> {
return baseUrl + "/" + filename; 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 * @param filename name of the file
* @return accessible URL of the uploaded 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);
} }

View File

@@ -5,6 +5,10 @@ spring.jpa.hibernate.ddl-auto=update
resend.api.key=${RESEND_API_KEY:} resend.api.key=${RESEND_API_KEY:}
cos.base-url=${COS_BASE_URL:https://example.com} cos.base-url=${COS_BASE_URL:https://example.com}
cos.secret-id=${COS_SECRET_ID:}
cos.secret-key=${COS_SECRET_KEY:}
cos.region=${COS_REGION:ap-guangzhou}
cos.bucket-name=${COS_BUCKET_NAME:}
app.jwt.secret=${JWT_SECRET:ChangeThisSecretKeyForJwt} app.jwt.secret=${JWT_SECRET:ChangeThisSecretKeyForJwt}
app.jwt.expiration=${JWT_EXPIRATION:86400000} app.jwt.expiration=${JWT_EXPIRATION:86400000}

View File

@@ -49,7 +49,7 @@ class UserControllerTest {
@Test @Test
void uploadAvatar() throws Exception { void uploadAvatar() throws Exception {
MockMultipartFile file = new MockMultipartFile("file", "a.png", MediaType.IMAGE_PNG_VALUE, "img".getBytes()); MockMultipartFile file = new MockMultipartFile("file", "a.png", MediaType.IMAGE_PNG_VALUE, "img".getBytes());
Mockito.when(imageUploader.upload(any(), eq("a.png"))).thenReturn("http://img/a.png"); Mockito.when(imageUploader.upload(any(), eq("a.png"))).thenReturn(java.util.concurrent.CompletableFuture.completedFuture("http://img/a.png"));
mockMvc.perform(multipart("/api/users/me/avatar").file(file).principal(new UsernamePasswordAuthenticationToken("alice","p"))) mockMvc.perform(multipart("/api/users/me/avatar").file(file).principal(new UsernamePasswordAuthenticationToken("alice","p")))
.andExpect(status().isOk()) .andExpect(status().isOk())

View File

@@ -1,14 +1,21 @@
package com.openisle.service; package com.openisle.service;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.model.PutObjectRequest;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
class CosImageUploaderTest { class CosImageUploaderTest {
@Test @Test
void uploadReturnsUrl() { void uploadReturnsUrl() {
CosImageUploader uploader = new CosImageUploader("http://cos.example.com"); COSClient client = mock(COSClient.class);
String url = uploader.upload("data".getBytes(), "img.png"); CosImageUploader uploader = new CosImageUploader(client, "bucket", "http://cos.example.com");
String url = uploader.upload("data".getBytes(), "img.png").join();
verify(client).putObject(any(PutObjectRequest.class));
assertEquals("http://cos.example.com/img.png", url); assertEquals("http://cos.example.com/img.png", url);
} }
} }

View File

@@ -6,6 +6,10 @@ spring.jpa.hibernate.ddl-auto=create-drop
resend.api.key=dummy resend.api.key=dummy
cos.base-url=http://test.example.com cos.base-url=http://test.example.com
cos.secret-id=dummy
cos.secret-key=dummy
cos.region=ap-guangzhou
cos.bucket-name=testbucket
app.jwt.secret=TestSecret app.jwt.secret=TestSecret
app.jwt.expiration=3600000 app.jwt.expiration=3600000