如何在Step函数的GetHistoryExecution中使用nextToken?


不见你
2025-03-12 05:19:20 (27天前)
  1. 我试图使用lambda函数获取所有执行历史记录并将其存储到DynamoDB。该函数返回大约20个执行和一个名为NextToken的字符串值,该值将在下一个...

2 条回复
  1. 0# Frui tenebris、 | 2019-08-31 10-32



    需要在parms中使用nextToken传递给下一次调用getExecutionHistory。您可以递归调用此函数,直到所有令牌都用完为止。通过Cloud watch获取日志时遇到类似情况。



    递归获取历史记录的示例,



    将getExecutionHistory包装到promise中并添加到不同的JS文件(比如说writer.js)然后你的主index.js文件可以像这样调用那个函数,




    1. // writer.js which writes record to Dynamodb
      // returns promise
      // when history is fetched , dynamodb will be inserted and it will resolve dataexecution which has nextToken

    2. module.exports.get = function(fwdtoken) {

    3. if (fwdtoken) parms.nextToken= fwdtoken;
    4. return new Promise ( (resolve, reject)=>{
    5. stepfunctions.getExecutionHistory(params, function(err, dataExecution) {
    6.     if (err){
    7.        reject(err.stack)
    8.     } 
    9.     else {
    10.         const params2 = {
    11.             TableName: table,
    12.             Item: {
    13.                 id: executionARN,
    14.                 execution_history: dataExecution
    15.             }
    16.         };
    17.         dynamoDb.put(params2).promise();
    18.     resolve(dataExecution)
    19.     }
    20.   });   
    21. })    
    22. };

    23. //This goes in main logic
      // Invokes getAllLogs recursilvely

    24. var writer = require(‘./writer’);
      var fwdtoken;

    25. function getAllLogs(fwdtoken, fetchCount) {
      fetchCount = fetchCount || 0;
      if (fetchCount > 40) {
      throw new Error(“Fetched too many times.”);
      }
      return new Promise( (resolve) => {
      writer.get(fwdtoken).then( function consolidate( dataExecution ) {
      resolve( dataExecution );
      });
      })
      .then(function ( dataExecution ) {
      if (dataExecution.nextForwardToken) {
      fwdtoken = dataExecution.nextForwardToken;
      getAllLogs(fwdtoken, fetchCount+ 1)
      }
      else
      return fwdtoken
      });
      }
      getAllLogs(fwdtoken, 0);

    26. </code>

登录 后才能参与评论