Javascript通过不同地区的cognito访问dynamodb

Javascript access to dynamodb via cognito with different regions

本文关键字:cognito 访问 dynamodb 地区 Javascript      更新时间:2023-09-26

我所在的cognito地区是东京(ap-Northheast-1),而DynamoDB则设在悉尼(ap-southeast-2)。我的问题是,如果我配置

AWS.config.region = 'ap-northeast-1';

然后我可以访问凭据,但AWS假设我的数据库在同一个区域,我得到:

POST https://dynamodb.ap-northeast-1.amazonaws.com/ 400 (Bad Request)

如果我配置

AWS.config.region = "ap-southeast-2";

然后我得到:

OPTIONS https://cognito-identity.ap-southeast-2.amazonaws.com/ net::ERR_NAME_NOT_RESOLVED

大概是因为找不到身份。

现在Cognito只在3个地区提供,没有一个与我拥有的任何资源相对应。

那么,我如何同时使用这两种方法呢?

您可以为Cognito的区域设置SDK的全局配置,并使用特定于服务的配置来实例化其他区域的服务客户端。

//Set global region
AWS.config.region = 'ap-northeast-1';
//Get identity and credentials from Cognito
//Get dynamo db region specific client
var dynamoDB = new AWS.DynamoDB({region: "ap-southeast-2"});