

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

# `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` 參數，僅顯示快照 ID 和快照啟動時間。  

```
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` 參數，僅顯示快照 ID。  

```
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 帳戶在指定日期之前建立的所有快照。此範例僅顯示快照 ID。  

```
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 ]

**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：此範例描述具有值為 'TestValue' 之 'Name' 標籤的快照。**  

```
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](https://docs.aws.amazon.com/powershell/v4/reference)。

**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：此範例描述具有值為 'TestValue' 之 'Name' 標籤的快照。**  

```
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](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 詳細資訊，請參閱《適用於 Rust 的 AWS  SDK API 參考》**中的 [DescribeSnapshots](https://docs.rs/aws-sdk-ec2/latest/aws_sdk_ec2/client/struct.Client.html#method.describe_snapshots)。

------

如需 AWS SDK 開發人員指南和程式碼範例的完整清單，請參閱 [使用 SDK 建立 Amazon EC2 資源 AWS](sdk-general-information-section.md)。此主題也包含有關入門的資訊和舊版 SDK 的詳細資訊。