
driven-adapter.s3.s3-operations.mustache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of scaffold-clean-architecture Show documentation
Show all versions of scaffold-clean-architecture Show documentation
Gradle plugin to create a clean application in Java that already works, It follows our best practices!
package {{package}}.s3.operations;
import org.springframework.stereotype.Component;
{{#lombok}}
import lombok.AllArgsConstructor;
{{/lombok}}
import software.amazon.awssdk.core.sync.RequestBody;
import software.amazon.awssdk.core.sync.ResponseTransformer;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.*;
import java.io.File;
import java.io.InputStream;
import java.util.List;
@Component
{{#lombok}}
@AllArgsConstructor
{{/lombok}}
public class S3Operations {
private final S3Client s3Client;
{{^lombok}}
public S3Operations(S3Client s3Client ) {
this.s3Client = s3Client;
}
{{/lombok}}
public boolean uploadObject(String bucketName, String objectKey, byte[] fileContent) {
return s3Client.putObject(configurePutObject(bucketName, objectKey),
RequestBody.fromBytes(fileContent)).sdkHttpResponse().isSuccessful();
}
public boolean uploadObject(String bucketName, String objectKey, String fileContent) {
return s3Client.putObject(configurePutObject(bucketName, objectKey),
RequestBody.fromString(fileContent)).sdkHttpResponse().isSuccessful();
}
public boolean uploadObject(String bucketName, String objectKey, File fileContent) {
return s3Client.putObject(configurePutObject(bucketName, objectKey),
RequestBody.fromFile(fileContent)).sdkHttpResponse().isSuccessful();
}
public List listBucketObjects(String bucketName) {
return s3Client.listObjects(ListObjectsRequest
.builder()
.bucket(bucketName)
.build()).contents();
}
public InputStream getObject(String bucketName, String objectKey) {
return s3Client.getObject(GetObjectRequest.builder()
.key(objectKey)
.bucket(bucketName)
.build(), ResponseTransformer.toInputStream());
}
public boolean deleteObject(String bucketName, String objectKey) {
return s3Client.deleteObject(DeleteObjectRequest.builder()
.key(objectKey)
.bucket(bucketName).build()).sdkHttpResponse().isSuccessful();
}
private PutObjectRequest configurePutObject(String bucketName, String objectKey) {
return PutObjectRequest.builder()
.bucket(bucketName)
.key(objectKey)
.build();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy