Last active
April 29, 2023 22:20
-
-
Save kevcodez/26fc25d4aa886c22abe25cdb65e22d0c to your computer and use it in GitHub Desktop.
Uploading files to Wasabi Cloud Storage using AWS Java SDK
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} | |
} |
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.
Thanks a lot.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.