【问题标题】:Amazon DynamoDB scan makes two HTTP requests to server provoking an error, why?Amazon DynamoDB 扫描向服务器发出两个 HTTP 请求引发错误,为什么?
【发布时间】:2020-04-29 23:24:42
【问题描述】:

我有一个 typescript react 应用程序,它使用 DynamoDB 服务来存储数据。 我有管理数据库访问操作的 javascript 中间件。 我在本地开发了 scanputdelete 操作,没有任何错误。 设置生产环境后,出现错误。 如果我在生产环境中本地执行代码,我会遇到同样的错误。

我注意到 scan 操作失败。 实际上它以一种奇怪的方式失败了,因为我看到对 aws 服务器的请求被执行了两次。

为了让我的调试生活变得简单,我在我的 react onClick 方法中调试所有数据库访问,以便将操作与应用程序的生命周期隔离开来。

我注意到了

  • onClick fetch 操作确实被调用了一次(如预期的那样)
  • DynamoDB 扫描操作确实被调用了一次(如预期的那样)
  • scann 的回调被执行了两次(不像预期的那样)
    • 第一次确实获取了数据并且我可以记录它(完美)
    • 第二次调用出错

aws 配置代码

const awsConfig = {
  accessKeyId: process.env.REACT_APP_AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.REACT_APP_AWS_SECRET_ACCESS_KEY,
  region: process.env.REACT_APP_AWS_REGION,
  endpoint: process.env.REACT_APP_AWS_ENDPOINT
}

new AWS.Endpoint(awsConfig.endpoint || '')

AWS.config.update({
  region: awsConfig.region,
  //endpoint: awsConfig.endpoint || '',
  credentials: new AWS.Credentials(awsConfig.accessKeyId || '', awsConfig.secretAccessKey || ''),
  sslEnabled: false,
  maxRetries: 5
})

const dynamoDbClient = new AWS.DynamoDB.DocumentClient()

请注意这部分代码,如果我将端点留在 AWS.config.update 中,Typescript 会给我一个错误,因此我将其设置为不同 new AWS.Endpoint(awsConfig.端点 || '')

Argument of type '{ region: string | undefined; endpoint: string; credentials: Credentials; sslEnabled: false; maxRetries: number; }' is not assignable to parameter of type 'ConfigurationOptions & ConfigurationServicePlaceholders & APIVersions'.
  Object literal may only specify known properties, and 'endpoint' does not exist in type 'ConfigurationOptions & ConfigurationServicePlaceholders & APIVersions'.ts(2345)

aws 访问代码

const searchParams = { TableName: 'table' }

let dataItems: DateFields[] = []

await dynamoDbClient
  .scan(searchParams, (error: any, data: any) => {
    if (error) {
      console.error(`Error fetchCategories: `, JSON.stringify(error, null, 2))
      return
    }
    dataItems = data.Items
  })
  .promise()

出现以下错误

Error fetchCategories:  {
  "message": "The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.",
  "code": "InvalidSignatureException",
  "time": "2020-04-29T08:39:14.449Z",
  "requestId": "xxxxxxxxxxxxxxxxxxx",
  "statusCode": 400,
  "retryable": false,
  "retryDelay": 15.082420221534553
}

{"__type":"com.amazon.coral.service#InvalidSignatureException","message":"The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details."}

知道为什么会这样吗?非常感谢

【问题讨论】:

    标签: node.js typescript amazon-web-services http amazon-dynamodb


    【解决方案1】:

    问题在于 aws 访问代码,为了解决这个问题,我更新了 DynamoDB 客户端,如下所示:

      try {
        const data = await dynamoDbClient.scan(params).promise()
        return data.Items
      } catch(error) {
        logError(operation, error)
      }
    

    代码中的这种更改可以防止:

    • 要执行两次的回调
    • 服务器回答错误,这可能反映了 AWS 安全机制阻止了多个相同和相应的服务器查询。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-10-24
      • 1970-01-01
      • 2017-05-19
      相关资源
      最近更新 更多