

文件 AWS 開發套件範例 GitHub 儲存庫中有更多可用的 [AWS SDK 範例](https://github.com/awsdocs/aws-doc-sdk-examples)。

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

# 使用 AWS SDKs Amazon EBS 程式碼範例
<a name="ebs_code_examples"></a>

下列程式碼範例示範如何使用 Amazon Elastic Block Store 搭配 AWS 軟體開發套件 (SDK)。

*Actions* 是大型程式的程式碼摘錄，必須在內容中執行。雖然動作會告訴您如何呼叫個別服務函數，但您可以在其相關情境中查看內容中的動作。

**其他資源**
+  **[Amazon EBS 使用者指南](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/AmazonEBS.html)** – Amazon EBS 的詳細資訊。
+ **[Amazon EBS API 參考](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/OperationList-query-ebs.html)** – 所有可用 Amazon EBS 動作的詳細資訊。
+ **[AWS 開發人員中心](https://aws.amazon.com/developer/code-examples/?awsf.sdk-code-examples-product=product%23ebs)** – 您可以依類別或全文搜尋篩選的程式碼範例。
+ **[AWS SDK 範例](https://github.com/awsdocs/aws-doc-sdk-examples)** – GitHub 儲存庫使用慣用語言的完整程式碼。包含設定和執行程式碼的指示。

**Contents**
+ [基本概念](ebs_code_examples_basics.md)
  + [動作](ebs_code_examples_actions.md)
    + [`CompleteSnapshot`](ebs_example_ebs_CompleteSnapshot_section.md)
    + [`PutSnapshotBlock`](ebs_example_ebs_PutSnapshotBlock_section.md)
    + [`StartSnapshot`](ebs_example_ebs_StartSnapshot_section.md)

# 使用 AWS SDKs 的 Amazon EBS 基本範例
<a name="ebs_code_examples_basics"></a>

下列程式碼範例示範如何搭配 AWS SDK 使用 Amazon Elastic Block Store 的基本功能。

**Contents**
+ [動作](ebs_code_examples_actions.md)
  + [`CompleteSnapshot`](ebs_example_ebs_CompleteSnapshot_section.md)
  + [`PutSnapshotBlock`](ebs_example_ebs_PutSnapshotBlock_section.md)
  + [`StartSnapshot`](ebs_example_ebs_StartSnapshot_section.md)

# 使用 AWS SDKs 的 Amazon EBS 動作
<a name="ebs_code_examples_actions"></a>

下列程式碼範例示範如何使用 AWS SDKs 執行個別 Amazon EBS 動作。每個範例均包含 GitHub 的連結，您可以在連結中找到設定和執行程式碼的相關說明。

 下列範例僅包含最常使用的動作。如需完整清單，請參閱《[Amazon Elastic Block Store API 參考](https://docs.aws.amazon.com/AWSEC2/latest/APIReference/OperationList-query-ebs.html)》。

**Topics**
+ [`CompleteSnapshot`](ebs_example_ebs_CompleteSnapshot_section.md)
+ [`PutSnapshotBlock`](ebs_example_ebs_PutSnapshotBlock_section.md)
+ [`StartSnapshot`](ebs_example_ebs_StartSnapshot_section.md)

# `CompleteSnapshot` 搭配 AWS SDK 使用
<a name="ebs_example_ebs_CompleteSnapshot_section"></a>

以下程式碼範例顯示如何使用 `CompleteSnapshot`。

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

**適用於 Rust 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/rustv1/examples/ebs#code-examples)中設定和執行。

```
async fn finish(client: &Client, id: &str) -> Result<(), Error> {
    client
        .complete_snapshot()
        .changed_blocks_count(2)
        .snapshot_id(id)
        .send()
        .await?;

    println!("Snapshot ID {}", id);
    println!("The state is 'completed' when all of the modified blocks have been transferred to Amazon S3.");
    println!("Use the get-snapshot-state code example to get the state of the snapshot.");

    Ok(())
}
```
+  如需 API 詳細資訊，請參閱《AWS SDK for Rust API 參考》**中的 [CompleteSnapshot](https://docs.rs/aws-sdk-ebs/latest/aws_sdk_ebs/client/struct.Client.html#method.complete_snapshot)。

------

# `PutSnapshotBlock` 搭配 AWS SDK 使用
<a name="ebs_example_ebs_PutSnapshotBlock_section"></a>

以下程式碼範例顯示如何使用 `PutSnapshotBlock`。

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

**適用於 Rust 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/rustv1/examples/ebs#code-examples)中設定和執行。

```
async fn add_block(
    client: &Client,
    id: &str,
    idx: usize,
    block: Vec<u8>,
    checksum: &str,
) -> Result<(), Error> {
    client
        .put_snapshot_block()
        .snapshot_id(id)
        .block_index(idx as i32)
        .block_data(ByteStream::from(block))
        .checksum(checksum)
        .checksum_algorithm(ChecksumAlgorithm::ChecksumAlgorithmSha256)
        .data_length(EBS_BLOCK_SIZE as i32)
        .send()
        .await?;

    Ok(())
}
```
+  如需 API 詳細資訊，請參閱《AWS SDK for Rust API 參考》**中的 [PutSnapshotBlock](https://docs.rs/aws-sdk-ebs/latest/aws_sdk_ebs/client/struct.Client.html#method.put_snapshot_block)。

------

# `StartSnapshot` 搭配 AWS SDK 使用
<a name="ebs_example_ebs_StartSnapshot_section"></a>

以下程式碼範例顯示如何使用 `StartSnapshot`。

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

**適用於 Rust 的 SDK**  
 GitHub 上提供更多範例。尋找完整範例，並了解如何在 [AWS 程式碼範例儲存庫](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/rustv1/examples/ebs#code-examples)中設定和執行。

```
async fn start(client: &Client, description: &str) -> Result<String, Error> {
    let snapshot = client
        .start_snapshot()
        .description(description)
        .encrypted(false)
        .volume_size(1)
        .send()
        .await?;

    Ok(snapshot.snapshot_id.unwrap())
}
```
+  如需 API 詳細資訊，請參閱《AWS SDK for Rust API 參考》**中的 [StartSnapshot](https://docs.rs/aws-sdk-ebs/latest/aws_sdk_ebs/client/struct.Client.html#method.start_snapshot)。

------