在Eclipse中执行AWS命令

时间:2018-11-06 11:30:03

标签: java eclipse shell amazon-ec2

我通过eclipse执行EC2命令,例如:

public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    String spot = "aws ec2 describe-spot-price-history --instance-types"
            + " m3.medium  --product-description \"Linux/UNIX (Amazon VPC)\"";
    System.out.println(spot);
    Runtime runtime = Runtime.getRuntime();
    final Process process = runtime.exec(spot);

    //********************
            InputStreamReader isr = new InputStreamReader(process.getInputStream());
            BufferedReader buff = new BufferedReader (isr);

            String line;
            while((line = buff.readLine()) != null)
                System.out.print(line);

}

eclipse控制台中的结果是:

aws ec2 describe-spot-price-history --instance-types m3.medium  --product-description "Linux/UNIX (Amazon VPC)"
{    "SpotPriceHistory": []}

但是,当我在shell中执行相同的命令(aws ec2 describe-spot-price-history --instance-types m3.medium --product-description "Linux/UNIX (Amazon VPC)")时,会得到不同的结果。

"Timestamp": "2018-09-07T17:52:48.000Z", 
        "AvailabilityZone": "us-east-1f", 
        "InstanceType": "m3.medium", 
        "ProductDescription": "Linux/UNIX", 
        "SpotPrice": "0.046700"
    }, 
    {
        "Timestamp": "2018-09-07T17:52:48.000Z", 
        "AvailabilityZone": "us-east-1a", 
        "InstanceType": "m3.medium", 
        "ProductDescription": "Linux/UNIX", 
        "SpotPrice": "0.047000"
    }

我的问题是:如何在Eclipse控制台中获得与Shell控制台相同的结果?

1 个答案:

答案 0 :(得分:1)

您似乎未获得预期的输出,因为您正在通过未正确解析的Java代码传递控制台命令,而没有使用Java的AWS开发工具包。 为了在Eclipse控制台中获得预期的输出,可以在代码中利用DescribeSpotPriceHistory Java SDK API调用[1]。根据文档,此API调用的示例代码片段如下:

AmazonEC2 client = AmazonEC2ClientBuilder.standard().build();
DescribeSpotPriceHistoryRequest request = new DescribeSpotPriceHistoryRequest().withEndTime(new Date("2014-01-06T08:09:10"))
        .withInstanceTypes("m1.xlarge").withProductDescriptions("Linux/UNIX (Amazon VPC)").withStartTime(new Date("2014-01-06T07:08:09"));
DescribeSpotPriceHistoryResult response = client.describeSpotPriceHistory(request);

此外,您可以通过Java [2]来访问包含这个网站的Java文件示例,其中包含各种情况的Java文件示例。 有关DescribeSpotPriceHistory的更多详细信息,请参考官方文档[3]。

参考

[1]。 https://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/ec2/AmazonEC2.html#describeSpotPriceHistory-com.amazonaws.services.ec2.model.DescribeSpotPriceHistoryRequest-

[2]。 https://www.programcreek.com/java-api-examples/index.php?api=com.amazonaws.services.ec2.model.SpotPrice

[3]。 https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeSpotPriceHistory.html