适用于 Kotlin 并支持协同程序的新 AWS SDK
source link: https://blog.jetbrains.com/zh-hans/kotlin/2022/01/the-new-aws-sdk-for-kotlin-with-coroutines-support/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
适用于 Kotlin 并支持协同程序的新 AWS SDK
Read this post in other languages:
English, 한국어
适用于 Kotlin 的新 AWS SDK 已于 2021 年 12 月在 AWS re:Invent 上宣布。 您可以使用此 SDK 构建与 Amazon S3、Amazon EC2、DynamoDB 等协同工作的 Kotlin 应用程序。 SDK 目前处于预览阶段,预计很快会推出稳定版。
从头开始设计 SDK 是为了支持 Kotlin 语言及其最佳做法,为 Kotlin 开发者提供与 AWS 交互的熟悉和惯用体验。 SDK 利用 Kotlin 协同程序进行异步实现,并计划使其兼容 Kotlin 多平台项目。
要开始在 Kotlin 程序中使用新 SDK,您需要向项目添加相应的依赖项。 例如,如果要与 DynamoDB 通信,则需要 aws.sdk.kotlin:dynamodb
模块:
repositories { mavenCentral() } dependencies { implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0") // The following line adds a dependency on the dynamodb client. // For demonstration purposes, we use 0.+ to get the latest version implementation("aws.sdk.kotlin:dynamodb:0.+") }
xxxxxxxxxx
repositories {
mavenCentral()
}
dependencies {
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0")
// The following line adds a dependency on the dynamodb client.
// For demonstration purposes, we use 0.+ to get the latest version
implementation("aws.sdk.kotlin:dynamodb:0.+")
}
您可以在 AWS SDK 文档中找到受支持模块的完整列表。
下载依赖项后,即可开始使用 API:
import kotlinx.coroutines.runBlocking import aws.sdk.kotlin.services.dynamodb.DynamoDbClient //sampleStart fun main() = runBlocking { val client = DynamoDbClient { region = "us-east-2" } val resp = client.listTables { limit = 10 } println("Current DynamoDB tables: ") resp.tableNames?.forEach { println(it) } client.close() } //sampleEnd
xxxxxxxxxx
fun main() = runBlocking {
val client = DynamoDbClient { region = "us-east-2" }
val resp = client.listTables { limit = 10 }
println("Current DynamoDB tables: ")
resp.tableNames?.forEach { println(it) }
client.close()
}
该程序只会检索表的列表,并将表名打印到标准输出中。 在上面的示例中,listTables
是一个挂起函数,因此代码被包装到 runBlocking
调用中。
我们来看一个稍微复杂一点的示例,演示如何将存储桶中的每个 S3 对象下载到本地临时目录(包含分页访问、并发流和字节流响应)。
import aws.sdk.kotlin.services.s3.S3Client import aws.sdk.kotlin.services.s3.model.GetObjectRequest import aws.sdk.kotlin.services.s3.model.ListObjectsV2Request import aws.sdk.kotlin.services.s3.paginators.listObjectsV2Paginated import aws.smithy.kotlin.runtime.content.writeToFile import kotlinx.coroutines.flow.asFlow import kotlinx.coroutines.flow.filter import kotlinx.coroutines.flow.flatMapConcat import kotlinx.coroutines.flow.flowOf import java.nio.file.Paths import kotlin.io.path.createDirectories //sampleStart suspend fun downloadAllS3Objects(bucketName: String) { val s3 = S3Client.fromEnvironment() val listReq = ListObjectsV2Request { bucket = bucketName } s3.listObjectsV2Paginated(listReq) .flatMapConcat { it.contents?.asFlow() ?: flowOf() } .filter { it.size > 0 } .collect { obj -> val getReq = GetObjectRequest { bucket = bucketName key = obj.key } s3.getObject(getReq) { val path = Paths.get(System.getProperty("java.io.tmpdir"), obj.key) path.parent.createDirectories() it.body?.writeToFile(path) } } } //sampleEnd
xxxxxxxxxx
suspend fun downloadAllS3Objects(bucketName: String) {
val s3 = S3Client.fromEnvironment()
val listReq = ListObjectsV2Request {
bucket = bucketName
}
s3.listObjectsV2Paginated(listReq)
.flatMapConcat { it.contents?.asFlow() ?: flowOf() }
.filter { it.size > 0 }
.collect { obj ->
val getReq = GetObjectRequest {
bucket = bucketName
key = obj.key
}
s3.getObject(getReq) {
val path = Paths.get(System.getProperty("java.io.tmpdir"), obj.key)
path.parent.createDirectories()
it.body?.writeToFile(path)
}
}
}
在上面的示例中,您可以再次看到 SDK 中挂起函数的使用:getObject
和 writeToFile
函数都使用 suspend
关键字进行标记。
您可以在适用于 Kotlin 的新 AWS SDK 的官方文档中找到有关如何入门的详细分步说明。 此外,您还可以找到许多有趣的示例,它们使用 Amazon DynamoDB、S3、Rekognition、Amazon Simple Notification Service 和 AWS Key Management Service 等一系列 AWS 服务来演示 API。
适用于 Kotlin 的新 AWS SDK 正在积极开发中,您可以在路线图中查看计划的功能。 立即试用,并告诉我们您的想法!
英文博文原作者:
Anton Arhipov
Recommend
-
10
AWS SDK for Go version 2 (v2) – Release Candidate by Sean McGrail | on 23 DEC 2020 | in Announcements,
-
8
AWS SDK for Go Version 2 – General Availability by Shantanu Kotambkar | on 19 JAN 2021 | in Announcements,
-
4
AWS SDK for Java 2.xDeveloper guide - AWS SDK for Java 2.x - AWS SDK for Java ...
-
4
A New AWS SDK for Rust – Alpha Launch by iliana etaoin | on 07 MAY 2021 | in Announcements,
-
3
用 Go 語言 SDK 整合 AWS SageMaker 流程如果團隊未來想把
-
11
Mocking modular AWS SDK for JavaScript (v3) in Unit Tests by Trivikram Kamat | on 27 JUL 2021 | in
-
9
Sample AWS PHP v3 SDK code It's not recommended to store credentials in an executable file. A better option is to load credentials from 'environment variables' or from a...
-
7
Announcing new AWS SDK for Kotlin alpha release by Aaron Todd, Ian Botsford, and Ken Gilmer | on 30 AUG 2021 | in
-
7
Announcing new AWS SDK for Swift alpha release by Nicki Stone and John Woo | on 30 AUG 2021 | in Announc...
-
3
Mobile Android 集成和对适用于 AppCode 的 Kotlin Multiplatform Mobile 插件中各种多平台项目的支持 ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK