适用于 JavaScript 的 Amazon SDKV3 API 参考指南详细描述了 适用于 JavaScript 的 Amazon SDK 版本 3 (V3) 的所有 API 操作。
Amazon Transcribe 示例
在此示例中,使用一系列 Node.js 模块通过 TranscribeService 客户端类的以下方法创建、列出和删除转录作业:
有关 Amazon Transcribe 的更多信息,请参阅 Amazon Transcribe 开发人员指南。
先决条件任务
要设置和运行此示例,您必须先完成以下任务:
重要
这些示例演示了如何使用 ECMAScript6(ES6)导入/导出客户端服务对象和命令。
这需要使用 Node.js 版本 13.x 或更高版本。要下载并安装最新版本的 Node.js,请参阅 Node.js 下载
。 如果您更喜欢使用 CommonJS 语法,请参阅 JavaScript ES6/CommonJS 语法
启动 Amazon Transcribe 作业
此示例演示如何使用 适用于 JavaScript 的 Amazon SDK 启动 Amazon Transcribe 转录作业。有关更多信息,请参阅 StartTranscriptionJobCommand。
创建一个 libs 目录,然后使用文件名 transcribeClient.js 创建一个 Node.js 模块。将以下代码复制并粘贴到其中,这将创建 Amazon Transcribe 客户端对象。将 REGION 替换为您的 Amazon 区域。
import { TranscribeClient } from "@aws-sdk/client-transcribe"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon Transcribe service client object. const transcribeClient = new TranscribeClient({ region: REGION }); export { transcribeClient };
此示例代码可在 GitHub 上的此处
创建文件名为 transcribe-create-job.js 的 Node.js 模块。确保如前所示配置 SDK,包括安装所需的客户端和软件包。创建一个参数对象,指定所需的参数。使用 StartMedicalTranscriptionJobCommand 命令启动作业。
注意
将 MEDICAL_JOB_NAME 替换为转录作业的名称。对于 OUTPUT_BUCKET_NAME,指定用于保存输出的 Amazon S3 存储桶。对于 JOB_TYPE,请指定作业类型。对于 SOURCE_LOCATION,指定源文件的位置。对于 SOURCE_FILE_LOCATION,指定输入媒体文件的位置。
// Import the required AWS SDK clients and commands for Node.js import { StartTranscriptionJobCommand } from "@aws-sdk/client-transcribe"; import { transcribeClient } from "./libs/transcribeClient.js"; // Set the parameters export const params = { TranscriptionJobName: "JOB_NAME", LanguageCode: "LANGUAGE_CODE", // For example, 'en-US' MediaFormat: "SOURCE_FILE_FORMAT", // For example, 'wav' Media: { MediaFileUri: "SOURCE_LOCATION", // For example, "https://transcribe-demo.s3-REGION.amazonaws.com/hello_world.wav" }, OutputBucketName: "OUTPUT_BUCKET_NAME", }; export const run = async () => { try { const data = await transcribeClient.send( new StartTranscriptionJobCommand(params), ); console.log("Success - put", data); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
要运行示例,请在命令提示符中键入以下内容。
node transcribe-create-job.js
此示例代码可在 GitHub 上的此处
列出 Amazon Transcribe 作业
此示例演示如何使用 适用于 JavaScript 的 Amazon SDK 列出 Amazon Transcribe 转录作业。有关您可以修改的其他设置的更多信息,请参阅 ListTranscriptionJobCommand。
创建一个 libs 目录,然后使用文件名 transcribeClient.js 创建一个 Node.js 模块。将以下代码复制并粘贴到其中,这将创建 Amazon Transcribe 客户端对象。将 REGION 替换为您的 Amazon 区域。
import { TranscribeClient } from "@aws-sdk/client-transcribe"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon Transcribe service client object. const transcribeClient = new TranscribeClient({ region: REGION }); export { transcribeClient };
此示例代码可在 GitHub 上的此处
创建文件名为 transcribe-list-jobs.js 的 Node.js 模块。确保如前所示配置 SDK,包括安装所需的客户端和软件包。使用所需参数创建参数对象。
注意
将 KEY_WORD 替换为返回的作业名称必须包含的关键字。
// Import the required AWS SDK clients and commands for Node.js import { ListTranscriptionJobsCommand } from "@aws-sdk/client-transcribe"; import { transcribeClient } from "./libs/transcribeClient.js"; // Set the parameters export const params = { JobNameContains: "KEYWORD", // Not required. Returns only transcription // job names containing this string }; export const run = async () => { try { const data = await transcribeClient.send( new ListTranscriptionJobsCommand(params), ); console.log("Success", data.TranscriptionJobSummaries); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
要运行示例,请在命令提示符中键入以下内容。
node transcribe-list-jobs.js
此示例代码可在 GitHub 上的此处
删除 Amazon Transcribe 作业
此示例演示如何使用 适用于 JavaScript 的 Amazon SDK 删除 Amazon Transcribe 转录作业。有关选项的更多信息,请参阅 DeleteTranscriptionJobCommand。
创建一个 libs 目录,然后使用文件名 transcribeClient.js 创建一个 Node.js 模块。将以下代码复制并粘贴到其中,这将创建 Amazon Transcribe 客户端对象。将 REGION 替换为您的 Amazon 区域。
import { TranscribeClient } from "@aws-sdk/client-transcribe"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create Transcribe service object. const transcribeClient = new TranscribeClient({ region: REGION }); export { transcribeClient };
此示例代码可在 GitHub 上的此处
创建文件名为 transcribe-delete-job.js 的 Node.js 模块。确保如前所示配置 SDK,包括安装所需的客户端和软件包。指定要删除的作业的 Amazon 区域和名称。
注意
将 JOB_NAME 替换为要删除的作业的名称。
// Import the required AWS SDK clients and commands for Node.js import { DeleteTranscriptionJobCommand } from "@aws-sdk/client-transcribe"; import { transcribeClient } from "./libs/transcribeClient.js"; // Set the parameters export const params = { TranscriptionJobName: "JOB_NAME", // Required. For example, 'transciption_demo' }; export const run = async () => { try { const data = await transcribeClient.send( new DeleteTranscriptionJobCommand(params), ); console.log("Success - deleted"); return data; // For unit tests. } catch (err) { console.log("Error", err); } }; run();
要运行示例,请在命令提示符中键入以下内容。
node transcribe-delete-job.js
此示例代码可在 GitHub 上的此处