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
:
1// Obtain reference to the plugin2AWSPredictionsPlugin predictionsPlugin = (AWSPredictionsPlugin)3 Amplify.Predictions.getPlugin("awsPredictionsPlugin");4AWSPredictionsEscapeHatch escapeHatch = predictionsPlugin.getEscapeHatch();5
6// Send a new request to the Rekognition endpoint directly using the client7RekognitionClient 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 @NonNull14 @Override15 public CoroutineContext getContext() {16 return GlobalScope.INSTANCE.getCoroutineContext();17 }18
19 @Override20 public void resumeWith(@NonNull Object resultOrException) {21 Log.i("MyAmplifyApp", resultOrException.toString());22 }23});
1// Obtain reference to the plugin2val plugin = Amplify.Predictions.getPlugin("awsPredictionsPlugin")3val escapeHatch = (plugin as AWSPredictionsPlugin).escapeHatch4
5// Send a new request to the Rekognition endpoint directly using the client6val client = escapeHatch.rekognitionClient7val 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 plugin2AWSPredictionsPlugin predictionsPlugin = (AWSPredictionsPlugin)3 Amplify.Predictions.getPlugin("awsPredictionsPlugin");4AWSPredictionsEscapeHatch escapeHatch = predictionsPlugin.getEscapeHatch();5
6// Create a presigned URL to convert text to speech using the client7AmazonPollyPresigningClient 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 thread15runOnUiThread(new Runnable() {16 @Override17 public void run() {18 URL url = client.getPresignedSynthesizeSpeechUrl(request);19 }20});
1// Obtain reference to the plugin2val plugin = Amplify.Predictions.getPlugin("awsPredictionsPlugin")3val escapeHatch = (plugin as AWSPredictionsPlugin).escapeHatch4
5// Create a presigned URL to convert text to speech using the client6val client = escapeHatch.pollyClient as AmazonPollyPresigningClient7val request = SynthesizeSpeechRequest {8 languageCode = LanguageCode.EnUs9 text = "I like to eat spaghetti"10}11
12// The presigned URL needs to be created on a worker thread13runOnUiThread {14 val url = client.getPresignedSynthesizeSpeechUrl(request)15}
API Documentation Resources