Fixed s3 document issue
This commit is contained in:
@@ -11,11 +11,10 @@ import java.io.InputStream;
|
||||
@Component
|
||||
public interface AmazonS3Service {
|
||||
|
||||
public String upload(String fileName, String s3Folder, MultipartFile file) throws IOException;
|
||||
|
||||
public Boolean delete(String s3Folder, String fileName);
|
||||
|
||||
UploadFileOnAmazonS3Response uploadFileOnAmazonS3(String s3Folder, MultipartFile file);
|
||||
|
||||
Boolean delete(String s3Folder, String filePath);
|
||||
|
||||
InputStream getFile(String s3Folder, String filePath) throws IOException;
|
||||
|
||||
public UploadFileOnAmazonS3Response uploadFileOnAmazonS3(String s3Folder, MultipartFile file);
|
||||
}
|
||||
@@ -12,6 +12,8 @@ import net.gepafin.tendermanagement.web.rest.api.errors.CustomValidationExceptio
|
||||
import net.gepafin.tendermanagement.web.rest.api.errors.Status;
|
||||
|
||||
import org.apache.commons.io.FilenameUtils;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.core.env.Environment;
|
||||
@@ -28,6 +30,8 @@ import java.util.Optional;
|
||||
|
||||
@Service
|
||||
public class AmazonS3ServiceImpl implements AmazonS3Service {
|
||||
|
||||
private final Logger log = LoggerFactory.getLogger(AmazonS3ServiceImpl.class);
|
||||
|
||||
@Autowired
|
||||
private AmazonS3 amazonS3;
|
||||
@@ -42,11 +46,9 @@ public class AmazonS3ServiceImpl implements AmazonS3Service {
|
||||
private String s3Url;
|
||||
|
||||
|
||||
@Override
|
||||
public String upload(String fileName, String s3Folder,
|
||||
private String upload(String fileName, String s3Folder,
|
||||
MultipartFile file) throws IOException {
|
||||
|
||||
// String path = bucketName+"/"+s3Folder;
|
||||
String path = s3Folder +"/"+fileName;
|
||||
|
||||
InputStream inputStream = file.getInputStream();
|
||||
@@ -66,17 +68,23 @@ public class AmazonS3ServiceImpl implements AmazonS3Service {
|
||||
if(Boolean.FALSE.equals(isTestProfileActivated())) {
|
||||
amazonS3.putObject(bucketName, path, inputStream, objectMetadata);
|
||||
}
|
||||
return s3Url + s3Folder +"/"+ fileName;
|
||||
path =s3Url + s3Folder +"/"+ fileName;
|
||||
log.info("File '{}' uploaded successfully to Amazon S3 with URL: {}", fileName, path);
|
||||
return path;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Boolean delete(String s3Folder, String fileName) {
|
||||
public Boolean delete(String s3Folder, String filePath) {
|
||||
|
||||
String fileName = Utils.extractFileName(filePath);
|
||||
String path = s3Folder +"/"+fileName;
|
||||
final DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucketName, path);
|
||||
if(Boolean.FALSE.equals(isTestProfileActivated())) {
|
||||
amazonS3.deleteObject(deleteObjectRequest);
|
||||
}
|
||||
log.info("File '{}' deleted successfully from Amazon S3", fileName);
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
public Boolean isTestProfileActivated() {
|
||||
@@ -85,28 +93,34 @@ public class AmazonS3ServiceImpl implements AmazonS3Service {
|
||||
}
|
||||
|
||||
@Override
|
||||
public InputStream getFile(String s3Folder, String filePath) throws IOException {
|
||||
public InputStream getFile(String s3Folder, String filePath) {
|
||||
try {
|
||||
String path = s3Folder +"/"+filePath;
|
||||
String fileName = Utils.extractFileName(filePath);
|
||||
String path = s3Folder + "/" + fileName;
|
||||
GetObjectRequest getObjectRequest = new GetObjectRequest(bucketName, path);
|
||||
S3Object s3Object = amazonS3.getObject(getObjectRequest);
|
||||
log.info("File fetched successfully from Amazon S3: {}", fileName);
|
||||
return s3Object.getObjectContent();
|
||||
} catch (AmazonS3Exception e) {
|
||||
throw new IOException("Error getting file from Amazon S3", e);
|
||||
log.error("Error occurred while getting file from Amazon S3: {}", e);
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR,
|
||||
Translator.toLocale(GepafinConstant.GET_ERROR_S3));
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public UploadFileOnAmazonS3Response uploadFileOnAmazonS3(String s3Folder, MultipartFile file) {
|
||||
String extension = FilenameUtils.getExtension(file.getOriginalFilename());
|
||||
String fileName = org.springframework.util.StringUtils.cleanPath(file.getOriginalFilename());
|
||||
String firstNameContain = fileName.substring(0, fileName.lastIndexOf('.'));
|
||||
firstNameContain+=Utils.randomKey(5);
|
||||
fileName = (firstNameContain + "." + extension);
|
||||
String originalFileName = org.springframework.util.StringUtils.cleanPath(file.getOriginalFilename());
|
||||
String firstNameContain = originalFileName.substring(0, originalFileName.lastIndexOf('.'));
|
||||
firstNameContain = Utils.replaceSpacesWithUnderscores(firstNameContain);
|
||||
firstNameContain += "_" + Utils.randomKey(7);
|
||||
String fileName = (firstNameContain + "." + extension);
|
||||
try {
|
||||
String filepath = upload(fileName, s3Folder, file);
|
||||
return UploadFileOnAmazonS3Response.builder().fileName(fileName).filePath(filepath).build();
|
||||
return UploadFileOnAmazonS3Response.builder().fileName(originalFileName).filePath(filepath).build();
|
||||
} catch (Exception e) {
|
||||
log.error("Error occurred while uploading file from Amazon S3: {}", e);
|
||||
throw new CustomValidationException(Status.VALIDATION_ERROR,
|
||||
Translator.toLocale(GepafinConstant.UPLOAD_ERROR_S3));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user