---
description: Configure the AWS SDK for Kotlin to work with Cloudflare R2 object storage.
title: aws-sdk-kotlin
image: https://developers.cloudflare.com/og-docs.png
---

[Skip to content](#main-content)

> Documentation Index  
> Fetch the complete documentation index at: https://developers.cloudflare.com/r2/llms.txt  
> Use this file to discover all available pages before exploring further.

# aws-sdk-kotlin

Last updated Aug 24, 2026|Copy as Markdown|[View as Markdown](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/r2/examples/aws/aws-sdk-kotlin/index.md)|[Agent setup](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/agent-setup/)

You must [generate an Access Key](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/r2/api/tokens/) before getting started. All examples will utilize `access_key_id` and `access_key_secret` variables which represent the **Access Key ID** and **Secret Access Key** values you generated.

  
This example uses the [aws-sdk-kotlin ↗](https://github.com/aws/aws-sdk-kotlin). You must pass in the R2 configuration credentials when instantiating your `S3` client:

## Basic Usage

```kotlin
import aws.sdk.kotlin.runtime.auth.credentials.StaticCredentialsProvider
import aws.sdk.kotlin.services.s3.S3Client
import aws.sdk.kotlin.services.s3.listObjects
import aws.sdk.kotlin.services.s3.model.GetObjectRequest
import aws.sdk.kotlin.services.s3.model.PutObjectRequest
import aws.sdk.kotlin.services.s3.presigners.presignGetObject
import aws.sdk.kotlin.services.s3.presigners.presignPutObject
import aws.smithy.kotlin.runtime.auth.awscredentials.Credentials
import aws.smithy.kotlin.runtime.net.url.Url
import kotlinx.coroutines.runBlocking
import kotlin.time.Duration.Companion.minutes

val ACCOUNT_ID = "<ACCOUNT_ID>"
val ACCESS_KEY = "<ACCESS_KEY>"
val SECRET_KEY = "<SECRET_KEY>"

fun main() = runBlocking {
    val r2Client = S3Client.fromEnvironment {
        region = "auto" // Required by SDK, but not used by R2
        endpointUrl = Url.parse("https://${ACCOUNT_ID}.r2.cloudflarestorage.com")
        credentialsProvider = StaticCredentialsProvider(
            Credentials(
                accessKeyId = ACCESS_KEY,
                secretAccessKey = SECRET_KEY
            ),
        )
    }

    println("Available buckets:")
    r2Client.listBuckets().buckets?.forEach { bucket ->
        println("* ${bucket.name}")
    }

    val bucketName = "<BUCKET_NAME>"
    println("\nObjects in bucket '${bucketName}':")
    r2Client.listObjects { bucket = bucketName }.contents?.forEach {
        println("* ${it.key} (size: ${it.size} bytes, modified: ${it.lastModified})")
    }

    r2Client.close()
}
```

```sh
Available buckets:
* my-bucket-1
* my-bucket-2

Objects in bucket 'my-bucket-1':
* image1.png (size: 253167 bytes, modified: 2026-01-17T11:30:58.896Z)
* image2.png (size: 247027 bytes, modified: 2026-01-17T11:30:57.779Z)
```

## Generate presigned URLs

You can also generate presigned links that can be used to temporarily share public read or write access to a bucket.

```kotlin
val uploadUrl = r2Client.presignPutObject(
    input = PutObjectRequest {
        bucket = bucketName
        key = "README.md"
    },
    duration = 15.minutes,
).url
println(uploadUrl)

val getUrl = r2Client.presignGetObject(
    input = GetObjectRequest {
        bucket = bucketName
        key = "README.md"
    },
    duration = 15.minutes,
).url
println(getUrl)
```

You can use these presigned URLs with any HTTP client. For example, to upload a file using the PUT URL:

```bash
curl -X PUT "https://<your-presigned-put-url>" -H "Content-Type: application/octet-stream" --data-binary "@local-file.txt"
```

To download a file using the GET URL:

```bash
curl -X GET "https://<your-presigned-get-url>" -o downloaded-file.txt
```

Was this helpful?

YesNo

## On this page

[![](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/_astro/logo.te5VL_aD.svg)Docs](https://docs-durable-objects-instance-replaced-errors.previews.developers.cloudflare.com/)

```json
{"@context":"https://schema.org","@type":"TechArticle","@id":"https://developers.cloudflare.com/r2/examples/aws/aws-sdk-kotlin/#page","headline":"aws-sdk-kotlin · Cloudflare R2 docs","description":"Configure the AWS SDK for Kotlin to work with Cloudflare R2 object storage.","url":"https://developers.cloudflare.com/r2/examples/aws/aws-sdk-kotlin/","inLanguage":"en","image":"https://developers.cloudflare.com/og-docs.png","dateModified":"2026-08-24","publisher":{"@type":"Organization","name":"Cloudflare","description":"One platform for your apps, agents, and workforce. Build, secure, and scale without managing infrastructure","url":"https://www.cloudflare.com/","sameAs":["https://github.com/cloudflare","https://www.linkedin.com/company/cloudflare","https://x.com/cloudflare"],"logo":{"@type":"ImageObject","url":"https://developers.cloudflare.com/logo.svg"},"address":{"@type":"PostalAddress","streetAddress":"101 Townsend St","addressLocality":"San Francisco","addressRegion":"CA","postalCode":"94107","addressCountry":"US"},"contactPoint":[{"@type":"ContactPoint","contactType":"Customer Support","url":"https://support.cloudflare.com/","availableLanguage":["English"]},{"@type":"ContactPoint","contactType":"Sales","url":"https://www.cloudflare.com/contact/","availableLanguage":["English"]}]},"isPartOf":{"@type":"WebSite","@id":"https://developers.cloudflare.com/#website","name":"Cloudflare Docs","url":"https://developers.cloudflare.com/"}}
```
