【问题标题】:Amazon Spot Price History - Java Code亚马逊现货价格历史 - Java 代码
【发布时间】:2012-09-17 18:32:32
【问题描述】:

应该可以获取过去 90 天的 AWS 现货价格历史记录。使用 Java SDK 时,可以创建一个查询来获取一些历史记录,但由于这个列表太长,他们将其拆分。使用令牌,您应该能够获得列表的下一部分,直到您收到整个列表。

问题是使用给定的令牌我还无法检索到列表的第一部分。在搜索互联网时,很明显我对这个令牌的理解是正确的。

    // Create the AmazonEC2Client object so we can call various APIs.
    AmazonEC2 ec2 = new AmazonEC2Client(credentials);

    // Get the spot price history
    DescribeSpotPriceHistoryResult result = ec2.describeSpotPriceHistory();

    // Print first part of list
    for (int i = 0; i < result.getSpotPriceHistory().size(); i++) {
        System.out.println(result.getSpotPriceHistory().get(i));
    }

    result = result.withNextToken(result.getNextToken());

    // Print second part of list
    for (int i = 0; i < result.getSpotPriceHistory().size(); i++) {
            System.out.println(result.getSpotPriceHistory().get(i));
    }

结果的“nextToken”没有改变。任何想法我做错了什么? SDK中是否存在错误?我是通过 Eclipse 安装的。

提前致谢!

【问题讨论】:

    标签: java sdk amazon-ec2


    【解决方案1】:

    您确实没有按预期使用 API - 您需要使用从 DescribeSpotPriceHistoryResult 检索到的 nextToken 重新提交 DescribeSpotPriceHistoryRequest(诚然,您也可以在后者上设置 nextToken 有点令人困惑, 猜测理想情况下它应该只是一个内部方法),例如:

    // Create the AmazonEC2Client object so we can call various APIs.
    AmazonEC2 ec2 = new AmazonEC2Client(credentials);
    
    // Get the spot price history
    String nextToken = "";
    do {
        // Prepare request (include nextToken if available from previous result)
        DescribeSpotPriceHistoryRequest request = new DescribeSpotPriceHistoryRequest()
                .withNextToken(nextToken);
    
        // Perform request
        DescribeSpotPriceHistoryResult result = ec2
                .describeSpotPriceHistory(request);
        for (int i = 0; i < result.getSpotPriceHistory().size(); i++) {
            System.out.println(result.getSpotPriceHistory().get(i));
        }
    
        // 'nextToken' is the string marking the next set of results returned (if any), 
        // it will be empty if there are no more results to be returned.            
        nextToken = result.getNextToken();
    
    } while (!nextToken.isEmpty());
    

    【讨论】:

      猜你喜欢
      • 2011-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-22
      • 1970-01-01
      • 2013-01-24
      相关资源
      最近更新 更多