Connect to S4 with the AWS SDK for Java
Add the aws-for-java framework
Prerequisite: install the SDK (https://github.com/aws/aws-sdk-java-v2 )
implementation 'software.amazon.awssdk:s3:2.20.29'Connect to the S4 endpoint
fun getS3Client() = S3Client.builder().region(Region.CN_NORTH_1).endpointOverride(URI.create("https://s3.bitiful.net/"))// Use the value shown under the "Endpoint" label at the bottom of the "Bucket settings" page in the console
.credentialsProvider(AwsCredentialsProvider {
return@AwsCredentialsProvider object :AwsCredentials{
override fun accessKeyId(): String {
return accessKey // Create a sub-account in the console, then create an accessKey for it
}
override fun secretAccessKey(): String {
return sercetKey // Important: when you create a sub-account, you must manually grant it specific permissions!
}
}
}).build()List objects in a bucket
val s3 = getS3Client()
s3.listObjects(ListObjectsRequest.builder().bucket(bucketName))Generate a time-limited presigned download URL
val presign = S3Presigner.builder().region(Region.CN_NORTH_1).endpointOverride(URI.create("https://s3.bitiful.net/"))// Use the value shown under the "Endpoint" label at the bottom of the "Bucket settings" page in the console
.credentialsProvider(AwsCredentialsProvider {
return@AwsCredentialsProvider object :AwsCredentials{
override fun accessKeyId(): String {
return accessKey // Create a sub-account in the console, then create an accessKey for it
}
override fun secretAccessKey(): String {
return sercetKey // Important: when you create a sub-account, you must manually grant it specific permissions!
}
}
}).build()
val request = GetObjectRequest.builder().bucket(bucketName).key(key).build()
val presignRequest = GetObjectPresignRequest.builder()
.signatureDuration(Duration.ofSeconds(time))
.getObjectRequest(request).build()
val url = presign.presignGetObject(presignRequest).url().toString()