0

我在 Node.js 应用程序中运行 AWS SDK V3 Translate Client 来翻译句子。这是我的代码:

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

const client = new TranslateClient({ 
  region: "ap-southeast-1",
  credentials: {
    accessKeyId: process.env.AWS_ACCESS_KEY_ID, 
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
  }
});

const params = {
  Text: "StackOverflow is very good.",
  SourceLanguageCode: "en",
  TargetLanguageCode: "id"
};

const command = new CreateParallelDataCommand(params);

function translate() {
  return client.send(command)
    .then((data) => {
      console.log(data);
    })
    .catch((reason) => {
      console.log(reason);
    })
}

translate();

我已按照文档中的说明进行操作。

但是,结果如下。

TypeError: Cannot read property 'byteLength' of undefined
    at Object.fromArrayBuffer (\projectDir\node_modules\@aws-sdk\util-buffer-from\dist-cjs\index.js:6:60)
    at castSourceData (\projectDir\node_modules\@aws-sdk\hash-node\dist-cjs\index.js:29:31)
    at Hash.update (\projectDir\node_modules\@aws-sdk\hash-node\dist-cjs\index.js:12:26)
    at hmac (\projectDir\node_modules\@aws-sdk\signature-v4\dist-cjs\credentialDerivation.js:36:10)
    at Object.getSigningKey (\projectDir\node_modules\@aws-sdk\signature-v4\dist-cjs\credentialDerivation.js:11:29)
    at SignatureV4.getSigningKey (\projectDir\node_modules\@aws-sdk\signature-v4\dist-cjs\SignatureV4.js:139:39)
    at SignatureV4.signRequest (\projectDir\node_modules\@aws-sdk\signature-v4\dist-cjs\SignatureV4.js:98:73)
    at async \projectDir\node_modules\@aws-sdk\middleware-signing\dist-cjs\middleware.js:13:18
    at async StandardRetryStrategy.retry (\projectDir\node_modules\@aws-sdk\middleware-retry\dist-cjs\StandardRetryStrategy.js:51:46)
    at async \projectDir\node_modules\@aws-sdk\middleware-logger\dist-cjs\loggerMiddleware.js:6:22 {
  '$metadata': { attempts: 1, totalRetryDelay: 0 }
}

这个问题的原因是什么,我该如何解决?

4

1 回答 1

0

我找到了解决方案。我们可以使用TranslateTextCommandfrom@aws-sdk/client-translate而不是CreateParallelDataCommand构建命令。这些类使用不同的输入参数格式。我在示例程序中使用的参数仅适用于TranslateTextCommand. 因此,修复如下。

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

// ...

const params = {
  Text: "StackOverflow is very good.",
  SourceLanguageCode: "en",
  TargetLanguageCode: "id"
};

const command = new TranslateTextCommand(params)

// ...
于 2021-11-08T05:50:11.967 回答