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

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