• Что бы вступить в ряды "Принятый кодер" Вам нужно:
    Написать 10 полезных сообщений или тем и Получить 10 симпатий.
    Для того кто не хочет терять время,может пожертвовать средства для поддержки сервеса, и вступить в ряды VIP на месяц, дополнительная информация в лс.

  • Пользаватели которые будут спамить, уходят в бан без предупреждения. Спам сообщения определяется администрацией и модератором.

  • Гость, Что бы Вы хотели увидеть на нашем Форуме? Изложить свои идеи и пожелания по улучшению форума Вы можете поделиться с нами здесь. ----> Перейдите сюда
  • Все пользователи не прошедшие проверку электронной почты будут заблокированы. Все вопросы с разблокировкой обращайтесь по адресу электронной почте : info@guardianelinks.com . Не пришло сообщение о проверке или о сбросе также сообщите нам.

Using AWS SDK from n8n Code Node

Lomanu4 Оффлайн

Lomanu4

Команда форума
Администратор
Регистрация
1 Мар 2015
Сообщения
1,481
Баллы
155
HTTP Request vs. JS Client Code


Many of the service-integration nodes in n8n are just customized wrappers for a RESTful, http based API. If the entire API is not supported in a particular service-integration node, it is often possible to use an HTTP Request node to implement other RESTful service API calls. It is even possible to "borrow" the credentials item from the service-integration node and use it in the HTTP Request node.

Some services, like (Amazon) AWS services are (apparently) are too complex to support with a simple http based client, so they generally require the use of an SDK client library, and cannot be implemented within n8n by simply using an HTTP Request node. n8n also has a Code node that can use npm based libraries/modules with a bit of special configuration in the n8n runtime environment. This article shows how to use AWS SDK libraries in an n8n Code node.

Polly Text-To-Speech


To demonstrate the use of an AWS SDK library, and handling a binary data response, the example use-case here will call the Polly Text-to-Speech (TTS) service to generate an audio file from text input. The general approach to installing, enabling and using AWS SDK libraries should be applicable to other SDKs, but it's sometimes difficult to understand instructions given in generalized/abstract terms, so hopefully it will be easier to understand this concrete example.

SDK Access Policy


Before using any of the various AWS services via SDK, the authenticated user needs to have access to the service. AWS manages this primarily through policies. Access to the Polly API service can be granted by adding the AWSPollyFullAccess policy to the IAM user to which the Access Key/Secret belongs. (Links below to the AWS user/policy management page.)

Setting up the n8n Runtime Environment


This assumes n8n is running self-hosted, in a Docker container. There are 3 elements of the n8n runtime environment that need to be customized in order to get an AWS SDK client to work.

  1. Install the SDK module/library, and its dependencies
  2. Allow n8n to use the SDK module/library (and dependencies)
  3. Provide configuration/environment-variable-values required by the SDK.
Installing Polly SDK and AWS CredentialsProviders


Use docker build with a Dockerfile like the following to create a customized n8n (Docker) container image with the AWS SDK npm libraries added.

  • Dockerfile

ARG tag=latest
FROM n8nio/n8n:$tag
USER root
RUN npm install -g @aws-sdk/credential-providers
RUN npm install -g @aws-sdk/client-polly
USER node
Allow n8n to use the AWS SDK modules/libraries


Use the image created in the first step instead of the base, published n8n image, specify (or add) the npm library/module names in the NODE_FUNCTION_ALLOW_EXTERNAL environment variable's csv list


  • docker-compose.yml

    services:
    n8n:
    image: n8n-with-aws-sdk-addons
    ...
    environment:
    - N8N_HOST=n8n
    - N8N_PORT=5678
    ...
    - NODE_FUNCTION_ALLOW_EXTERNAL=@aws-sdk/credential-providers,@aws-sdk/client-polly
Set AWS SDK credentials environment variables


In this case, the Code node will use the fromEnv() credentials provider, so it will need environment variables with the access key and secret.


  • docker-compose.yml

    services:
    n8n:
    ...
    environment:
    - N8N_HOST=n8n
    - N8N_PORT=5678
    ...
    - AWS_ACCESS_KEY_ID={your-aws-access-key}
    - AWS_SECRET_ACCESS_KEY={your-aws-secret}
Create a Code node to use the SDK Library/Module


The following example code takes a number of input items and, for each one, calls the Polly AWS service to convert the input text to a generated audio file.


  • The input item must include the text-to-speech input text in an attribute named ttsTextInput like this:

    {
    "otherAttribute": "any other json content",
    "ttsTextInput": "What would a wood chuck chuck if a wood chuck could chuck wood."
    }

  • Code node Javascript

const { PollyClient, SynthesizeSpeechCommand } = require("@aws-sdk/client-polly");
const { fromEnv } = require("@aws-sdk/credential-providers");

/*
* This gets the credentials from environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY.
*/
const config = {
region: "us-east-1",
credentials: fromEnv()
}

const client = new PollyClient(config);

async function sendPollyRequest(ttsTextInput) {
const input = {
OutputFormat: "mp3",
Text: ttsTextInput,
VoiceId: "Joanna"
};
const command = new SynthesizeSpeechCommand(input);
const response = await client.send(command);
const pollyResponseAudioBytes = await response.AudioStream.transformToByteArray();
// console.log(`Received polly response with ${pollyResponseBytes.length} bytes`);
return pollyResponseAudioBytes;
}

for (const item of $input.all()) {
const pollyResponseAudioBytes = await sendPollyRequest(item.json.ttsTextInput);
const pollyResponseAudioBytesBuffer = Buffer.from(pollyResponseAudioBytes);
const n8nBinaryDataPollyAudio = {
data: await this.helpers.prepareBinaryData(pollyResponseAudioBytesBuffer,
'polly-audio-response.mp3', 'audio/mp3')
}

// console.log(`Binary data item: ${JSON.stringify(n8nBinaryDataPollyAudio)}`);
item.binary = n8nBinaryDataPollyAudio;
}


return $input.all();
Links and References



Пожалуйста Авторизируйтесь или Зарегистрируйтесь для просмотра скрытого текста.

 
Вверх Снизу