Skip to content

Commit

Permalink
Get Kotlin MS Examples ready for Release (awsdocs#5655)
Browse files Browse the repository at this point in the history
* updated Gradle Build file
  • Loading branch information
scmacdon authored and max-webster committed Mar 15, 2024
1 parent c8409e0 commit ff0a65f
Show file tree
Hide file tree
Showing 43 changed files with 492 additions and 1,424 deletions.
22 changes: 15 additions & 7 deletions kotlin/usecases/creating_message_application/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
| ----------- | ----------- |
| Description | Discusses how to develop a Spring REST API that sends and retrieves messages by using the AWS SDK for Kotlin and Amazon Simple Queue Service (Amazon SQS). This application also detects the language code of the posted message by using Amazon Comprehend. The Spring REST API is used by a React application that displays the data. |
| Audience | Developer (intermediate) |
| Updated | 9/15/2022 |
| Updated | 11/14/2023 |
| Required skills | Kotlin, Gradle, JavaScript |


Expand Down Expand Up @@ -36,8 +36,8 @@ To complete the tutorial, you need the following:

+ An AWS account.
+ A Kotlin IDE. (This tutorial uses the IntelliJ IDE.)
+ Java 1.8 JDK.
+ Gradle 6.8 or higher.
+ Java 17 JDK.
+ Gradle 8.1 or higher.
+ A Kotlin development environment setup. For more information, see [Setting up the AWS SDK for Kotlin](https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html).

**Note**: Make sure that you have installed the Kotlin plug-in for IntelliJ.
Expand Down Expand Up @@ -99,13 +99,19 @@ At this point, you have a new project named **AWSMessageRestKotlin**. Confirm th
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.7.10"
kotlin("jvm") version "1.9.0"
application
}

group = "me.scmacdon"
version = "1.0-SNAPSHOT"

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}


buildscript {
repositories {
maven("https://plugins.gradle.org/m2/")
Expand All @@ -121,19 +127,21 @@ repositories {
}
apply(plugin = "org.jlleitschuh.gradle.ktlint")
dependencies {
implementation("aws.sdk.kotlin:sqs:0.33.1-beta")
implementation("aws.sdk.kotlin:comprehend:0.33.1-beta")
implementation("aws.smithy.kotlin:http-client-engine-okhttp:0.28.0")
implementation("aws.smithy.kotlin:http-client-engine-crt:0.28.0")
implementation("org.springframework.boot:spring-boot-starter-web:2.7.3")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.13.3")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("aws.sdk.kotlin:sqs:0.17.1-beta")
implementation("aws.sdk.kotlin:comprehend:0.17.1-beta")
testImplementation("org.springframework.boot:spring-boot-starter-test:2.7.3")
}

tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
jvmTarget = "17"
}
}

Expand Down
15 changes: 11 additions & 4 deletions kotlin/usecases/creating_message_application/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.7.10"
kotlin("jvm") version "1.9.0"
application
}

group = "me.scmacdon"
version = "1.0-SNAPSHOT"

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

buildscript {
repositories {
maven("https://plugins.gradle.org/m2/")
Expand All @@ -23,19 +28,21 @@ repositories {
}
apply(plugin = "org.jlleitschuh.gradle.ktlint")
dependencies {
implementation("aws.sdk.kotlin:sqs:1.0.0")
implementation("aws.sdk.kotlin:comprehend:1.0.0")
implementation("aws.smithy.kotlin:http-client-engine-okhttp:0.30.0")
implementation("aws.smithy.kotlin:http-client-engine-crt:0.30.0")
implementation("org.springframework.boot:spring-boot-starter-web:2.7.3")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.13.3")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("aws.sdk.kotlin:sqs:0.17.1-beta")
implementation("aws.sdk.kotlin:comprehend:0.17.1-beta")
testImplementation("org.springframework.boot:spring-boot-starter-test:2.7.3")
}

tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
jvmTarget = "17"
}
}

Expand Down
54 changes: 34 additions & 20 deletions kotlin/usecases/creating_pam/Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
| ----------- | ----------- |
| Description | Discusses how to develop a photo asset management application for users to manage photos with labels. This application is developed by using the AWS SDK for Kotlin. |
| Audience | Developer (beginner / intermediate) |
| Updated | 4/25/2023 |
| Updated | 11/14/2023 |
| Required skills | Kotlin, Gradle |

## Purpose
Expand Down Expand Up @@ -42,8 +42,8 @@ To complete the tutorial, you need the following:

