【问题标题】:Execute an AWS command in eclipse在eclipse中执行AWS命令
【发布时间】:2019-04-09 19:03:04
【问题描述】:

我通过 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 控制台相同的结果?

【问题讨论】:

    标签: java eclipse shell amazon-ec2


    【解决方案1】:

    您似乎没有获得预期的输出,因为您正在通过未正确解析的 Java 代码传递控制台命令,并且您没有使用 AWS SDKs for Java。 要在 Eclipse 控制台中获得预期的输出,您可以在代码中使用 DescribeSpotPriceHistory Java SDK API 调用 [1]。根据文档,此 API 调用的示例代码 sn-p 如下:

    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 中的 DescribeSpotPriceHistory API 调用[2] 的各种场景的 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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-09-27
      • 1970-01-01
      • 1970-01-01
      • 2017-04-21
      • 1970-01-01
      • 2013-05-30
      • 2018-07-23
      • 1970-01-01
      相关资源
      最近更新 更多