

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# `DescribeSnapshots`与 AWS SDK 或 CLI 配合使用
<a name="example_ec2_DescribeSnapshots_section"></a>

以下代码示例演示如何使用 `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 EC2 用户指南》**中的 [Amazon EBS 快照](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/EBSSnapshots.html)。  
**示例 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 用户指南》**中的[列出和筛选资源](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Filtering.html#Filtering_Resources_CLI)。  
**示例 3：根据标签描述快照**  
以下 `describe-snapshots` 示例使用标签筛选器，将结果范围限定为带有标签 `Stack=Prod` 的快照。  

```
aws ec2 describe-snapshots \
    --filters Name=tag:Stack,Values=prod
```
有关 `describe-snapshots` 的输出示例，请参阅示例 1。  
有关使用标签筛选器的更多示例，请参阅《Amazon EC2 用户指南》**中的[使用标签](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html#Using_Tags_CLI)。  
**示例 4：根据期限描述快照**  
以下`describe-snapshots`示例使用 JMESPath 表达式来描述您的 AWS 账户在指定日期之前创建的所有快照。它仅显示快照 IDs。  

```
aws ec2 describe-snapshots \
    --owner-ids 012345678910 \
    --query "Snapshots[?(StartTime<='2020-03-31')].[SnapshotId]"
```
有关使用筛选器的其他示例，请参阅《Amazon EC2 用户指南》**中的[列出和筛选资源](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Filtering.html#Filtering_Resources_CLI)。  
**示例 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 用户指南》**中的[查看已存档快照](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/working-with-snapshot-archiving.html#view-archived-snapshot)。  
+  有关 API 的详细信息，请参阅*AWS CLI 命令参考[DescribeSnapshots](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/ec2/describe-snapshots.html)*中的。

------
#### [ PowerShell ]

**适用于 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：此示例描述了带有 “名称” 标签且值为 “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 参考 (V* 4) [DescribeSnapshots](https://docs.aws.amazon.com/powershell/v4/reference)中的。

**适用于 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：此示例描述了带有 “名称” 标签且值为 “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 参考 (V* 5) [DescribeSnapshots](https://docs.aws.amazon.com/powershell/v5/reference)中的。

------
#### [ Rust ]

**适用于 Rust 的 SDK**  
 还有更多相关信息 GitHub。在 [AWS 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/rustv1/examples/ebs#code-examples)中查找完整示例，了解如何进行设置和运行。
显示快照的状态。  

```
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](https://docs.rs/aws-sdk-ec2/latest/aws_sdk_ec2/client/struct.Client.html#method.describe_snapshots)于 *Rust 的AWS SDK API 参考*。

------

有关 S AWS DK 开发者指南和代码示例的完整列表，请参阅[使用 AWS SDK 创建 Amazon EC2 资源](sdk-general-information-section.md)。本主题还包括有关入门的信息以及有关先前的 SDK 版本的详细信息。