Multicloud:从 AWS Lambda (Nodejs) 验证 Googe 云翻译客户端

问题描述 投票:0回答:0

问题

我正在尝试使用 Nodejs 和无服务器框架从我的 AWS Lambda 访问 Google Cloud 服务(Cloud Translate API)。当我使用 Google 服务帐户密钥时,该系统已经完美运行,因此可以验证这两个云服务是否正常运行。

但是,我正在尝试遵循最佳实践并使用 Google 的 Federated Workforce ID 而不是服务帐户密钥。 (文档)。

但是,我收到一个错误: FetchError: request to http://169.254.169.254/latest/meta-data/iam/security-credentials failed, reason: connect ETIMEDOUT 169.254.169.254:80

我已经多次按照文档中的说明进行操作,包括创建工作场所池和下载客户端配置文件。我将环境变量设置为配置文件:
GOOGLE_APPLICATION_CREDENTIALS: ./clientLibraryConfig-fq-aws-apis.json

Google Auth 获取凭据文件(我可以通过在常量“客户端”上运行 console.log 来查看),并在 auth.getProjectId(); 中检索我的 projectId。

但是在启动 TranslationServiceClient 时,我得到了这个:

错误

“errorMessage”:“请求http://169.254.169.254/latest/meta-data/iam/security-credentials失败,原因:连接ETIMEDOUT 169.254.169.254:80”,

代码

"use strict";

const { GoogleAuth } = require("google-auth-library");
const { TranslationServiceClient } = require("@google-cloud/translate");

//////////
// This function gets translation from Google
//////////

const getTranslations = async (originalClipArray, translateTo) => {
  // G Translate params
  // const projectId = "rw-frequency";
  const location = "global";

  const auth = new GoogleAuth({
    scopes: 'https://www.googleapis.com/auth/cloud-platform'
  });
  
  const client = await auth.getClient();

  const projectId = await auth.getProjectId();


  const translationClient = new TranslationServiceClient()

  console.log("past translationserviceclient constructor");


  // Build the params for the translate request
  const request = {
    parent: `projects/${projectId}/locations/${location}`,
    contents: originalClipArray,
    mimeType: "text/plain", // mime types: text/plain, text/html
    targetLanguageCode: translateTo,
  };

  // Call Google client
  // try {
  const response = await translationClient.translateText(request);
  console.log(`response`);
  console.dir(response);
  return response;
  // } catch (error) {
  //   console.log(`Google translate error raised:`);
  //   console.log(error);
  // }
};

module.exports.getTranslations = getTranslations;

google-cloud-platform aws-lambda federated-identity google-auth-library google-cloud-translate
© www.soinside.com 2019 - 2024. All rights reserved.