This worked for me
public String uploadBase64ToStorage(String filePath,String location,String fileName) {
String key = Config.getProperty("spaces", "key");
String secret = Config.getProperty("spaces", "secret");
String bucket = Config.getProperty("spaces", "bucket");
String endpoint = Config.getProperty("spaces", "endpoint");
String region = Config.getProperty("spaces", "region");
AWSCredentialsProvider awscp = new AWSStaticCredentialsProvider(
new BasicAWSCredentials(key, secret)
);
AmazonS3 space = AmazonS3ClientBuilder
.standard()
.withCredentials(awscp)
.withEndpointConfiguration(
new AwsClientBuilder.EndpointConfiguration(endpoint, region)
)
.build();
File file = new File(filePath);
String filepath = location+"/"+fileName; // /somefolder/someanotherfolder/testfile.jpg";
ObjectMetadata om = new ObjectMetadata();
om.setContentLength(file.length());
om.setContentType("image/jpg");
PutObjectRequest putObjectRequest = new PutObjectRequest(bucket,filepath,file).withCannedAcl(CannedAccessControlList.PublicRead);
space.putObject(putObjectRequest);
return space.getUrl(bucket, filepath).toString();
}```