|
|
@@ -1,5 +1,10 @@
|
|
|
package com.njuzr.eaibackend.controller;
|
|
|
|
|
|
+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;
|
|
|
@@ -7,6 +12,9 @@ import org.springframework.web.bind.annotation.*;
|
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
|
@@ -20,24 +28,27 @@ import java.util.concurrent.TimeUnit;
|
|
|
@RestController
|
|
|
@RequestMapping("/api/file")
|
|
|
public class FileController {
|
|
|
- private final OssUtil ossUtil;
|
|
|
|
|
|
@Autowired
|
|
|
- public FileController(OssUtil ossUtil) {
|
|
|
- this.ossUtil = ossUtil;
|
|
|
- }
|
|
|
+ ECloudEosConfig eCloudEosConfig;
|
|
|
|
|
|
|
|
|
@PostMapping
|
|
|
public CompletableFuture<MyResponse> upload(@RequestParam("file") MultipartFile file) {
|
|
|
return CompletableFuture.supplyAsync(() -> {
|
|
|
- try {
|
|
|
- String filePath = "uploads/" + file.getOriginalFilename();
|
|
|
- String url = ossUtil.uploadFile(file.getInputStream(), filePath);
|
|
|
- log.info("url:" + url);
|
|
|
+ String filePath = "uploads/" + 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);
|
|
|
+ String url = String.format("%s/%s/%s", eCloudEosConfig.getEndpoint(), eCloudEosConfig.getBucketName(), file);
|
|
|
+ log.info("文件上传成功: {}, url: {}", filePath, url);
|
|
|
return MyResponse.success(url);
|
|
|
} catch (IOException e) {
|
|
|
- throw new RuntimeException("文件上传失败", e);
|
|
|
+ throw new MyException(500, "上传文件失败");
|
|
|
}
|
|
|
}).completeOnTimeout(
|
|
|
MyResponse.error(400,"文件上传超时,请稍后再试"),
|
|
|
@@ -48,13 +59,14 @@ public class FileController {
|
|
|
|
|
|
@GetMapping
|
|
|
public MyResponse getFiles() {
|
|
|
- return MyResponse.success(ossUtil.listFiles());
|
|
|
+ // 列出存储桶中的对象
|
|
|
+ ObjectListing objectListing = eCloudEosConfig.EosClient().listObjects(eCloudEosConfig.getBucketName());
|
|
|
+ List<String> files = new ArrayList<>();
|
|
|
+ objectListing.getObjectSummaries().forEach(objectSummary -> {
|
|
|
+ files.add(objectSummary.getKey());
|
|
|
+ });
|
|
|
+ return MyResponse.success(files);
|
|
|
}
|
|
|
|
|
|
- @DeleteMapping
|
|
|
- public MyResponse delete(@RequestParam String filePath) {
|
|
|
- ossUtil.deleteFile(filePath);
|
|
|
- return MyResponse.success("删除成功");
|
|
|
- }
|
|
|
|
|
|
}
|