|
|
@@ -1,13 +1,11 @@
|
|
|
package com.njuzr.eaibackend.controller;
|
|
|
|
|
|
+import com.amazonaws.AmazonServiceException;
|
|
|
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;
|
|
|
import com.njuzr.eaibackend.config.ECloudEosConfig;
|
|
|
import com.njuzr.eaibackend.exception.MyException;
|
|
|
-import com.njuzr.eaibackend.utils.OssUtil;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
@@ -20,8 +18,7 @@ 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;
|
|
|
+
|
|
|
|
|
|
/**
|
|
|
* @author: Leonezhurui
|
|
|
@@ -39,40 +36,32 @@ public class FileController {
|
|
|
|
|
|
|
|
|
@PostMapping
|
|
|
- public CompletableFuture<MyResponse> upload(@RequestParam("file") MultipartFile file) {
|
|
|
- return CompletableFuture.supplyAsync(() -> {
|
|
|
+ public MyResponse upload(@RequestParam("file") MultipartFile file) {
|
|
|
String filePath = "uploads/" + UUID.randomUUID() + file.getOriginalFilename();
|
|
|
- ObjectMetadata metadata = new ObjectMetadata();
|
|
|
- metadata.setContentLength(file.getSize());
|
|
|
- metadata.setContentType(file.getContentType());
|
|
|
- metadata.setHeader("original-filename", file.getOriginalFilename());
|
|
|
-
|
|
|
try (InputStream inputStream = file.getInputStream()) {
|
|
|
- eCloudEosConfig.EosClient().putObject(eCloudEosConfig.getBucketName(), filePath, inputStream, metadata);
|
|
|
- Date expiration = new Date();
|
|
|
- expiration.setTime(expiration.getTime() + 1000L * 60 * 1000);
|
|
|
-
|
|
|
+ eCloudEosConfig.eosClient().putObject(eCloudEosConfig.getBucketName(), filePath, inputStream, null);
|
|
|
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(eCloudEosConfig.getBucketName(), filePath)
|
|
|
.withMethod(HttpMethod.GET)
|
|
|
- .withExpiration(expiration);
|
|
|
- URL presignedUrl = eCloudEosConfig.EosClient().generatePresignedUrl(request);
|
|
|
+ .withExpiration(new Date(System.currentTimeMillis() + 7 * 24 * 60 * 60 * 1000));
|
|
|
+
|
|
|
+ URL presignedUrl = eCloudEosConfig.eosClient().generatePresignedUrl(request);
|
|
|
String url = presignedUrl.toString();
|
|
|
log.info("文件上传成功: {}, url: {}", filePath, url);
|
|
|
return MyResponse.success(url);
|
|
|
- } catch (IOException e) {
|
|
|
- throw new MyException(500, "上传文件失败");
|
|
|
+ } catch (AmazonServiceException e) {
|
|
|
+ // 这是服务端返回的错误(如权限不足、Bucket不存在等)
|
|
|
+ log.error("EOS服务端处理请求失败。错误信息: {}", e.getMessage());
|
|
|
+ throw new MyException(500, "云存储服务异常: " + e.getErrorMessage());
|
|
|
+ }catch (IOException e) {
|
|
|
+ log.error("EOS服务端处理请求失败。错误信息: {}", e.getMessage());
|
|
|
+ throw new MyException(500, "上传文件失败" + e.getMessage());
|
|
|
}
|
|
|
- }).completeOnTimeout(
|
|
|
- MyResponse.error(400,"文件上传超时,请稍后再试"),
|
|
|
- 300, // 超时时间(秒)
|
|
|
- TimeUnit.SECONDS
|
|
|
- );
|
|
|
}
|
|
|
|
|
|
@GetMapping
|
|
|
public MyResponse getFiles() {
|
|
|
// 列出存储桶中的对象
|
|
|
- ObjectListing objectListing = eCloudEosConfig.EosClient().listObjects(eCloudEosConfig.getBucketName());
|
|
|
+ ObjectListing objectListing = eCloudEosConfig.eosClient().listObjects(eCloudEosConfig.getBucketName());
|
|
|
List<String> files = new ArrayList<>();
|
|
|
objectListing.getObjectSummaries().forEach(objectSummary -> {
|
|
|
files.add(objectSummary.getKey());
|