1

I have the following question...I wonder if there is some methods on the EC2 .NET API that allow me to list all the available AMIs(like in the web interface) in a given region. At least their id's. I want to set up a random image and this is the only missed piece from the puzzle.

4

1 に答える 1

4

シンプルにする必要があります。以下は、使用可能または保留状態のすべての AMI を返すコードの抜粋です (独自のフィルターと周囲のtry/catchブロックを追加します)。

AmazonEC2 ec2 = AWSClientFactory.CreateAmazonEC2Client(
    "YOUR_ACCESS_KEY",
    "YOUR_SECRET_KEY"
    );

DescribeImagesRequest request = new DescribeImagesRequest();
request.WithFilter(new Filter[] {
    new Filter().WithName("state").WithValue("available", "pending")});
DescribeImagesResponse ec2Response = ec2.DescribeImages(request);

このクエリは、すべてのパブリック AMI とプライベート AMI を取得します。結果セットの結果には、インスタンスがパブリックかプライベートかを示す属性が含まれているため (例: <Visibility>Private</Visibility>)。

独自の AMI のみが必要な場合は.WithOwner("YOUR_AMAZON_ID")、フィルター定義に を追加します。例えば:

request.WithFilter(new Filter[] {
    new Filter().WithName("state").WithValue("available", pending")})
    .WithOwner("YOUR_AMAZON_ID");

また

request.WithOwner("YOUR_AMAZON_ID");  

詳細については、AWS SDK for .NET ドキュメントを参照してください。左側のツリーで、Amazon/Amazon.EC2.Model/DescribeImagesRequest クラスを選択します。また、DescribeImages API リファレンスFilterには、このリクエストで使用できるすべての名前と可能な値が含まれています。

PS: 明示的にリージョンについて話しているのに、AMI にはリージョンが関連付けられていないため、代わりにインスタンスについて話している可能性があります。この場合、 と呼ばれる同様の要求がありDescribeInstancesます。詳細については、こちら(Amazon/Amazon.EC2.Model/DescribeInstancesRequest クラス) とこちらをご覧ください。

それが役に立てば幸い。

于 2012-07-06T00:11:06.787 に答える