Google cloud platform 使用谷歌云翻译,只需一个令牌,不需要密钥文件

Google cloud platform 使用谷歌云翻译,只需一个令牌,不需要密钥文件,google-cloud-platform,google-translate,google-translation-api,Google Cloud Platform,Google Translate,Google Translation Api,当使用谷歌云翻译API时,我不想使用生成的密钥文件()。我们使用部署到某个随机主机的docker容器。出于明显的安全原因,我无法将密钥文件添加到要编译到docker容器的源代码中,并且我不想将密钥文件复制到部署(或可能部署)容器的每个主机上 通常API都可以使用令牌,我可以使用容器管理环境变量设置令牌,当我需要扩展容器或切换主机等时,我可以将令牌应用到容器的所有实例。google是否提供这种设置?我可以使用REST请求,不需要任何sdk 在我看来,唯一的选择是在gitlab中添加keyfile

当使用谷歌云翻译API时,我不想使用生成的密钥文件()。我们使用部署到某个随机主机的docker容器。出于明显的安全原因,我无法将密钥文件添加到要编译到docker容器的源代码中,并且我不想将密钥文件复制到部署(或可能部署)容器的每个主机上

通常API都可以使用令牌,我可以使用容器管理环境变量设置令牌,当我需要扩展容器或切换主机等时,我可以将令牌应用到容器的所有实例。google是否提供这种设置?我可以使用REST请求,不需要任何sdk

在我看来,唯一的选择是在gitlab中添加keyfile json作为环境变量,然后将该文件构建到容器中


或者有没有其他方法可以只使用令牌而不使用密钥文件来使用google translate API?

google的SDK可以隐式使用默认服务帐户()

编辑:这可能会解决您的问题:

此外:

下面是代码示例:

json := os.Getenv("GOOGLE_APPLICATION_CREDENTIALS_JSON") // `{"type": "service_account", "project_id": "my-project", ...}`
ctx := context.Background()
jwtConfig, err := google.JWTConfigFromJSON([]byte(json), datastore.ScopeDatastore)
if err != nil {
    ...
}
ts := jwtConfig.TokenSource(ctx)
datastoreClient, err := datastore.NewClient(ctx, projectID, option.WithTokenSource(ts))
编辑2:

也检查


隐马尔可夫模型。。这太麻烦了。或者我可以直接将json传递到GOOGLE_应用程序_凭据中,而不必创建文件吗?然后我可以将json从构建环境传递到docker容器并完成。规范说“首先,ADC检查环境变量GOOGLE_APPLICATION_CREDENTIALS是否设置。如果设置了该变量,ADC将使用该变量指向的服务帐户文件。下一节描述如何设置环境变量。”我同意这不是100%最优,但这是一件相当一次性的事情。这可能会有帮助:我也将它附加到我的原始答案中。
Loading credentials from environment variables
Instead of loading credentials from a key file, you can also provide them using an environment variable and the GoogleAuth.fromJSON() method. This is particularly convenient for systems that deploy directly from source control (Heroku, App Engine, etc).

Start by exporting your credentials:

$ export CREDS='{
  "type": "service_account",
  "project_id": "your-project-id",
  "private_key_id": "your-private-key-id",
  "private_key": "your-private-key",
  "client_email": "your-client-email",
  "client_id": "your-client-id",
  "auth_uri": "https://accounts.google.com/o/oauth2/auth",
  "token_uri": "https://accounts.google.com/o/oauth2/token",
  "auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
  "client_x509_cert_url": "your-cert-url"
}'
Now you can create a new client from the credentials:

const {auth} = require('google-auth-library');

// load the environment variable with our keys
const keysEnvVar = process.env['CREDS'];
if (!keysEnvVar) {
  throw new Error('The $CREDS environment variable was not found!');
}
const keys = JSON.parse(keysEnvVar);

async function main() {
  // load the JWT or UserRefreshClient from the keys
  const client = auth.fromJSON(keys);
  client.scopes = ['https://www.googleapis.com/auth/cloud-platform'];
  const url = `https://dns.googleapis.com/dns/v1/projects/${keys.project_id}`;
  const res = await client.request({url});
  console.log(res.data);
}

main().catch(console.error);