

文档 AWS SDK 示例 GitHub 存储库中还有更多 [S AWS DK 示例](https://github.com/awsdocs/aws-doc-sdk-examples)。

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

# 使用 Amazon EBS 的代码示例 AWS SDKs
<a name="ebs_code_examples"></a>

以下代码示例向您展示了如何使用带有 AWS 软件开发套件 (SDK) 的 Amazon Elastic Block Store。

*操作*是大型程序的代码摘录，必须在上下文中运行。您可以通过操作了解如何调用单个服务函数，还可以通过函数相关场景的上下文查看操作。

**更多资源**
+  **[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)

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

以下代码示例展示了如何使用 Amazon Elastic Block Store 的基础知识 AWS SDKs。

**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)

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

以下代码示例演示了如何使用执行单个 Amazon EBS AWS SDKs 操作。每个示例都包含一个指向的链接 GitHub，您可以在其中找到有关设置和运行代码的说明。

 以下示例仅包括最常用的操作。有关完整列表，请参阅《[Amazon Elastic Block Store API Reference](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)

# 与 AWS SDK `CompleteSnapshot` 配合使用
<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 的详细信息，请参阅适用[CompleteSnapshot](https://docs.rs/aws-sdk-ebs/latest/aws_sdk_ebs/client/struct.Client.html#method.complete_snapshot)于 *Rust 的AWS SDK API 参考*。

------

# 与 AWS SDK `PutSnapshotBlock` 配合使用
<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 的详细信息，请参阅适用[PutSnapshotBlock](https://docs.rs/aws-sdk-ebs/latest/aws_sdk_ebs/client/struct.Client.html#method.put_snapshot_block)于 *Rust 的AWS SDK API 参考*。

------

# `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 的详细信息，请参阅适用[StartSnapshot](https://docs.rs/aws-sdk-ebs/latest/aws_sdk_ebs/client/struct.Client.html#method.start_snapshot)于 *Rust 的AWS SDK API 参考*。

------