{"id":227923,"date":"2022-01-28T10:29:02","date_gmt":"2022-01-28T09:29:02","guid":{"rendered":"https:\/\/blog.jetbrains.com\/kotlin\/2022\/01\/the-new-aws-sdk-for-kotlin-with-coroutines-support\/"},"modified":"2022-02-11T12:17:54","modified_gmt":"2022-02-11T11:17:54","slug":"the-new-aws-sdk-for-kotlin-with-coroutines-support","status":"publish","type":"kotlin","link":"https:\/\/blog.jetbrains.com\/zh-hans\/kotlin\/2022\/01\/the-new-aws-sdk-for-kotlin-with-coroutines-support\/","title":{"rendered":"\u9002\u7528\u4e8e Kotlin \u5e76\u652f\u6301\u534f\u540c\u7a0b\u5e8f\u7684\u65b0 AWS SDK"},"content":{"rendered":"\n<p>\u9002\u7528\u4e8e Kotlin \u7684\u65b0 AWS SDK \u5df2\u4e8e 2021 \u5e74 12 \u6708\u5728 AWS re:Invent \u4e0a\u5ba3\u5e03\u3002&nbsp;\u60a8\u53ef\u4ee5\u4f7f\u7528\u6b64 SDK \u6784\u5efa\u4e0e Amazon S3\u3001Amazon EC2\u3001DynamoDB \u7b49\u534f\u540c\u5de5\u4f5c\u7684 Kotlin \u5e94\u7528\u7a0b\u5e8f\u3002&nbsp;SDK \u76ee\u524d\u5904\u4e8e\u9884\u89c8\u9636\u6bb5\uff0c\u9884\u8ba1\u5f88\u5feb\u4f1a\u63a8\u51fa\u7a33\u5b9a\u7248\u3002<\/p>\n\n\n\n<p>\u4ece\u5934\u5f00\u59cb\u8bbe\u8ba1 SDK \u662f\u4e3a\u4e86\u652f\u6301 Kotlin \u8bed\u8a00\u53ca\u5176\u6700\u4f73\u505a\u6cd5\uff0c\u4e3a Kotlin \u5f00\u53d1\u8005\u63d0\u4f9b\u4e0e AWS \u4ea4\u4e92\u7684\u719f\u6089\u548c\u60ef\u7528\u4f53\u9a8c\u3002&nbsp;SDK \u5229\u7528 Kotlin \u534f\u540c\u7a0b\u5e8f\u8fdb\u884c\u5f02\u6b65\u5b9e\u73b0\uff0c\u5e76\u8ba1\u5212\u4f7f\u5176\u517c\u5bb9 Kotlin \u591a\u5e73\u53f0\u9879\u76ee\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"800\" height=\"418\" src=\"https:\/\/blog.jetbrains.com\/wp-content\/uploads\/2022\/01\/AWS-SDK-for-Kotlin_Twitter-Blog.png\" alt=\"\" class=\"wp-image-221785\"\/><\/figure>\n\n\n\n<p>\u8981\u5f00\u59cb\u5728 Kotlin \u7a0b\u5e8f\u4e2d\u4f7f\u7528\u65b0 SDK\uff0c\u60a8\u9700\u8981\u5411\u9879\u76ee\u6dfb\u52a0\u76f8\u5e94\u7684\u4f9d\u8d56\u9879\u3002&nbsp;\u4f8b\u5982\uff0c\u5982\u679c\u8981\u4e0e DynamoDB \u901a\u4fe1\uff0c\u5219\u9700\u8981&nbsp;<code>aws.sdk.kotlin:dynamodb<\/code>&nbsp;\u6a21\u5757\uff1a<\/p>\n\n\n\n<pre class=\"kotlin-code\" data-highlight-only=\"true\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\nrepositories {\n    mavenCentral()\n}\n\ndependencies {\n    implementation(&quot;org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.0&quot;)\n    \n    \/\/ The following line adds a dependency on the dynamodb client.\n    \/\/ For demonstration purposes, we use 0.+ to get the latest version\n    implementation(&quot;aws.sdk.kotlin:dynamodb:0.+&quot;)\n}\n<\/pre>\n\n\n\n<p>\u60a8\u53ef\u4ee5\u5728&nbsp;<a href=\"https:\/\/docs.aws.amazon.com\/sdk-for-kotlin\/latest\/reference\/\" target=\"_blank\" rel=\"noopener\">AWS SDK \u6587\u6863<\/a>\u4e2d\u627e\u5230\u53d7\u652f\u6301\u6a21\u5757\u7684\u5b8c\u6574\u5217\u8868\u3002<\/p>\n\n\n\n<p>\u4e0b\u8f7d\u4f9d\u8d56\u9879\u540e\uff0c\u5373\u53ef\u5f00\u59cb\u4f7f\u7528 API\uff1a<\/p>\n\n\n\n<pre class=\"kotlin-code\" data-highlight-only=\"true\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\nimport kotlinx.coroutines.runBlocking\nimport aws.sdk.kotlin.services.dynamodb.DynamoDbClient\n\/\/sampleStart \nfun main() = runBlocking {\n    val client = DynamoDbClient { region = &quot;us-east-2&quot; }\n    val resp = client.listTables { limit = 10 }\n\n    println(&quot;Current DynamoDB tables: &quot;)\n    resp.tableNames?.forEach { println(it) }\n\n    client.close()\n}\n\/\/sampleEnd \n<\/pre>\n\n\n\n<p>\u8be5\u7a0b\u5e8f\u53ea\u4f1a\u68c0\u7d22\u8868\u7684\u5217\u8868\uff0c\u5e76\u5c06\u8868\u540d\u6253\u5370\u5230\u6807\u51c6\u8f93\u51fa\u4e2d\u3002&nbsp;\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c<code>listTables<\/code>&nbsp;\u662f\u4e00\u4e2a\u6302\u8d77\u51fd\u6570\uff0c\u56e0\u6b64\u4ee3\u7801\u88ab\u5305\u88c5\u5230&nbsp;<code>runBlocking<\/code>&nbsp;\u8c03\u7528\u4e2d\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u6765\u770b\u4e00\u4e2a\u7a0d\u5fae\u590d\u6742\u4e00\u70b9\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u5c06\u5b58\u50a8\u6876\u4e2d\u7684\u6bcf\u4e2a S3 \u5bf9\u8c61\u4e0b\u8f7d\u5230\u672c\u5730\u4e34\u65f6\u76ee\u5f55\uff08\u5305\u542b\u5206\u9875\u8bbf\u95ee\u3001\u5e76\u53d1\u6d41\u548c\u5b57\u8282\u6d41\u54cd\u5e94\uff09\u3002<\/p>\n\n\n\n<pre class=\"kotlin-code\" data-highlight-only=\"true\" theme=\"idea\" indent=\"4\" style=\"visibility: hidden; padding: 36px 0;\">\nimport aws.sdk.kotlin.services.s3.S3Client\nimport aws.sdk.kotlin.services.s3.model.GetObjectRequest\nimport aws.sdk.kotlin.services.s3.model.ListObjectsV2Request\nimport aws.sdk.kotlin.services.s3.paginators.listObjectsV2Paginated\nimport aws.smithy.kotlin.runtime.content.writeToFile\nimport kotlinx.coroutines.flow.asFlow\nimport kotlinx.coroutines.flow.filter\nimport kotlinx.coroutines.flow.flatMapConcat\nimport kotlinx.coroutines.flow.flowOf\nimport java.nio.file.Paths\nimport kotlin.io.path.createDirectories\n\/\/sampleStart \nsuspend fun downloadAllS3Objects(bucketName: String) {\n   val s3 = S3Client.fromEnvironment()\n   val listReq = ListObjectsV2Request {\n       bucket = bucketName\n   }\n   s3.listObjectsV2Paginated(listReq)\n       .flatMapConcat { it.contents?.asFlow() ?: flowOf() }\n       .filter { it.size &gt; 0 }\n       .collect { obj -&gt;                     \n           val getReq = GetObjectRequest {\n               bucket = bucketName\n               key = obj.key\n           }\n           s3.getObject(getReq) {\n               val path = Paths.get(System.getProperty(&quot;java.io.tmpdir&quot;), obj.key)\n               path.parent.createDirectories()\n               it.body?.writeToFile(path)\n           }\n       }\n}\n\/\/sampleEnd\n<\/pre>\n\n\n\n<p>\u5728\u4e0a\u9762\u7684\u793a\u4f8b\u4e2d\uff0c\u60a8\u53ef\u4ee5\u518d\u6b21\u770b\u5230 SDK \u4e2d\u6302\u8d77\u51fd\u6570\u7684\u4f7f\u7528\uff1a<code>getObject<\/code>&nbsp;\u548c&nbsp;<code>writeToFile<\/code>&nbsp;\u51fd\u6570\u90fd\u4f7f\u7528&nbsp;<code>suspend<\/code>&nbsp;\u5173\u952e\u5b57\u8fdb\u884c\u6807\u8bb0\u3002<\/p>\n\n\n\n<p>\u60a8\u53ef\u4ee5\u5728\u9002\u7528\u4e8e Kotlin \u7684\u65b0 AWS SDK \u7684\u5b98\u65b9\u6587\u6863\u4e2d\u627e\u5230\u6709\u5173<a href=\"https:\/\/docs.aws.amazon.com\/sdk-for-kotlin\/latest\/developer-guide\/get-started.html\" target=\"_blank\" rel=\"noopener\">\u5982\u4f55\u5165\u95e8<\/a>\u7684\u8be6\u7ec6\u5206\u6b65\u8bf4\u660e\u3002&nbsp;\u6b64\u5916\uff0c\u60a8\u8fd8\u53ef\u4ee5\u627e\u5230\u8bb8\u591a\u6709\u8da3\u7684<a href=\"https:\/\/docs.aws.amazon.com\/sdk-for-kotlin\/latest\/developer-guide\/examples.html\" target=\"_blank\" rel=\"noopener\">\u793a\u4f8b<\/a>\uff0c\u5b83\u4eec\u4f7f\u7528 Amazon DynamoDB\u3001S3\u3001Rekognition\u3001Amazon Simple Notification Service \u548c AWS Key Management Service \u7b49\u4e00\u7cfb\u5217 AWS \u670d\u52a1\u6765\u6f14\u793a API\u3002<\/p>\n\n\n\n<p>\u9002\u7528\u4e8e Kotlin \u7684\u65b0 AWS SDK \u6b63\u5728\u79ef\u6781\u5f00\u53d1\u4e2d\uff0c\u60a8\u53ef\u4ee5\u5728<a href=\"https:\/\/github.com\/awslabs\/aws-sdk-kotlin\/projects\/2\" target=\"_blank\" rel=\"noopener\">\u8def\u7ebf\u56fe<\/a>\u4e2d\u67e5\u770b\u8ba1\u5212\u7684\u529f\u80fd\u3002\u00a0\u7acb\u5373\u8bd5\u7528\uff0c\u5e76\u544a\u8bc9\u6211\u4eec\u60a8\u7684\u60f3\u6cd5\uff01<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>\u82f1\u6587\u535a\u6587\u539f\u4f5c\u8005\uff1a<\/p>\n\n\n    <div class=\"about-author \">\n        <div class=\"about-author__box\">\n            <div class=\"row\">\n                <div class=\"about-author__box-img\">\n                    <img decoding=\"async\" src=\"https:\/\/secure.gravatar.com\/avatar\/?s=200&#038;r=g\" width=\"200\" height=\"200\" alt=\"\" loading=\"lazy\"  class=\"avatar avatar-200 wp-user-avatar wp-user-avatar-200 photo avatar-default\">\n                <\/div>\n                <div class=\"about-author__box-text\">\n                                                        <\/div>\n            <\/div>\n        <\/div>\n    <\/div>\n\n\n\n<p><\/p>\n","protected":false},"author":1297,"featured_media":221797,"comment_status":"closed","ping_status":"closed","template":"","categories":[89],"tags":[1041,6850,21,4975],"cross-post-tag":[],"acf":[],"_links":{"self":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/kotlin\/227923"}],"collection":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/kotlin"}],"about":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/types\/kotlin"}],"author":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/users\/1297"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/comments?post=227923"}],"version-history":[{"count":5,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/kotlin\/227923\/revisions"}],"predecessor-version":[{"id":228090,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/kotlin\/227923\/revisions\/228090"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media\/221797"}],"wp:attachment":[{"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/media?parent=227923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/categories?post=227923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/tags?post=227923"},{"taxonomy":"cross-post-tag","embeddable":true,"href":"https:\/\/blog.jetbrains.com\/zh-hans\/wp-json\/wp\/v2\/cross-post-tag?post=227923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}