Done ticket GEPAFINBE-8

This commit is contained in:
harish
2024-08-21 20:55:13 +05:30
parent fa714faef9
commit e7466d16ec
45 changed files with 1642 additions and 0 deletions

View File

@@ -0,0 +1,91 @@
package net.gepafin.tendermanagement.service.impl;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.*;
import net.gepafin.tendermanagement.service.AmazonS3Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
@Service
public class AmazonS3ServiceImpl implements AmazonS3Service {
@Autowired
private AmazonS3 amazonS3;
@Autowired
private Environment environment;
@Value("${aws.s3.bucket.name}")
private String bucketName;
@Value("${aws.s3.url.folder}")
private String s3Folder;
@Value("${aws.s3.url}")
private String s3Url;
@Override
public String upload(String fileName,
MultipartFile file) throws IOException {
String path = bucketName+"/"+s3Folder;
InputStream inputStream = file.getInputStream();
ObjectMetadata objectMetadata = new ObjectMetadata();
Map<String, String> metadata = new HashMap<>();
metadata.put("Content-Type", file.getContentType());
metadata.put("Content-Length", String.valueOf(file.getSize()));
Optional<Map<String, String>> optionalMetaData = Optional.of(metadata);
optionalMetaData.ifPresent(map -> {
if (!map.isEmpty()) {
map.forEach(objectMetadata::addUserMetadata);
}
});
if(Boolean.FALSE.equals(isTestProfileActivated())) {
amazonS3.putObject(path, fileName, inputStream, objectMetadata);
}
return s3Url + s3Folder +"/"+ fileName;
}
@Override
public Boolean delete(String bucketName,String fileName) {
final DeleteObjectRequest deleteObjectRequest = new DeleteObjectRequest(bucketName, fileName);
if(Boolean.FALSE.equals(isTestProfileActivated())) {
amazonS3.deleteObject(deleteObjectRequest);
}
return true;
}
public Boolean isTestProfileActivated() {
String[] activeProfiles = environment.getActiveProfiles();
return Arrays.stream(activeProfiles).anyMatch("test"::equals);
}
@Override
public InputStream getFile(String filePath) throws IOException {
try {
String path = bucketName+ s3Folder +"/";
GetObjectRequest getObjectRequest = new GetObjectRequest(path, filePath);
S3Object s3Object = amazonS3.getObject(getObjectRequest);
return s3Object.getObjectContent();
} catch (AmazonS3Exception e) {
throw new IOException("Error getting file from Amazon S3", e);
}
}
}