亚马逊现货价格历史 – Java代码

应该可以获得过去90天的AWS Spot Price历史记录.使用
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安装的.

提前致谢!

最佳答案 您确实没有按预期使用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());
点赞