将 DescribeSnapshots 与 AWS SDK 或 CLI 配合使用
以下代码示例演示如何使用 DescribeSnapshots。
- CLI
-
- AWS CLI
-
示例 1:描述快照
以下
describe-snapshots示例描述了指定的快照。aws ec2 describe-snapshots \ --snapshot-idssnap-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 EC2 用户指南》中的 Amazon EBS 快照。
示例 2:描述基于筛选器的快照
以下
describe-snapshots示例使用筛选器,将结果范围限定为 AWS 账户拥有的处于pending状态的快照。示例使用--query参数仅显示快照 ID 和快照的启动时间。aws ec2 describe-snapshots \ --owner-idsself\ --filtersName=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参数仅显示快照 ID。aws ec2 describe-snapshots \ --filtersName=volume-id,Values=049df61146c4d7901\ --query"Snapshots[*].[SnapshotId]"\ --outputtext输出:
snap-1234567890abcdef0 snap-08637175a712c3fb9 ...有关使用筛选器的其他示例,请参阅《Amazon EC2 用户指南》中的列出和筛选资源。
示例 3:根据标签描述快照
以下
describe-snapshots示例使用标签筛选器,将结果范围限定为带有标签Stack=Prod的快照。aws ec2 describe-snapshots \ --filtersName=tag:Stack,Values=prod有关
describe-snapshots的输出示例,请参阅示例 1。有关使用标签筛选器的更多示例,请参阅《Amazon EC2 用户指南》中的使用标签。
示例 4:根据期限描述快照
以下
describe-snapshots示例使用 JMESPath 表达式,来描述 AWS 账户在指定日期之前创建的所有快照。仅显示快照 ID。aws ec2 describe-snapshots \ --owner-ids012345678910\ --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 用户指南》中的查看已存档快照。
-
有关 API 详细信息,请参阅《AWS CLI 命令参考》中的 DescribeSnapshots
。
-
- PowerShell
-
- Tools for PowerShell V4
-
示例 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:此示例描述标签为“Name”且值为“TestValue”的快照。
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 参考(V4)》中的 DescribeSnapshots。
-
- Tools for PowerShell V5
-
示例 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:此示例描述标签为“Name”且值为“TestValue”的快照。
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 参考(V5)》中的 DescribeSnapshots。
-
- Rust
-
- 适用于 Rust 的 SDK
-
注意
查看 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 详细信息,请参阅《AWS SDK for Rust API 参考》中的 DescribeSnapshots
。
-
有关 AWS SDK 开发人员指南和代码示例的完整列表,请参阅 使用 AWS SDK 创建 Amazon EC2 资源。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。