DescribeSnapshots 搭配 AWS SDK或 使用 CLI - Amazon Elastic Compute Cloud

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

DescribeSnapshots 搭配 AWS SDK或 使用 CLI

下列程式碼範例示範如何使用 DescribeSnapshots

CLI
AWS CLI

範例 1:描述快照

下列 describe-snapshots 範例會描述指定的快照。

aws ec2 describe-snapshots \ --snapshot-ids snap-1234567890abcdef0

輸出:

{ "Snapshots": [ { "Description": "This is my snapshot", "Encrypted": false, "VolumeId": "vol-049df61146c4d7901", "State": "completed", "VolumeSize": 8, "StartTime": "2019-02-28T21:28:32.000Z", "Progress": "100%", "OwnerId": "012345678910", "SnapshotId": "snap-01234567890abcdef", "Tags": [ { "Key": "Stack", "Value": "test" } ] } ] }

如需詳細資訊,請參閱 Amazon 使用者指南 中的 Amazon EBS快照 EC2

範例 2:根據篩選條件描述快照

下列describe-snapshots範例使用篩選條件,將結果範圍擴展至您 AWS 帳戶擁有且處於 pending 狀態的快照。此範例使用 --query 參數僅顯示快照IDs和快照啟動的時間。

aws ec2 describe-snapshots \ --owner-ids self \ --filters Name=status,Values=pending \ --query "Snapshots[*].{ID:SnapshotId,Time:StartTime}"

輸出:

[ { "ID": "snap-1234567890abcdef0", "Time": "2019-08-04T12:48:18.000Z" }, { "ID": "snap-066877671789bd71b", "Time": "2019-08-04T02:45:16.000Z }, ... ]

下列 describe-snapshots 範例會使用篩選條件,將結果範圍限制為從指定磁碟區建立的快照。此範例使用 --query 參數僅顯示快照 IDs。

aws ec2 describe-snapshots \ --filters Name=volume-id,Values=049df61146c4d7901 \ --query "Snapshots[*].[SnapshotId]" \ --output text

輸出:

snap-1234567890abcdef0 snap-08637175a712c3fb9 ...

如需使用篩選條件的其他範例,請參閱 Amazon EC2使用者指南 中的列出和篩選資源

範例 3:根據標籤描述快照

下列 describe-snapshots 範例會使用標籤篩選條件,將結果範圍設定為具有標籤 Stack=Prod 的快照。

aws ec2 describe-snapshots \ --filters Name=tag:Stack,Values=prod

如需 describe-snapshots 的輸出範例,請參閱範例 1。

如需使用標籤篩選條件的其他範例,請參閱 Amazon EC2使用者指南 中的使用標籤

範例 4:根據年齡描述快照

下列describe-snapshots範例使用JMESPath表達式來描述 AWS 帳戶在指定日期之前建立的所有快照。它只會顯示快照 IDs。

aws ec2 describe-snapshots \ --owner-ids 012345678910 \ --query "Snapshots[?(StartTime<='2020-03-31')].[SnapshotId]"

如需使用篩選條件的其他範例,請參閱 Amazon EC2使用者指南 中的列出和篩選資源

範例 5:僅檢視封存的快照

以下 describe-snapshots 範例只列出儲存在封存層中的快照。

aws ec2 describe-snapshots \ --filters "Name=storage-tier,Values=archive"

輸出:

{ "Snapshots": [ { "Description": "Snap A", "Encrypted": false, "VolumeId": "vol-01234567890aaaaaa", "State": "completed", "VolumeSize": 8, "StartTime": "2021-09-07T21:00:00.000Z", "Progress": "100%", "OwnerId": "123456789012", "SnapshotId": "snap-01234567890aaaaaa", "StorageTier": "archive", "Tags": [] }, ] }

如需詳細資訊,請參閱《Amazon Elastic Compute Cloud 使用者指南》中的檢視封存的快照

PowerShell
適用於 的工具 PowerShell

範例 1:此範例說明指定的快照。

Get-EC2Snapshot -SnapshotId snap-12345678

輸出:

DataEncryptionKeyId : Description : Created by CreateImage(i-1a2b3c4d) for ami-12345678 from vol-12345678 Encrypted : False KmsKeyId : OwnerAlias : OwnerId : 123456789012 Progress : 100% SnapshotId : snap-12345678 StartTime : 10/23/2014 6:01:28 AM State : completed StateMessage : Tags : {} VolumeId : vol-12345678 VolumeSize : 8

範例 2:此範例描述具有 'Name' 標籤的快照。

Get-EC2Snapshot | ? { $_.Tags.Count -gt 0 -and $_.Tags.Key -eq "Name" }

範例 3:此範例描述具有值為 '' 之 'NameTestValue' 標籤的快照。

Get-EC2Snapshot | ? { $_.Tags.Count -gt 0 -and $_.Tags.Key -eq "Name" -and $_.Tags.Value -eq "TestValue" }

範例 4:此範例說明您的所有快照。

Get-EC2Snapshot -Owner self
  • 如需API詳細資訊,請參閱 AWS Tools for PowerShell Cmdlet 參考 DescribeSnapshots中的 。

Rust
SDK for Rust
注意

還有更多 。 GitHub尋找完整範例,並了解如何在 AWS 程式碼範例儲存庫中設定和執行。

顯示快照的狀態。

async fn show_state(client: &Client, id: &str) -> Result<(), Error> { let resp = client .describe_snapshots() .filters(Filter::builder().name("snapshot-id").values(id).build()) .send() .await?; println!( "State: {}", resp.snapshots().first().unwrap().state().unwrap().as_ref() ); Ok(()) }
async fn show_snapshots(client: &Client) -> Result<(), Error> { // "self" represents your account ID. // You can list the snapshots for any account by replacing // "self" with that account ID. let resp = client.describe_snapshots().owner_ids("self").send().await?; let snapshots = resp.snapshots(); let length = snapshots.len(); for snapshot in snapshots { println!( "ID: {}", snapshot.snapshot_id().unwrap_or_default() ); println!( "Description: {}", snapshot.description().unwrap_or_default() ); println!("State: {}", snapshot.state().unwrap().as_ref()); println!(); } println!(); println!("Found {} snapshot(s)", length); println!(); Ok(()) }
  • 如需API詳細資訊,請參閱 DescribeSnapshots 中的 AWS SDK for Rust API參考

如需開發人員指南和程式碼範例的完整清單 AWS SDK,請參閱 使用 建立 Amazon EC2 資源 AWS SDK。本主題也包含有關入門的資訊,以及先前SDK版本的詳細資訊。