
Developer-preview OSS SDK enabling object and bucket management, uploads/downloads, paginators and presigner support, ByteStream I/O and pluggable credentials for streamlined cloud storage operations.
alibabacloud-oss-kotlin-sdk-v2 is the Developer Preview for the v2 of the OSS SDK for the Kotlin programming language
- This Kotlin SDK is based on the official APIs of Alibaba Cloud OSS.
- Alibaba Cloud Object Storage Service (OSS) is a cloud storage service provided by Alibaba Cloud, featuring massive capacity, security, a low cost, and high reliability.
- The OSS can store any type of files and therefore applies to various websites, development enterprises and developers.
- With this SDK, you can upload, download and manage data on any app anytime and anywhere conveniently.
Kotlin 2.1.0or aboveAdd the following dependencies to your project
implementation("com.aliyun:kotlin-oss-v2:<latest-version>")
// implementation("com.aliyun:kotlin-oss-v2-extension:<latest-version>")Note:
kotlin-oss-v2provides bucket basic api, all object api, and high-level api (such as paginator, presinger).kotlin-oss-v2-extensionprovides other bucket control apis, such as BucketCors.
You can run the gradle command for installing after cloning the project source code:
# Clone the project
$ git clone https://github.com/aliyun/alibabacloud-oss-kotlin-sdk-v2.git
# Enter the directory
$ cd alibabacloud-oss-kotlin-sdk-v2/
# Publish To MavenLocal
$ ./gradlew clean publishToMavenLocalAdd mavenLocal to repositories
repositories {
...
mavenLocal()
}Add the following dependencies to your project
implementation("com.aliyun:kotlin-oss-v2:<latest-version>")
// implementation("com.aliyun:kotlin-oss-v2-extension:<latest-version>")You can run the gradle command for packaging after cloning the project source code:
# Clone the project
$ git clone https://github.com/aliyun/alibabacloud-oss-kotlin-sdk-v2.git
# Enter the directory
$ cd aliyun-oss-kotlin-sdk-v2/
# Run the packaging script.
$ ./gradlew :oss-sdk:assemble
# ./gradlew :oss-sdk-extension:assemble
# Take AAR package as an example
# Enter the directory generated after packaging and the package will be generated in this directory
$ cd oss-sdk/build/outputs/aar && ls
# cd oss-sdk-extension/build/outputs/aar && lsimport com.aliyun.kotlin.sdk.service.oss2.ClientConfiguration
import com.aliyun.kotlin.sdk.service.oss2.OSSClient
import com.aliyun.kotlin.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider
import com.aliyun.kotlin.sdk.service.oss2.models.ListBucketsRequest
import com.aliyun.kotlin.sdk.service.oss2.paginator.listBucketsPaginator
suspend fun main() {
val region = "cn-hangzhou"
// Using the SDK's default configuration
// loading credentials values from the environment variables
val config = ClientConfiguration.loadDefault().apply{
this.region = region
credentialsProvider = EnvironmentVariableCredentialsProvider()
}
OSSClient.create(config).use { client ->
// Create the Paginator for the ListBuckets operation.
// Iterate through the bucket pages
client.listBucketsPaginator(ListBucketsRequest {}).collect { it ->
it.buckets?.forEach { bucket ->
println("bucket name: ${bucket.name}")
}
}
}
}import com.aliyun.kotlin.sdk.service.oss2.ClientConfiguration
import com.aliyun.kotlin.sdk.service.oss2.OSSClient
import com.aliyun.kotlin.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider
import com.aliyun.kotlin.sdk.service.oss2.models.PutBucketRequest
suspend fun main() {
val region = "cn-hangzhou"
val bucket = "your bucket name"
// Using the SDK's default configuration
// loading credentials values from the environment variables
val config = ClientConfiguration.loadDefault().apply{
this.region = region
credentialsProvider = EnvironmentVariableCredentialsProvider()
}
OSSClient.create(config).use { client ->
val result = client.putBucket(PutBucketRequest {
this.bucket = bucket
})
println("PutBucket done, StatusCode:${result.statusCode}, RequestId:${result.requestId}.")
}
}import com.aliyun.kotlin.sdk.service.oss2.ClientConfiguration
import com.aliyun.kotlin.sdk.service.oss2.OSSClient
import com.aliyun.kotlin.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider
import com.aliyun.kotlin.sdk.service.oss2.models.DeleteBucketRequest
suspend fun main() {
val region = "cn-hangzhou"
val bucket = "your bucket name"
// Using the SDK's default configuration
// loading credentials values from the environment variables
val config = ClientConfiguration.loadDefault().apply{
this.region = region
credentialsProvider = EnvironmentVariableCredentialsProvider()
}
OSSClient.create(config).use { client ->
val result = client.deleteBucket(DeleteBucketRequest {
this.bucket = bucket
})
println("DeleteBucket done, StatusCode:${result.statusCode}, RequestId:${result.requestId}.")
}
}import com.aliyun.kotlin.sdk.service.oss2.ClientConfiguration
import com.aliyun.kotlin.sdk.service.oss2.OSSClient
import com.aliyun.kotlin.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider
import com.aliyun.kotlin.sdk.service.oss2.models.ListObjectsV2Request
import com.aliyun.kotlin.sdk.service.oss2.paginator.listObjectsV2Paginator
suspend fun main() {
val region = "cn-hangzhou"
val bucket = "your bucket name"
// Using the SDK's default configuration
// loading credentials values from the environment variables
val config = ClientConfiguration.loadDefault().apply{
this.region = region
credentialsProvider = EnvironmentVariableCredentialsProvider()
}
OSSClient.create(config).use { client ->
// Create the Paginator for the ListObjectsV2 operation.
// Lists all objects in a bucket
client.listObjectsV2Paginator(ListObjectsV2Request {
this.bucket = bucket
}).collect { it ->
it.contents?.forEach { obj ->
println("object: ${obj.key}")
}
}
}
}import com.aliyun.kotlin.sdk.service.oss2.ClientConfiguration
import com.aliyun.kotlin.sdk.service.oss2.OSSClient
import com.aliyun.kotlin.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider
import com.aliyun.kotlin.sdk.service.oss2.models.PutObjectRequest
import com.aliyun.kotlin.sdk.service.oss2.types.ByteStream
suspend fun main() {
val region = "cn-hangzhou"
val bucket = "your bucket name"
val key = "your object name"
// Using the SDK's default configuration
// loading credentials values from the environment variables
val config = ClientConfiguration.loadDefault().apply{
this.region = region
credentialsProvider = EnvironmentVariableCredentialsProvider()
}
OSSClient.create(config).use { client ->
val result = client.putObject(PutObjectRequest {
this.bucket = bucket
this.key = key
this.body = ByteStream.fromString("Hello oss.")
})
println("PutObject done, StatusCode:${result.statusCode}, RequestId:${result.requestId}.")
}
}import com.aliyun.kotlin.sdk.service.oss2.ClientConfiguration
import com.aliyun.kotlin.sdk.service.oss2.OSSClient
import com.aliyun.kotlin.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider
import com.aliyun.kotlin.sdk.service.oss2.models.GetObjectRequest
suspend fun main() {
val region = "cn-hangzhou"
val bucket = "your bucket name"
val key = "your object name"
val filePath = "download to file path"
// Using the SDK's default configuration
// loading credentials values from the environment variables
val config = ClientConfiguration.loadDefault().apply{
this.region = region
credentialsProvider = EnvironmentVariableCredentialsProvider()
}
OSSClient.create(config).use { client ->
// Get data of object to local file
val result = client.getObject(GetObjectRequest {
this.bucket = bucket
this.key = key
})
println("GetObject done, StatusCode:${result.statusCode}, RequestId:${result.requestId}.")
}
}import com.aliyun.kotlin.sdk.service.oss2.ClientConfiguration
import com.aliyun.kotlin.sdk.service.oss2.OSSClient
import com.aliyun.kotlin.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider
import com.aliyun.kotlin.sdk.service.oss2.models.GetObjectRequest
import kotlinx.io.files.Path
suspend fun main() {
val region = "cn-hangzhou"
val bucket = "your bucket name"
val key = "your object name"
val filePath = "download to file path"
// Using the SDK's default configuration
// loading credentials values from the environment variables
val config = ClientConfiguration.loadDefault().apply{
this.region = region
credentialsProvider = EnvironmentVariableCredentialsProvider()
}
OSSClient.create(config).use { client ->
// Get data of object to local file
val result = client.getObjectToFile(GetObjectRequest {
this.bucket = bucket
this.key = key
}, Path(filePath))
println("GetObject done, StatusCode:${result.statusCode}, RequestId:${result.requestId}.")
}
}import com.aliyun.kotlin.sdk.service.oss2.ClientConfiguration
import com.aliyun.kotlin.sdk.service.oss2.OSSClient
import com.aliyun.kotlin.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider
import com.aliyun.kotlin.sdk.service.oss2.models.DeleteObjectRequest
suspend fun main() {
val region = "cn-hangzhou"
val bucket = "your bucket name"
val key = "your object name"
// Using the SDK's default configuration
// loading credentials values from the environment variables
val config = ClientConfiguration.loadDefault().apply{
this.region = region
credentialsProvider = EnvironmentVariableCredentialsProvider()
}
OSSClient.create(config).use { client ->
val result = client.deleteObject(DeleteObjectRequest {
this.bucket = bucket
this.key = key
})
println("DeleteObject done, StatusCode:${result.statusCode}, RequestId:${result.requestId}.")
}
}import com.aliyun.kotlin.sdk.service.oss2.ClientConfiguration
import com.aliyun.kotlin.sdk.service.oss2.OSSClient
import com.aliyun.kotlin.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider
import com.aliyun.kotlin.sdk.service.oss2.extension.api.getBucketCors
import com.aliyun.kotlin.sdk.service.oss2.extension.models.GetBucketCorsRequest
suspend fun main() {
val region = "cn-hangzhou"
val bucket = "your bucket name"
// Using the SDK's default configuration
// loading credentials values from the environment variables
val config = ClientConfiguration.loadDefault().apply{
this.region = region
credentialsProvider = EnvironmentVariableCredentialsProvider()
}
OSSClient.create(config).use { client ->
val result = client.getBucketCors(GetBucketCorsRequest {
this.bucket = bucket
})
println("GetBucketCors done, StatusCode:${result.statusCode}, RequestId:${result.requestId}.")
result.corsConfiguration?.corsRules?.forEach {
println("allowedMethods: ${it.allowedMethods?.joinToString(" ")}")
println("allowedHeaders: ${it.allowedHeaders?.joinToString(" ")}")
println("allowedOrigins: ${it.allowedOrigins?.joinToString(" ")}")
println("exposeHeaders: ${it.exposeHeaders?.joinToString(" ")}")
println("maxAgeSeconds: ${it.maxAgeSeconds}")
}
}
}More example projects can be found in the Sample folder
# build project
./gradlew :sample:cli:build
# Go to the sample code folder
cd sample/cli/build/libs/
# Configure credentials values from the environment variables
export OSS_ACCESS_KEY_ID="your access key id"
export OSS_ACCESS_KEY_SECRET="your access key secrect"
# Take ListBuckets as an example
java -jar cli-jvm.jar ListBuckets --region cn-hangzhou
- Run
sample.composeApporsample[jvm]- Set
AccessKeyId,AccessKeySecretandRegion- Client on
Set Client, complete client initialization- Taking ListObjects as an example, fill in the
Bucket nameand click onListObjects
- Apache-2.0, see license file
alibabacloud-oss-kotlin-sdk-v2 is the Developer Preview for the v2 of the OSS SDK for the Kotlin programming language
- This Kotlin SDK is based on the official APIs of Alibaba Cloud OSS.
- Alibaba Cloud Object Storage Service (OSS) is a cloud storage service provided by Alibaba Cloud, featuring massive capacity, security, a low cost, and high reliability.
- The OSS can store any type of files and therefore applies to various websites, development enterprises and developers.
- With this SDK, you can upload, download and manage data on any app anytime and anywhere conveniently.
Kotlin 2.1.0or aboveAdd the following dependencies to your project
implementation("com.aliyun:kotlin-oss-v2:<latest-version>")
// implementation("com.aliyun:kotlin-oss-v2-extension:<latest-version>")Note:
kotlin-oss-v2provides bucket basic api, all object api, and high-level api (such as paginator, presinger).kotlin-oss-v2-extensionprovides other bucket control apis, such as BucketCors.
You can run the gradle command for installing after cloning the project source code:
# Clone the project
$ git clone https://github.com/aliyun/alibabacloud-oss-kotlin-sdk-v2.git
# Enter the directory
$ cd alibabacloud-oss-kotlin-sdk-v2/
# Publish To MavenLocal
$ ./gradlew clean publishToMavenLocalAdd mavenLocal to repositories
repositories {
...
mavenLocal()
}Add the following dependencies to your project
implementation("com.aliyun:kotlin-oss-v2:<latest-version>")
// implementation("com.aliyun:kotlin-oss-v2-extension:<latest-version>")You can run the gradle command for packaging after cloning the project source code:
# Clone the project
$ git clone https://github.com/aliyun/alibabacloud-oss-kotlin-sdk-v2.git
# Enter the directory
$ cd aliyun-oss-kotlin-sdk-v2/
# Run the packaging script.
$ ./gradlew :oss-sdk:assemble
# ./gradlew :oss-sdk-extension:assemble
# Take AAR package as an example
# Enter the directory generated after packaging and the package will be generated in this directory
$ cd oss-sdk/build/outputs/aar && ls
# cd oss-sdk-extension/build/outputs/aar && lsimport com.aliyun.kotlin.sdk.service.oss2.ClientConfiguration
import com.aliyun.kotlin.sdk.service.oss2.OSSClient
import com.aliyun.kotlin.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider
import com.aliyun.kotlin.sdk.service.oss2.models.ListBucketsRequest
import com.aliyun.kotlin.sdk.service.oss2.paginator.listBucketsPaginator
suspend fun main() {
val region = "cn-hangzhou"
// Using the SDK's default configuration
// loading credentials values from the environment variables
val config = ClientConfiguration.loadDefault().apply{
this.region = region
credentialsProvider = EnvironmentVariableCredentialsProvider()
}
OSSClient.create(config).use { client ->
// Create the Paginator for the ListBuckets operation.
// Iterate through the bucket pages
client.listBucketsPaginator(ListBucketsRequest {}).collect { it ->
it.buckets?.forEach { bucket ->
println("bucket name: ${bucket.name}")
}
}
}
}import com.aliyun.kotlin.sdk.service.oss2.ClientConfiguration
import com.aliyun.kotlin.sdk.service.oss2.OSSClient
import com.aliyun.kotlin.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider
import com.aliyun.kotlin.sdk.service.oss2.models.PutBucketRequest
suspend fun main() {
val region = "cn-hangzhou"
val bucket = "your bucket name"
// Using the SDK's default configuration
// loading credentials values from the environment variables
val config = ClientConfiguration.loadDefault().apply{
this.region = region
credentialsProvider = EnvironmentVariableCredentialsProvider()
}
OSSClient.create(config).use { client ->
val result = client.putBucket(PutBucketRequest {
this.bucket = bucket
})
println("PutBucket done, StatusCode:${result.statusCode}, RequestId:${result.requestId}.")
}
}import com.aliyun.kotlin.sdk.service.oss2.ClientConfiguration
import com.aliyun.kotlin.sdk.service.oss2.OSSClient
import com.aliyun.kotlin.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider
import com.aliyun.kotlin.sdk.service.oss2.models.DeleteBucketRequest
suspend fun main() {
val region = "cn-hangzhou"
val bucket = "your bucket name"
// Using the SDK's default configuration
// loading credentials values from the environment variables
val config = ClientConfiguration.loadDefault().apply{
this.region = region
credentialsProvider = EnvironmentVariableCredentialsProvider()
}
OSSClient.create(config).use { client ->
val result = client.deleteBucket(DeleteBucketRequest {
this.bucket = bucket
})
println("DeleteBucket done, StatusCode:${result.statusCode}, RequestId:${result.requestId}.")
}
}import com.aliyun.kotlin.sdk.service.oss2.ClientConfiguration
import com.aliyun.kotlin.sdk.service.oss2.OSSClient
import com.aliyun.kotlin.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider
import com.aliyun.kotlin.sdk.service.oss2.models.ListObjectsV2Request
import com.aliyun.kotlin.sdk.service.oss2.paginator.listObjectsV2Paginator
suspend fun main() {
val region = "cn-hangzhou"
val bucket = "your bucket name"
// Using the SDK's default configuration
// loading credentials values from the environment variables
val config = ClientConfiguration.loadDefault().apply{
this.region = region
credentialsProvider = EnvironmentVariableCredentialsProvider()
}
OSSClient.create(config).use { client ->
// Create the Paginator for the ListObjectsV2 operation.
// Lists all objects in a bucket
client.listObjectsV2Paginator(ListObjectsV2Request {
this.bucket = bucket
}).collect { it ->
it.contents?.forEach { obj ->
println("object: ${obj.key}")
}
}
}
}import com.aliyun.kotlin.sdk.service.oss2.ClientConfiguration
import com.aliyun.kotlin.sdk.service.oss2.OSSClient
import com.aliyun.kotlin.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider
import com.aliyun.kotlin.sdk.service.oss2.models.PutObjectRequest
import com.aliyun.kotlin.sdk.service.oss2.types.ByteStream
suspend fun main() {
val region = "cn-hangzhou"
val bucket = "your bucket name"
val key = "your object name"
// Using the SDK's default configuration
// loading credentials values from the environment variables
val config = ClientConfiguration.loadDefault().apply{
this.region = region
credentialsProvider = EnvironmentVariableCredentialsProvider()
}
OSSClient.create(config).use { client ->
val result = client.putObject(PutObjectRequest {
this.bucket = bucket
this.key = key
this.body = ByteStream.fromString("Hello oss.")
})
println("PutObject done, StatusCode:${result.statusCode}, RequestId:${result.requestId}.")
}
}import com.aliyun.kotlin.sdk.service.oss2.ClientConfiguration
import com.aliyun.kotlin.sdk.service.oss2.OSSClient
import com.aliyun.kotlin.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider
import com.aliyun.kotlin.sdk.service.oss2.models.GetObjectRequest
suspend fun main() {
val region = "cn-hangzhou"
val bucket = "your bucket name"
val key = "your object name"
val filePath = "download to file path"
// Using the SDK's default configuration
// loading credentials values from the environment variables
val config = ClientConfiguration.loadDefault().apply{
this.region = region
credentialsProvider = EnvironmentVariableCredentialsProvider()
}
OSSClient.create(config).use { client ->
// Get data of object to local file
val result = client.getObject(GetObjectRequest {
this.bucket = bucket
this.key = key
})
println("GetObject done, StatusCode:${result.statusCode}, RequestId:${result.requestId}.")
}
}import com.aliyun.kotlin.sdk.service.oss2.ClientConfiguration
import com.aliyun.kotlin.sdk.service.oss2.OSSClient
import com.aliyun.kotlin.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider
import com.aliyun.kotlin.sdk.service.oss2.models.GetObjectRequest
import kotlinx.io.files.Path
suspend fun main() {
val region = "cn-hangzhou"
val bucket = "your bucket name"
val key = "your object name"
val filePath = "download to file path"
// Using the SDK's default configuration
// loading credentials values from the environment variables
val config = ClientConfiguration.loadDefault().apply{
this.region = region
credentialsProvider = EnvironmentVariableCredentialsProvider()
}
OSSClient.create(config).use { client ->
// Get data of object to local file
val result = client.getObjectToFile(GetObjectRequest {
this.bucket = bucket
this.key = key
}, Path(filePath))
println("GetObject done, StatusCode:${result.statusCode}, RequestId:${result.requestId}.")
}
}import com.aliyun.kotlin.sdk.service.oss2.ClientConfiguration
import com.aliyun.kotlin.sdk.service.oss2.OSSClient
import com.aliyun.kotlin.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider
import com.aliyun.kotlin.sdk.service.oss2.models.DeleteObjectRequest
suspend fun main() {
val region = "cn-hangzhou"
val bucket = "your bucket name"
val key = "your object name"
// Using the SDK's default configuration
// loading credentials values from the environment variables
val config = ClientConfiguration.loadDefault().apply{
this.region = region
credentialsProvider = EnvironmentVariableCredentialsProvider()
}
OSSClient.create(config).use { client ->
val result = client.deleteObject(DeleteObjectRequest {
this.bucket = bucket
this.key = key
})
println("DeleteObject done, StatusCode:${result.statusCode}, RequestId:${result.requestId}.")
}
}import com.aliyun.kotlin.sdk.service.oss2.ClientConfiguration
import com.aliyun.kotlin.sdk.service.oss2.OSSClient
import com.aliyun.kotlin.sdk.service.oss2.credentials.EnvironmentVariableCredentialsProvider
import com.aliyun.kotlin.sdk.service.oss2.extension.api.getBucketCors
import com.aliyun.kotlin.sdk.service.oss2.extension.models.GetBucketCorsRequest
suspend fun main() {
val region = "cn-hangzhou"
val bucket = "your bucket name"
// Using the SDK's default configuration
// loading credentials values from the environment variables
val config = ClientConfiguration.loadDefault().apply{
this.region = region
credentialsProvider = EnvironmentVariableCredentialsProvider()
}
OSSClient.create(config).use { client ->
val result = client.getBucketCors(GetBucketCorsRequest {
this.bucket = bucket
})
println("GetBucketCors done, StatusCode:${result.statusCode}, RequestId:${result.requestId}.")
result.corsConfiguration?.corsRules?.forEach {
println("allowedMethods: ${it.allowedMethods?.joinToString(" ")}")
println("allowedHeaders: ${it.allowedHeaders?.joinToString(" ")}")
println("allowedOrigins: ${it.allowedOrigins?.joinToString(" ")}")
println("exposeHeaders: ${it.exposeHeaders?.joinToString(" ")}")
println("maxAgeSeconds: ${it.maxAgeSeconds}")
}
}
}More example projects can be found in the Sample folder
# build project
./gradlew :sample:cli:build
# Go to the sample code folder
cd sample/cli/build/libs/
# Configure credentials values from the environment variables
export OSS_ACCESS_KEY_ID="your access key id"
export OSS_ACCESS_KEY_SECRET="your access key secrect"
# Take ListBuckets as an example
java -jar cli-jvm.jar ListBuckets --region cn-hangzhou
- Run
sample.composeApporsample[jvm]- Set
AccessKeyId,AccessKeySecretandRegion- Client on
Set Client, complete client initialization- Taking ListObjects as an example, fill in the
Bucket nameand click onListObjects
- Apache-2.0, see license file