|
|
@@ -1,5 +1,7 @@
|
|
|
package com.njuzr.eaibackend.controller;
|
|
|
|
|
|
+import com.amazonaws.HttpMethod;
|
|
|
+import com.amazonaws.services.s3.model.GeneratePresignedUrlRequest;
|
|
|
import com.amazonaws.services.s3.model.ObjectListing;
|
|
|
import com.amazonaws.services.s3.model.ObjectMetadata;
|
|
|
import com.amazonaws.services.s3.model.PutObjectResult;
|
|
|
@@ -13,8 +15,11 @@ import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStream;
|
|
|
+import java.net.URL;
|
|
|
import java.util.ArrayList;
|
|
|
+import java.util.Date;
|
|
|
import java.util.List;
|
|
|
+import java.util.UUID;
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@@ -36,7 +41,7 @@ public class FileController {
|
|
|
@PostMapping
|
|
|
public CompletableFuture<MyResponse> upload(@RequestParam("file") MultipartFile file) {
|
|
|
return CompletableFuture.supplyAsync(() -> {
|
|
|
- String filePath = "uploads/" + file.getOriginalFilename();
|
|
|
+ String filePath = "uploads/" + UUID.randomUUID() + file.getOriginalFilename();
|
|
|
ObjectMetadata metadata = new ObjectMetadata();
|
|
|
metadata.setContentLength(file.getSize());
|
|
|
metadata.setContentType(file.getContentType());
|
|
|
@@ -44,7 +49,14 @@ public class FileController {
|
|
|
|
|
|
try (InputStream inputStream = file.getInputStream()) {
|
|
|
eCloudEosConfig.EosClient().putObject(eCloudEosConfig.getBucketName(), filePath, inputStream, metadata);
|
|
|
- String url = String.format("%s/%s/%s", eCloudEosConfig.getEndpoint(), eCloudEosConfig.getBucketName(), file);
|
|
|
+ Date expiration = new Date();
|
|
|
+ expiration.setTime(expiration.getTime() + 1000L * 60 * 1000);
|
|
|
+
|
|
|
+ GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(eCloudEosConfig.getBucketName(), filePath)
|
|
|
+ .withMethod(HttpMethod.GET)
|
|
|
+ .withExpiration(expiration);
|
|
|
+ URL presignedUrl = eCloudEosConfig.EosClient().generatePresignedUrl(request);
|
|
|
+ String url = presignedUrl.toString();
|
|
|
log.info("文件上传成功: {}, url: {}", filePath, url);
|
|
|
return MyResponse.success(url);
|
|
|
} catch (IOException e) {
|