Skip to content

Add S3 bucket management and file upload/download features #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>s3</artifactId>
<version>2.25.70</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand Down
45 changes: 45 additions & 0 deletions src/main/java/com/app/config/AwsS3Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.app.config;


import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;

@Configuration
@ConditionalOnProperty(name = "aws.s3.enabled", havingValue = "true")
@Log4j2
public class AwsS3Config {

@Value("${aws.s3.region:us-east-1}")
private String awsRegion;
@Value("${aws.s3.accessKeyId:us-east-1}")
private String accessKeyId;
@Value("${aws.s3.secretAccessKey}")
private String secretAccessKey;

@Bean
public S3Client s3Client() {
try {
log.info("Trying to S3Client create.");
return S3Client.builder()
.region(Region.of(awsRegion))
.credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(accessKeyId, secretAccessKey)))
.overrideConfiguration(ClientOverrideConfiguration.builder()
.retryPolicy(r -> r.numRetries(3))
.build())
.build();
} catch (Exception e) {
log.error("Failed to create S3Client.", e);
throw e;
} finally {
log.info("S3Client created successfully.");
}
}
}
88 changes: 88 additions & 0 deletions src/main/java/com/app/controller/S3Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
}
25 changes: 25 additions & 0 deletions src/main/java/com/app/service/S3Service.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.app.service;

import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

public interface S3Service {

boolean uploadFile(MultipartFile file, boolean isReadPublicly);

InputStream downloadFileAsStream(String key);

String createBucket(String bucketName);

List<String> getBucketList() throws RuntimeException;

Map<String, String> listBucketsWithRegions();

void streamAllFilesAsZip(String bucketName, ZipOutputStream zos);

void moveFiles(String sourceBucketName, String destinationBucketName);
}
Loading