Page updated Apr 17, 2024

Use AWS SDK

If you need functionality in the AWS services used by the Amplify Predictions category that isn't available, we provide an escape hatch so you can get a reference to that service. For example, to get a reference to RekognitionClient:

Learn more about consuming Kotlin clients from Java using either a blocking interface or an equivalent async interface based on futures here.

1// Obtain reference to the plugin
2AWSPredictionsPlugin predictionsPlugin = (AWSPredictionsPlugin)
3 Amplify.Predictions.getPlugin("awsPredictionsPlugin");
4AWSPredictionsEscapeHatch escapeHatch = predictionsPlugin.getEscapeHatch();
5
6// Send a new request to the Rekognition endpoint directly using the client
7RekognitionClient client = escapeHatch.getRekognitionClient();
8CreateCollectionRequest request = CreateCollectionRequest.Companion.invoke((requestBuilder) -> {
9 requestBuilder.setCollectionId("<new-collection-id-here>");
10 return Unit.INSTANCE;
11});
12client.createCollection(request, new Continuation<CreateCollectionResponse>() {
13 @NonNull
14 @Override
15 public CoroutineContext getContext() {
16 return GlobalScope.INSTANCE.getCoroutineContext();
17 }
18
19 @Override
20 public void resumeWith(@NonNull Object resultOrException) {
21 Log.i("MyAmplifyApp", resultOrException.toString());
22 }
23});
1// Obtain reference to the plugin
2val plugin = Amplify.Predictions.getPlugin("awsPredictionsPlugin")
3val escapeHatch = (plugin as AWSPredictionsPlugin).escapeHatch
4
5// Send a new request to the Rekognition endpoint directly using the client
6val client = escapeHatch.rekognitionClient
7val request = CreateCollectionRequest {
8 collectionId = "<new-collection-id-here>"
9}
10client.createCollection(request)

In addition to the Amazon Polly APIs, the PollyClient returned in the escape hatch provides a way to create a presigned URL for a text to speech request. An example of creating a presigned URL is below.

1// Obtain reference to the plugin
2AWSPredictionsPlugin predictionsPlugin = (AWSPredictionsPlugin)
3 Amplify.Predictions.getPlugin("awsPredictionsPlugin");
4AWSPredictionsEscapeHatch escapeHatch = predictionsPlugin.getEscapeHatch();
5
6// Create a presigned URL to convert text to speech using the client
7AmazonPollyPresigningClient client = (AmazonPollyPresigningClient) escapeHatch.getPollyClient();
8SynthesizeSpeechRequest request = SynthesizeSpeechRequest.Companion.invoke((requestBuilder) -> {
9 requestBuilder.setLanguageCode(LanguageCode.EnUs.INSTANCE);
10 requestBuilder.setText("I like to eat spaghetti");
11 return Unit.INSTANCE;
12});
13
14// The presigned URL needs to be created on a worker thread
15runOnUiThread(new Runnable() {
16 @Override
17 public void run() {
18 URL url = client.getPresignedSynthesizeSpeechUrl(request);
19 }
20});
1// Obtain reference to the plugin
2val plugin = Amplify.Predictions.getPlugin("awsPredictionsPlugin")
3val escapeHatch = (plugin as AWSPredictionsPlugin).escapeHatch
4
5// Create a presigned URL to convert text to speech using the client
6val client = escapeHatch.pollyClient as AmazonPollyPresigningClient
7val request = SynthesizeSpeechRequest {
8 languageCode = LanguageCode.EnUs
9 text = "I like to eat spaghetti"
10}
11
12// The presigned URL needs to be created on a worker thread
13runOnUiThread {
14 val url = client.getPresignedSynthesizeSpeechUrl(request)
15}

API Documentation Resources