-
-
Save kevcodez/26fc25d4aa886c22abe25cdb65e22d0c to your computer and use it in GitHub Desktop.
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(); | |
} | |
} | |
} |
Thanks a lot. Let me try it and back to you soon. Thank you
Brother I am getting this exception: " The URI scheme of endpointOverride must not be null.". Is there any solution or suggestion for it?
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.
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.
@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.