Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@kevcodez
Last active April 29, 2023 22:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevcodez/26fc25d4aa886c22abe25cdb65e22d0c to your computer and use it in GitHub Desktop.
Save kevcodez/26fc25d4aa886c22abe25cdb65e22d0c to your computer and use it in GitHub Desktop.
Uploading files to Wasabi Cloud Storage using AWS Java SDK
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.client.builder.AwsClientBuilder;
import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.AmazonS3ClientBuilder;
import com.amazonaws.services.s3.model.CannedAccessControlList;
import com.amazonaws.services.s3.model.ObjectMetadata;
import com.amazonaws.services.s3.model.PutObjectRequest;
public class WasabiFileUploader {
private static final String SERVICE_ENDPOINT = "s3.wasabisys.com";
private static final String REGION = "us-east-1";
private static final String ACCESS_KEY = "xxx";
private static final String SECRET_KEY = "yyy";
private static final String BUCKET_NAME = "bucket";
private static final AmazonS3 AMAZON_S3_CLIENT = AmazonS3ClientBuilder.standard()
.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(SERVICE_ENDPOINT, REGION))
.withCredentials(new AWSStaticCredentialsProvider(new BasicAWSCredentials(ACCESS_KEY, SECRET_KEY)))
.build();
public String uploadFile(byte[] data) throws IOException {
try (InputStream inputStream = new ByteArrayInputStream(data)) {
String filename = "filename";
ObjectMetadata metadata = new ObjectMetadata();
metadata.setContentLength(data.length);
PutObjectRequest putObjectRequest = new PutObjectRequest(BUCKET_NAME, filename, inputStream, metadata)
.withCannedAcl(CannedAccessControlList.PublicRead);
AMAZON_S3_CLIENT.putObject(putObjectRequest);
return AMAZON_S3_CLIENT.getUrl(BUCKET_NAME, filename).toString();
}
}
}
@hasibul-hasan-shuvo
Copy link

Getting this exception java.lang.ClassNotFoundException: Didn't find class "javax.management.MBeanServerFactory" . Can you please tell me how to solve it?
Thank you.

@kevcodez
Copy link
Author

@Shuvo1260 There is no usage of MBeanServerFactory in the snippet and I don't expect the underlying S3 client to have such dependencies. Do you get the same exception without the code above?

Which dependencies are you using?

@hasibul-hasan-shuvo
Copy link

I am using
implementation 'com.amazonaws:aws-java-sdk-s3:1.11.870'
May I know your dependency?

@kevcodez
Copy link
Author

kevcodez commented Sep 28, 2020

@Shuvo1260 Best use the newer v2 SDK.

implementation 'software.amazon.awssdk:s3:2.14.26'

Here's some sample Kotlin Code, should be fairly easy to convert to Java.

import software.amazon.awssdk.auth.credentials.AwsBasicCredentials
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider
import software.amazon.awssdk.core.sync.RequestBody
import software.amazon.awssdk.regions.Region
import software.amazon.awssdk.services.s3.S3Client
import software.amazon.awssdk.services.s3.model.GetObjectRequest
import software.amazon.awssdk.services.s3.model.GetUrlRequest
import software.amazon.awssdk.services.s3.model.ObjectCannedACL
import software.amazon.awssdk.services.s3.model.PutObjectRequest
import java.io.ByteArrayInputStream
import java.net.URI

class WasabiFileUploader {

    fun uploadFile(data: ByteArray): String {
        ByteArrayInputStream(data).use { inputStream ->
            val filename = "filename"
            val putObjectRequest: PutObjectRequest = PutObjectRequest.builder()
                .bucket(BUCKET_NAME)
                .key(filename)
                .metadata(emptyMap())
                .acl(ObjectCannedACL.PUBLIC_READ)
                .build()

            AMAZON_S3_CLIENT.putObject(putObjectRequest, RequestBody.fromInputStream(inputStream, data.size.toLong()))

            return AMAZON_S3_CLIENT.utilities().getUrl(GetUrlRequest.builder().bucket(BUCKET_NAME).key(filename).build()).toExternalForm()
        }
    }

    companion object {
        private val REGION: Region = Region.US_EAST_1
        private const val SERVICE_ENDPOINT = "s3.wasabisys.com"
        private const val ACCESS_KEY = "xxx"
        private const val SECRET_KEY = "yyy"
        private const val BUCKET_NAME = "bucket"
        private val AMAZON_S3_CLIENT: S3Client = S3Client.builder()
            .endpointOverride(URI(SERVICE_ENDPOINT))
            .region(REGION)
            .credentialsProvider(StaticCredentialsProvider.create(AwsBasicCredentials.create(ACCESS_KEY, SECRET_KEY)))
            .build()
    }
}

@hasibul-hasan-shuvo
Copy link

Thanks a lot. Let me try it and back to you soon. Thank you

@hasibul-hasan-shuvo
Copy link

Brother I am getting this exception: " The URI scheme of endpointOverride must not be null.". Is there any solution or suggestion for it?

@kevcodez
Copy link
Author

You could try "https://s3.wasabisys.com" rather than "s3.wasabisys.com" as the service endpoint. When constructing an URI, you need to define the protocol aswell.

@hasibul-hasan-shuvo
Copy link

Thank for your response. But now getting this error
java.lang.VerifyError: Verifier rejected class software.amazon.awssdk.http.apache.ApacheHttpClient: void software.amazon.awssdk.http.apache.ApacheHttpClient.lambda$LRMaF_8eLuzzpRjS-Ew4gJswCBw(org.apache.http.client.methods.HttpRequestBase) failed to verify: void software.amazon.awssdk.http.apache.ApacheHttpClient.lambda$LRMaF_8eLuzzpRjS-Ew4gJswCBw(org.apache.http.client.methods.HttpRequestBase)

Can you tell me what is the reason of this error? I searched for the error but didn't get any idea. It will be a great pleasure for me if you tell me what is the reason.

@Abed-rahim
Copy link

Thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment