Search code examples
dynamodb-queriesaws-sdk-jsamazon-dynamodb

DynamoDB ExclusiveStartKey error The provided starting key is invalid


Well hello fellas, i'm starting in dynamodb and i have some missunders when i want to use the ExclusiveStartKey.currently im working with the GSI and heres is how i have the params for the query

{
   TableName: 'Search',
   IndexName: 'GSI1',
   ExclusiveStartKey: {
     GSI1PK: { S: '8a2bb021182ffff' },
     GSI1SK: { S: '5#182854f0-c4ea-39c7-a3f5-4b0b0d947cea' }
   },
   KeyConditionExpression: 'GSI1PK = :gsiHk AND begins_with(GSI1SK, :entityType)',
   ExpressionAttributeValues: { ':gsiHk': { S: '8a2bb021182ffff' }, ':entityType': {S:'5'}},
  Limit: 500
 }

and this returns me an error

ValidationException: The provided starting key is invalid

Is this the correct way to use it or how can i fix it ??


Solution

  • It's uncommon to explicitly set an ExclusiveStartKey

    From the docs:

    ExclusiveStartKey The primary key of the first item that this operation will evaluate. Use the value that was returned for LastEvaluatedKey in the previous operation.

    so the normal use of Query, given it's 1MB read limit is in a loop as shown in the following pseudo-code

    do 
      result=Query(parms);
      //process results
      parms.ExclusiveStartKey = results.LastEvaluatedKey;
    until results.LastEvaluatedKey is null; 
    

    I supposed there's no reason you couldn't explicitly set it, but I don't see that the format is documented anywhere. You'll have to examine what's returned in LastEvaluatedKey