I have a lambda which will get triggered when anyone uploads a audio file to the bucket. I need to process the file using the AWS Transcribe asynchronously. I wrote the code to do that but the problem is its check only once, it is not the calling the handler function after the proceesing of the file is completed.
Below is the stackoverflow link for AWS transcribe but we have to wait for thr response till the job is completed and lambda function has a timeout for 5 min. After that the execution will stop.
// to create a async client object to call AWS Transcribe
private AmazonTranscribeAsync asyncClient = AmazonTranscribeAsyncClientBuilder.standard().build();
// below is the method that will call the AWS API with the audio file uploaded
private void startText(String guid, String bucket) {
String jobName = UUID.randomUUID().toString();
StartTranscriptionJobRequest request = new StartTranscriptionJobRequest();
request.withLanguageCode(LanguageCode.EnUS);
Settings channel_settings = new Settings();
channel_settings.setChannelIdentification(true);
channel_settings.withChannelIdentification(true);
Media media = new Media();
media.setMediaFileUri(s3.getUrl(bucket, guid).toString());
request.withMedia(media);
request.setTranscriptionJobName(jobName);
request.withMediaFormat(getFileFormat(guid));
request.withSettings(channel_settings);
asyncClient.startTranscriptionJobAsync(request, new AsyncTranscriptionJobHandler());
}
// async handler method
private class AsyncTranscriptionJobHandler implements AsyncHandler<StartTranscriptionJobRequest, StartTranscriptionJobResult>
{
public void onError(Exception e) {
System.out.println(e.getMessage());
System.exit(1);
}
@Override
public void onSuccess(StartTranscriptionJobRequest request, StartTranscriptionJobResult result) {
logger.log(result.getTranscriptionJob().getTranscriptionJobName());
TranscriptionJob transcriptionJob = result.getTranscriptionJob();
if (transcriptionJob.getTranscriptionJobStatus().equals(TranscriptionJobStatus.COMPLETED.name())) {
logger.log("completed");
} else if(transcriptionJob.getTranscriptionJobStatus().equals(TranscriptionJobStatus.FAILED.name())) {
logger.log("failed");
} else if(transcriptionJob.getTranscriptionJobStatus().equals(TranscriptionJobStatus.IN_PROGRESS.name())) {
logger.log("processing");
}
}
}
The trick here is to not wait for transcribe to finish, but to invoke it in one lambda, and then trigger a separate lambda once the transcription has completed.
AWS Transcribe uses CloudWatch Events to notify when a job completes or fails (https://docs.aws.amazon.com/transcribe/latest/dg/cloud-watch-events.html) which are a supported event source for lambda (https://docs.aws.amazon.com/lambda/latest/dg/invoking-lambda-function.html#supported-event-source-cloudwatch-events)