Sunday, April 17, 2022

AWS Translate using AWS JS SDK

 In this post, I will discuss how to use AWS Javascript SDK V3 for AWS Translate.

Prerequisite: Node.js should be pre-installed

Steps:

  • Create a folder e.g. AWSTranslateApp
  • From command prompt navigate to folder "AWSTranslateApp"
  • Type npm init
  • This will create Node project
  • Install AWS JS SDK by typing "npm install @aws-sdk/client-translate" under folder "AWSTranslateApp"
  • Now open the "AWSTranslateApp" folder in VS Code
  • Create a file translateExample.js

Below is the code snippet for translateExample.js

const { TranslateClient, TranslateTextCommand } = require("@aws-sdk/client-translate");

 async function getTranslatedData(){

  const client = new TranslateClient({ region: "<region_name>", 

  credentials: {

    accessKeyId: '<access_key>', 

    secretAccessKey: '<secret_key>'

  } 

});

  const params = {

    SourceLanguageCode: "en",

    TargetLanguageCode: "es",

    Text: "Hello, world"

  };

      const command = new TranslateTextCommand(params);

      const data = await client.send(command);   

      const jsonData =await data.TranslatedText;

      console.log(jsonData);

  }

 getTranslatedData();


  • Replace the region, access key, secret key with the actual values. In my case the region was 'us-east-1'.
  • Once done, you can now run the program using below command from command prompt

        node translateExample

References:

https://docs.aws.amazon.com/AWSJavaScriptSDK/v3/latest/clients/client-translate/index.html



No comments:

Encryption using Google Tink

Google Tink is a tool which provides an End to End solution for Encryption/Decryption. Steps: Step #1: Create the Encryption key. Goto https...