Amazon Spot Price History - Java Code

AWS Spot Price usage history for the last 90 days must be available. Using the Java SDK, you can create a query to get some history, but since this list is so long that it laid out it. Using the token, you can get the next part of the list, en until you get the whole list.

The problem is that using this token, I still could not get more than the first part of the list. When searching the Internet, it became clear that my understanding of this token is right.

// 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" of the result does not change. Any ideas what I'm doing wrong? Is there an error in the SDK? I installed it through Eclipse.

Thanks in advance!

+4
source share
1 answer

You really don't use the API, as expected, you need to resubmit DescribeSpotPriceHistoryRequest using nextToken extracted from DescribeSpotPriceHistoryResult (admittedly, it's a little confusing that you can set nextToken on the latter, suppose ideally this should be only an internal method) eg:

 // 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()); 
+2
source

Source: https://habr.com/ru/post/1436305/


All Articles