-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathS3Controller.java
More file actions
88 lines (74 loc) · 3.53 KB
/
S3Controller.java
File metadata and controls
88 lines (74 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package com.app.controller;
import com.app.service.S3Service;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;
@RestController
@RequiredArgsConstructor
@RequestMapping("/api/files")
public class S3Controller {
private final S3Service s3Service;
@PostMapping("/upload")
public ResponseEntity<String> uploadFile(@RequestPart("file") MultipartFile file,
@RequestParam(value = "isReadPublicly", defaultValue = "false") boolean isReadPublicly) {
boolean isUploaded = s3Service.uploadFile(file, isReadPublicly);
if (isUploaded) {
return ResponseEntity.ok("File uploaded successfully: " + file.getOriginalFilename());
} else {
return ResponseEntity.status(500).body("Failed to upload file: " + file.getOriginalFilename());
}
}
@GetMapping("/download/{key}")
public ResponseEntity<InputStreamResource> downloadFile(@PathVariable String key) {
InputStream fileStream = s3Service.downloadFileAsStream(key);
InputStreamResource resource = new InputStreamResource(fileStream);
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + key)
.contentType(MediaType.APPLICATION_OCTET_STREAM)
.body(resource);
}
@GetMapping("/create-bucket")
public ResponseEntity<String> createBucket(@RequestParam String bucketName) {
return ResponseEntity.ok(s3Service.createBucket(bucketName));
}
@GetMapping("/bucket-list")
public ResponseEntity<List<String>> getBucketList() {
return ResponseEntity.ok(s3Service.getBucketList());
}
@GetMapping("/list-buckets-with-regions")
public Map<String, String> listBucketsWithRegions() {
return s3Service.listBucketsWithRegions();
}
@GetMapping("/download-all-files-zip")
public ResponseEntity<StreamingResponseBody> downloadAllFilesAsZip(@RequestParam String bucketName) {
// Streaming response to handle large files efficiently
StreamingResponseBody responseBody = outputStream -> {
try (ZipOutputStream zos = new ZipOutputStream(outputStream)) {
s3Service.streamAllFilesAsZip(bucketName, zos);
} catch (IOException e) {
throw new RuntimeException("Error while streaming files to output stream", e);
}
};
// Set headers for ZIP file download
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Disposition", "attachment; filename=all-files.zip");
headers.add("Content-Type", "application/zip");
return new ResponseEntity<>(responseBody, headers, HttpStatus.OK);
}
@GetMapping("/move-files")
public String moveFiles(@RequestParam String sourceBucketName, @RequestParam String destinationBucketName) {
s3Service.moveFiles(sourceBucketName, destinationBucketName);
return "Files are being moved from " + sourceBucketName + " to " + destinationBucketName;
}
}