import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.*;
import software.amazon.awssdk.services.s3.S3Configuration;
* Client for interacting with Cloudflare R2 Storage using AWS SDK S3 compatibility
public class CloudflareR2Client {
private final S3Client s3Client;
* Creates a new CloudflareR2Client with the provided configuration
public CloudflareR2Client(S3Config config) {
this.s3Client = buildS3Client(config);
* Configuration class for R2 credentials and endpoint
public static class S3Config {
private final String accountId;
private final String accessKey;
private final String secretKey;
private final String endpoint;
public S3Config(String accountId, String accessKey, String secretKey) {
this.accountId = accountId;
this.accessKey = accessKey;
this.secretKey = secretKey;
this.endpoint = String.format("https://%s.r2.cloudflarestorage.com", accountId);
public String getAccessKey() { return accessKey; }
public String getSecretKey() { return secretKey; }
public String getEndpoint() { return endpoint; }
* Builds and configures the S3 client with R2-specific settings
private static S3Client buildS3Client(S3Config config) {
AwsBasicCredentials credentials = AwsBasicCredentials.create(
S3Configuration serviceConfiguration = S3Configuration.builder()
.pathStyleAccessEnabled(true)
return S3Client.builder()
.endpointOverride(URI.create(config.getEndpoint()))
.credentialsProvider(StaticCredentialsProvider.create(credentials))
.region(Region.of("auto"))
.serviceConfiguration(serviceConfiguration)
* Lists all buckets in the R2 storage
public List<Bucket> listBuckets() {
return s3Client.listBuckets().buckets();
} catch (S3Exception e) {
throw new RuntimeException("Failed to list buckets: " + e.getMessage(), e);
* Lists all objects in the specified bucket
public List<S3Object> listObjects(String bucketName) {
ListObjectsV2Request request = ListObjectsV2Request.builder()
return s3Client.listObjectsV2(request).contents();
} catch (S3Exception e) {
throw new RuntimeException("Failed to list objects in bucket " + bucketName + ": " + e.getMessage(), e);
public static void main(String[] args) {
S3Config config = new S3Config(
CloudflareR2Client r2Client = new CloudflareR2Client(config);
System.out.println("Available buckets:");
r2Client.listBuckets().forEach(bucket ->
System.out.println("* " + bucket.name())
// List objects in a specific bucket
String bucketName = "demos";
System.out.println("\nObjects in bucket '" + bucketName + "':");
r2Client.listObjects(bucketName).forEach(object ->
System.out.printf("* %s (size: %d bytes, modified: %s)%n",