Untitled
unknown
plain_text
a year ago
1.5 kB
6
Indexable
public interface S3BucketStorageService {
String uploadImageToS3(String imageFile);
}
// impl
@Service
@RequiredArgsConstructor
public class S3BucketStorageServiceImpl implements S3BucketStorageService {
@Value("${cloud.aws.endpoint-url}")
private String endpointUrl;
@Value("${cloud.aws.bucket.name}")
private String bucketName;
@Value("${spring.profiles.active}")
String profileActive;
private final AmazonS3 amazonS3;
@Override
public String uploadImageToS3(String imageFile) {
String fileUrl = "";
File file = new File(imageFile);
if (!file.exists()) {
logger.error("File does not exist: {}", imageFile);
return null;
}
if (!file.canRead()) {
logger.error("File cannot be read: {}", imageFile);
return null;
}
String key = imageFile;
logger.info("key: {}", key);
try {
amazonS3.putObject(new PutObjectRequest(bucketName, key, file)
.withCannedAcl(CannedAccessControlList.BucketOwnerFullControl));
fileUrl = endpointUrl + "/" + bucketName + "/" + key;
logger.info("S3 put done file: {}", imageFile);
} catch (SdkClientException e) {
logger.error("Failed to upload file to S3: {}", e.getMessage());
} finally {
logger.debug("Should delete file here if prod");
}
return fileUrl;
}
}Editor is loading...
Leave a Comment