+ An AWS account.
+ A Kotlin IDE. (This tutorial uses the IntelliJ IDE with the Kotlin plugin).
+ Java JDK 11.
+ Gradle 6.8 or higher.
+ Java JDK 17.
+ Gradle 8.1 or higher.
+ You must also set up your Kotlin development environment. For more information, see [Get started with the SDK for Kotlin](https://docs.aws.amazon.com/sdk-for-kotlin/latest/developer-guide/setup.html).

### Important
Expand Down Expand Up @@ -204,17 +204,22 @@ At this point, you have a new project named **PhotoAssetKotlin**.
Add the following dependencies to your Gradle build file.

```xml
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.7.10"
kotlin("jvm") version "1.9.0"
id("com.github.johnrengelman.shadow") version "7.1.0"
application
}

group = "me.scmacdon"
version = "1.0-SNAPSHOT"

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

buildscript {
repositories {
maven("https://plugins.gradle.org/m2/")
Expand All @@ -226,12 +231,17 @@ buildscript {

repositories {
mavenCentral()
jcenter()
gradlePluginPortal()
}

apply(plugin = "org.jlleitschuh.gradle.ktlint")
dependencies {
implementation("aws.sdk.kotlin:dynamodb:1.0.0")
implementation("aws.sdk.kotlin:s3:1.0.0")
implementation("aws.sdk.kotlin:sns:1.0.0")
implementation("aws.sdk.kotlin:rekognition:1.0.0")
implementation("aws.smithy.kotlin:http-client-engine-okhttp:0.28.0")
implementation("aws.smithy.kotlin:http-client-engine-crt:0.28.0")
implementation("org.springframework.boot:spring-boot-starter-web:2.7.5")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.13.3")
implementation("org.jetbrains.kotlin:kotlin-reflect")
Expand All @@ -241,10 +251,6 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("javax.mail:javax.mail-api:1.6.2")
implementation("com.sun.mail:javax.mail:1.6.2")
implementation("aws.sdk.kotlin:dynamodb-jvm:0.21.3-beta")
implementation("aws.sdk.kotlin:s3-jvm:0.21.3-beta")
implementation("aws.sdk.kotlin:sns-jvm:0.21.3-beta")
implementation("aws.sdk.kotlin:rekognition-jvm:0.21.3-beta")
implementation("com.google.code.gson:gson:2.10")
implementation("org.json:json:20230227")
implementation("com.googlecode.json-simple:json-simple:1.1.1")
Expand All @@ -258,14 +264,15 @@ application {
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
jvmTarget = "17"
}
}

tasks.withType<Test> {
useJUnitPlatform()
}


```

## Create the Kotlin classes
Expand Down Expand Up @@ -814,18 +821,20 @@ package com.example.photo.services
import aws.sdk.kotlin.services.s3.S3Client
import aws.sdk.kotlin.services.s3.model.GetObjectRequest
import aws.sdk.kotlin.services.s3.model.PutObjectRequest
import aws.sdk.kotlin.services.s3.presigners.presign
import aws.sdk.kotlin.services.s3.presigners.presignGetObject
import aws.sdk.kotlin.services.s3.presigners.presignPutObject
import aws.smithy.kotlin.runtime.content.ByteStream
import aws.smithy.kotlin.runtime.content.toByteArray
import com.example.photo.PhotoApplicationResources
import java.io.ByteArrayOutputStream
import java.net.URL
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.minutes

class S3Service {
// Place the uploaded image into an S3 bucket.
// Place the upload image into an Amazon S3 bucket
suspend fun putObject(data: ByteArray, objectKey: String) {
val request = PutObjectRequest {
bucket = PhotoApplicationResources.STORAGE_BUCKET
Expand All @@ -838,7 +847,7 @@ class S3Service {
}
}

// Place the uploaded image into an S3 bucket.
// Place the upload image into an Amazon S3 bucket.
suspend fun putZIP(data: ByteArray, objectKey: String) {
val request = PutObjectRequest {
bucket = PhotoApplicationResources.WORKING_BUCKET
Expand Down Expand Up @@ -885,13 +894,16 @@ class S3Service {

suspend fun signObjectToDownload(keyName: String?): String? {
S3Client { region = "us-east-1" }.use { s3Client ->
val presignedUrl = GetObjectRequest {
val unsignedRequest = GetObjectRequest {
bucket = PhotoApplicationResources.WORKING_BUCKET
key = keyName
}.presign(s3Client.config, 1L.hours).url
}

// Presign the GetObject request.
val presignedRequest = s3Client.presignGetObject(unsignedRequest, 1.hours)
val presignedUrl = URL(presignedRequest.url.toString()).readText()
println(presignedUrl)
return presignedUrl.toString()
return presignedUrl
}
}

Expand All @@ -901,14 +913,16 @@ class S3Service {
bucket = PhotoApplicationResources.WORKING_BUCKET
key = keyName
contentType = "image/jpeg"
}.presign(s3Client.config, 5L.minutes).url
}

println(presignedUrl)
return presignedUrl.toString()
val presignedRequest = s3Client.presignPutObject(presignedUrl, 5L.minutes)
println(presignedRequest.url.toString())
return presignedRequest.url.toString()
}
}
}


```

### SnsService class
Expand Down
20 changes: 13 additions & 7 deletions kotlin/usecases/creating_pam/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
kotlin("jvm") version "1.7.10"
kotlin("jvm") version "1.9.0"
id("com.github.johnrengelman.shadow") version "7.1.0"
application
}

group = "me.scmacdon"
version = "1.0-SNAPSHOT"

java {
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

buildscript {
repositories {
maven("https://plugins.gradle.org/m2/")
Expand All @@ -20,12 +25,17 @@ buildscript {

repositories {
mavenCentral()
jcenter()
gradlePluginPortal()
}

apply(plugin = "org.jlleitschuh.gradle.ktlint")
dependencies {
implementation("aws.sdk.kotlin:dynamodb:1.0.0")
implementation("aws.sdk.kotlin:s3:1.0.0")
implementation("aws.sdk.kotlin:sns:1.0.0")
implementation("aws.sdk.kotlin:rekognition:1.0.0")
implementation("aws.smithy.kotlin:http-client-engine-okhttp:0.30.0")
implementation("aws.smithy.kotlin:http-client-engine-crt:0.30.0")
implementation("org.springframework.boot:spring-boot-starter-web:2.7.5")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin:2.13.3")
implementation("org.jetbrains.kotlin:kotlin-reflect")
Expand All @@ -35,10 +45,6 @@ dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
implementation("javax.mail:javax.mail-api:1.6.2")
implementation("com.sun.mail:javax.mail:1.6.2")
implementation("aws.sdk.kotlin:dynamodb-jvm:0.21.3-beta")
implementation("aws.sdk.kotlin:s3-jvm:0.21.3-beta")
implementation("aws.sdk.kotlin:sns-jvm:0.21.3-beta")
implementation("aws.sdk.kotlin:rekognition-jvm:0.21.3-beta")
implementation("com.google.code.gson:gson:2.10")
implementation("org.json:json:20230227")
implementation("com.googlecode.json-simple:json-simple:1.1.1")
Expand All @@ -52,7 +58,7 @@ application {
tasks.withType<KotlinCompile> {
kotlinOptions {
freeCompilerArgs = listOf("-Xjsr305=strict")
jvmTarget = "1.8"
jvmTarget = "17"
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@ package com.example.photo.services
import aws.sdk.kotlin.services.s3.S3Client
import aws.sdk.kotlin.services.s3.model.GetObjectRequest
import aws.sdk.kotlin.services.s3.model.PutObjectRequest
import aws.sdk.kotlin.services.s3.presigners.presign
import aws.sdk.kotlin.services.s3.presigners.presignGetObject
import aws.sdk.kotlin.services.s3.presigners.presignPutObject
import aws.smithy.kotlin.runtime.content.ByteStream
import aws.smithy.kotlin.runtime.content.toByteArray
import com.example.photo.PhotoApplicationResources
import java.io.ByteArrayOutputStream
import java.net.URL
import java.util.zip.ZipEntry
import java.util.zip.ZipOutputStream
import kotlin.time.Duration.Companion.hours
import kotlin.time.Duration.Companion.minutes

class S3Service {
// Place the upload image into an Amazon S3 bucket.
// Place the upload image into an Amazon S3 bucket
suspend fun putObject(data: ByteArray, objectKey: String) {
val request = PutObjectRequest {
bucket = PhotoApplicationResources.STORAGE_BUCKET
Expand Down Expand Up @@ -79,13 +81,16 @@ class S3Service {

suspend fun signObjectToDownload(keyName: String?): String? {
S3Client { region = "us-east-1" }.use { s3Client ->
val presignedUrl = GetObjectRequest {
val unsignedRequest = GetObjectRequest {
bucket = PhotoApplicationResources.WORKING_BUCKET
key = keyName
}.presign(s3Client.config, 1L.hours).url
}

// Presign the GetObject request.
val presignedRequest = s3Client.presignGetObject(unsignedRequest, 1.hours)
val presignedUrl = URL(presignedRequest.url.toString()).readText()
println(presignedUrl)
return presignedUrl.toString()
return presignedUrl
}
}

Expand All @@ -95,10 +100,11 @@ class S3Service {
bucket = PhotoApplicationResources.WORKING_BUCKET
key = keyName
contentType = "image/jpeg"
}.presign(s3Client.config, 5L.minutes).url
}

println(presignedUrl)
return presignedUrl.toString()
val presignedRequest = s3Client.presignPutObject(presignedUrl, 5L.minutes)
println(presignedRequest.url.toString())
return presignedRequest.url.toString()
}
}
}
Loading

0 comments on commit ff0a65f

Please sign in to comment.