Fixed s3 download issue

This commit is contained in:
rajesh
2024-12-06 22:17:02 +05:30
parent 126ee64f79
commit 88aed061cc

View File

@@ -30,6 +30,8 @@ import org.springframework.web.multipart.MultipartFile;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.net.URL; import java.net.URL;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
@@ -118,12 +120,15 @@ public class AmazonS3ServiceImpl implements AmazonS3Service {
public InputStream getFile(String s3Folder, String filePath) { public InputStream getFile(String s3Folder, String filePath) {
try { try {
String fileName = Utils.extractFileName(filePath); String fileName = Utils.extractFileName(filePath);
String path = s3Folder + "/" + fileName; // Decode the file name to handle special characters like '+' correctly
String decodedFileName = URLDecoder.decode(fileName, StandardCharsets.UTF_8.toString());
String path = s3Folder + "/" + decodedFileName;
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, path); GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, path);
S3Object s3Object = amazonS3.getObject(getObjectRequest); S3Object s3Object = amazonS3.getObject(getObjectRequest);
log.info("File fetched successfully from Amazon S3: {}", fileName); log.info("File fetched successfully from Amazon S3: {}", fileName);
return s3Object.getObjectContent(); return s3Object.getObjectContent();
} catch (AmazonS3Exception e) { } catch (Exception e) {
log.error("Error occurred while getting file from Amazon S3: {}", e); log.error("Error occurred while getting file from Amazon S3: {}", e);
throw new CustomValidationException(Status.VALIDATION_ERROR, throw new CustomValidationException(Status.VALIDATION_ERROR,
Translator.toLocale(GepafinConstant.GET_ERROR_S3)); Translator.toLocale(GepafinConstant.GET_ERROR_S3));