Skip to content

Implement asynchronous file upload and download using S3AsyncClient #3

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 2 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
46 changes: 46 additions & 0 deletions src/main/java/com/app/config/AwsS3Config.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
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.S3AsyncClient;

@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 S3AsyncClient s3Client() {
try {
log.info("Trying to S3Client create.");

return S3AsyncClient.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 S3AsyncClient.", e);
throw e;
} finally {
log.info("S3AsyncClient created successfully.");
}
}
}
62 changes: 62 additions & 0 deletions src/main/java/com/app/controller/S3Controller.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.app.controller;

import com.app.service.S3Service;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
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.ByteArrayInputStream;
import java.io.InputStream;
import java.util.concurrent.CompletableFuture;

@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 StreamingResponseBody downloadFile(@PathVariable String key, HttpServletResponse httpResponse) {

httpResponse.setContentType("application/octet-stream");
httpResponse.setHeader("Content-Disposition", String.format("inline; filename=\"%s\"", key));

CompletableFuture<ByteArrayInputStream> byteArrayInputStreamCompletableFuture = s3Service.downloadFileAsStream(key);

return outputStream -> {
ByteArrayInputStream byteArrayInputStream = byteArrayInputStreamCompletableFuture.join();
if (byteArrayInputStream != null) {
byte[] buffer = new byte[8192];
int bytesRead;
while ((bytesRead = byteArrayInputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.flush();
} else {
// Handle the case where the stream is null
httpResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
String errorMessage = "Failed to download the key. Please try again later.";
outputStream.write(errorMessage.getBytes());
outputStream.flush();
}
};
}
}
14 changes: 14 additions & 0 deletions src/main/java/com/app/service/S3Service.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.app.service;

import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.util.concurrent.CompletableFuture;

public interface S3Service {


boolean uploadFile(MultipartFile file, boolean isReadPublicly);

CompletableFuture<ByteArrayInputStream> downloadFileAsStream(String key);
}
95 changes: 95 additions & 0 deletions src/main/java/com/app/service/S3ServiceImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package com.app.service;

import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import software.amazon.awssdk.core.async.AsyncRequestBody;
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
import software.amazon.awssdk.services.s3.S3AsyncClient;
import software.amazon.awssdk.services.s3.model.GetObjectRequest;
import software.amazon.awssdk.transfer.s3.S3TransferManager;
import software.amazon.awssdk.transfer.s3.model.CompletedUpload;
import software.amazon.awssdk.transfer.s3.model.Upload;
import software.amazon.awssdk.transfer.s3.model.UploadRequest;
import software.amazon.awssdk.transfer.s3.progress.LoggingTransferListener;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.concurrent.CompletableFuture;

@Service
@Log4j2
@RequiredArgsConstructor
public class S3ServiceImpl implements S3Service {

private final S3AsyncClient s3AsyncClient;

@Value("${aws.s3.bucket-name}")
private String bucketName;


private S3TransferManager createTransferManager() {
return S3TransferManager.builder()
.s3Client(s3AsyncClient)
.build();
}


@Override
public boolean uploadFile(MultipartFile file, boolean isReadPublicly) {
log.info("Started uploading file '{}' to S3 Bucket '{}'", file.getOriginalFilename(), bucketName);
try (S3TransferManager transferManager = createTransferManager()) {
UploadRequest uploadRequest;
if (isReadPublicly) {
uploadRequest = UploadRequest.builder()
.putObjectRequest(builder -> builder.bucket(bucketName).key(file.getOriginalFilename()).acl("public-read"))
.requestBody(AsyncRequestBody.fromBytes(file.getBytes()))
.addTransferListener(LoggingTransferListener.create()) // For logging progress
.build();
} else {
uploadRequest = UploadRequest.builder()
.putObjectRequest(builder -> builder.bucket(bucketName).key(file.getOriginalFilename()))
.requestBody(AsyncRequestBody.fromBytes(file.getBytes()))
.addTransferListener(LoggingTransferListener.create()) // For logging progress
.build();
}
// Start the file upload
Upload upload = transferManager.upload(uploadRequest);

// Wait for the upload to complete
CompletableFuture<CompletedUpload> uploadCompletion = upload.completionFuture();
uploadCompletion.join();
log.info("Successfully uploaded file to S3. Bucket: {}, Key: {}", bucketName, file.getOriginalFilename());
return true;
} catch (Exception e) {
log.error("Failed to upload file to S3. Bucket: {}, Key: {}", bucketName, file.getOriginalFilename(), e);
return false;
}
}

@Override
public CompletableFuture<ByteArrayInputStream> downloadFileAsStream(String key) {
GetObjectRequest getObjectRequest = GetObjectRequest.builder()
.bucket(bucketName)
.key(key)
.build();

// Download the file directly into a ByteArrayOutputStream
return s3AsyncClient.getObject(getObjectRequest, AsyncResponseTransformer.toBytes())
.thenApply(response -> {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
byteArrayOutputStream.write(response.asByteArray());
} catch (Exception e) {
log.error("Failed to write response to ByteArrayOutputStream. Bucket: {}, Key: {}", bucketName, key, e);
}
return new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
})
.exceptionally(e -> {
log.error("Failed to download file from S3. Bucket: {}, Key: {}", bucketName, key, e);
return null;
});
}
}
7 changes: 7 additions & 0 deletions src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@ app:
logs:
path: C:/${spring.application.name}/logs

aws:
s3:
enabled: true
region: us-east-1
accessKeyId: <<access key>>
secretAccessKey: <<secret access key>>
bucket-name: <<enter bucket name>>