微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无服务器离线 + DynamoDb 本地问题

如何解决无服务器离线 + DynamoDb 本地问题

我最近一直在玩离线无服务器和本地 dynamodb。 当我使用 websockets 时 - 一切运行良好。 然后,我决定将协议更改为 http。不知道这有什么关系,但这是我在代码中所做的唯一更改。

现在,我收到来自 dynamodb 的错误包括 db 命令的输出):

Serverless: [AWS dynamodb 400 0.034s 0 retries] query({
  TableName: 'DEVICE_TABLE_DEV',KeyConditionExpression: '#id = :id',ExpressionAttributeNames: { '#id': 'deviceid' },ExpressionAttributeValues: { ':id': { S: 'a1173b07-af44-450b-b709-902c0b011df2' } }
})
ResourceNotFoundException: Cannot do operations on a non-existent table

我已经用命令检查了现有的表:

aws dynamodb list-tables --endpoint-url http://localhost:8042

我明白了,该表存在:

{
    "TableNames": [
        "DEVICE_TABLE_DEV","USER_TABLE_DEV"
    ]
}

然后我打印了 dynamodb 客户端,我看到,根据文档提供的选项似乎是正确的:

DocumentClient {
  options: {
    region: 'localhost',endpoint: 'http://localhost:8042',attrValue: 'S6'
  },service: Service {
    config: Config {
      credentials: [SharedIniFileCredentials],credentialProvider: [CredentialProviderChain],region: 'localhost',logger: [CLI],apiVersions: {},apiVersion: null,httpOptions: [Object],maxRetries: undefined,maxRedirects: 10,paramValidation: true,sslEnabled: true,s3ForcePathStyle: false,s3BucketEndpoint: false,s3disableBodySigning: true,s3UsEast1RegionalEndpoint: 'legacy',s3UseArnRegion: undefined,computeChecksums: true,convertResponseTypes: true,correctClockSkew: false,customUserAgent: null,dynamoDbCrc32: true,systemClockOffset: 0,signatureversion: null,signatureCache: true,retryDelayOptions: {},useAccelerateEndpoint: false,clientSideMonitoring: false,endpointdiscoveryEnabled: undefined,endpointCacheSize: 1000,hostPrefixEnabled: true,stsRegionalEndpoints: 'legacy'
    },endpoint: Endpoint {
      protocol: 'http:',host: 'localhost:8042',port: 8042,hostname: 'localhost',pathname: '/',path: '/',href: 'http://localhost:8042/'
    },_events: { apiCallAttempt: [Array],apiCall: [Array] },MONITOR_EVENTS_BUBBLE: [Function: EVENTS_BUBBLE],CALL_EVENTS_BUBBLE: [Function: CALL_EVENTS_BUBBLE],_clientId: 1
  },attrValue: 'S6'
}

这就是我创建客户端的方式:

const dynamo = new AWS.DynamoDB.DocumentClient({ region: 'localhost',endpoint: 'http://localhost:8042'});

更新:添加我的 serverless.yml dynamodb 部分配置:

dynamodb:
  # If you only want to use DynamoDB Local in some stages,declare them here
    stages:
      - local
    start:
      port: 8042
      inMemory: true
      heapInitial: 200m
      heapMax: 1g
      migrate: true
      seed: true
      convertEmptyValues: true
    # Uncomment only if you already have a DynamoDB running locally
      noStart: true

希望得到任何这方面的建议。

解决方法

您不应使用 region: 'localhost' 初始化 DynamoDB。

要么将其删除,要么将其更改为适当的区域,例如:

const dynamo = new AWS.DynamoDB.DocumentClient({ region: 'eu-west-2',endpoint: 'http://localhost:8042'});

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。