aws-sdk-js DynamoDB抛出错误:在Request.VALIDATE_REGION中缺少配置中的凭据

时间:2017-04-27 18:12:14

标签: javascript amazon-web-services react-native amazon-dynamodb aws-sdk

我正在尝试在react-native上使用DynamoDB执行batchGetItem,并且它会抛出以下错误:

Error: Missing credentials in config
    at Request.VALIDATE_REGION (aws-sdk-react-native.js:10326)
    at Request.callListeners (aws-sdk-react-native.js:10112)
    at Request.emit (aws-sdk-react-native.js:10084)
    at Request.emit (aws-sdk-react-native.js:14062)
    at Request.transition (aws-sdk-react-native.js:13413)
    at AcceptorStateMachine.runTo (aws-sdk-react-native.js:14199)
    at Request.runTo (aws-sdk-react-native.js:13788)
    at Request.send (aws-sdk-react-native.js:13752)
    at features.constructor.makeUnauthenticatedRequest (aws-sdk-react-native.js:6689)
    at features.constructor.getId (aws-sdk-react-native.js:9118) "ConfigError: Missing credentials in config
    at Request.VALIDATE_REGION (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:89572:43)
    at Request.callListeners (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:89495:17)
    at Request.emit (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:89470:10)
    at Request.emit (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:92539:15)
    at Request.transition (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:92283:11)
    at AcceptorStateMachine.runTo (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:92642:13)
    at Request.runTo (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:92362:16)
    at Request.send (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:92352:11)
    at features.constructor.makeUnauthenticatedRequest (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:87518:31)
    at features.constructor.getId (http://localhost:8081/index.ios.bundle?platform=ios&dev=true&minify=false:88786:17)"

我已经对用户进行了身份验证,用户具有IAM角色和执行batchGetItem的权限。

以下是相关代码的片段:

    import { Config, CognitoIdentityCredentials, DynamoDB } from 'aws-sdk/dist/aws-sdk-react-native';
    import { AuthenticationDetails, CognitoUser, CognitoUserPool, CognitoUserAttribute } from 'react-native-aws-cognito-js';

    const appConfig = {
      region: 'us-east-1',
      IdentityPoolId: 'us-east-1:foo-bar',
      UserPoolId: 'us-east-1_foo-bar',
      ClientId: '1f2o3o4b5a6r',
    };

    Config.region = appConfig.region;

    class AWS_testpage extends Component {

      constructor(props) {
        super(props);

        this.state = {
          username: 'someUsername',
          password: 'somePassword',
        };

    login() {
        const { username, password } = this.state;

        const authenticationData = {
          Username: username,
          Password: password,
        };

        const authenticationDetails = new AuthenticationDetails(authenticationData);

        const poolData = {
          UserPoolId: appConfig.UserPoolId,
          ClientId: appConfig.ClientId
        };

        const userPool = new CognitoUserPool(poolData);
        console.log('USERPOOOL', userPool);

        const userData = {
          Username: username,
          Pool: userPool
        };

        const cognitoUser = new CognitoUser(userData);

        cognitoUser.authenticateUser(authenticationDetails, {
          onSuccess: (result) => {
            console.log(`ACCESS TOKEN =====> ${result.getAccessToken().getJwtToken()}`);
            Config.credentials = new CognitoIdentityCredentials({
              IdentityPoolId: appConfig.IdentityPoolId,
              Logins: {
                [`cognito-idp.${appConfig.region}.amazonaws.com/${appConfig.UserPoolId}`]: result.getIdToken().getJwtToken()
              }
            });

            alert('Success');
          },
          onFailure: (error) => {
            alert(error);
          },
          newPasswordRequired: () => {
            console.log('NEW PASSWORD REQUIRED');
          }
        });
      }

    onDynamoPress() {
        const dynamodb = new DynamoDB(Config);
        console.log('DYNAMODB', dynamodb);


        const paramsDB = {
        RequestItems: {
        Readings: {
        Keys: [{
          hwid: {
            S: 'ThisIsFake'
          },
          timestamp: {
            S: 'Now'
          }
        }],
        ProjectionExpression: 'received' // this is what element you want back
      }
    }
  };

  dynamodb.batchGetItem(paramsDB, (err, data) => {
    if (err) console.log(err, err.stack); // an error occurred
    else console.log(data);           // successful response

  });
}

如果我在进行身份验证之前发出batchGetItem请求,则会收到以下错误:

Error: Missing credentials in config
    at credError (aws-sdk-react-native.js:7490)
    at Config.getCredentials (aws-sdk-react-native.js:7529)
    at Request.VALIDATE_CREDENTIALS (aws-sdk-react-native.js:10313)
    at Request.callListeners (aws-sdk-react-native.js:10108)
    at Request.emit (aws-sdk-react-native.js:10084)
    at Request.emit (aws-sdk-react-native.js:14062)
    at Request.transition (aws-sdk-react-native.js:13413)
    at AcceptorStateMachine.runTo (aws-sdk-react-native.js:14199)
    at Request.runTo (aws-sdk-react-native.js:13788)
    at Request.send (aws-sdk-react-native.js:13752) "CredentialsError: Missing credentials in config

1 个答案:

答案 0 :(得分:0)

我创建了一个新的联合身份库,但它确实有效!