

# AWS CLI 中的 Amazon S3 存储桶生命周期脚本示例
<a name="cli-services-s3-lifecycle-example"></a>

本主题介绍使用 AWS Command Line Interface (AWS CLI) 的 Amazon S3 存储桶生命周期操作的 bash 脚本示例。此脚本示例使用 [https://docs.aws.amazon.com/cli/latest/reference/s3api/index.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/index.html) 命令集。Shell 脚本是专用于在命令行界面中运行的程序。

**Topics**
+ [在您开始之前](#cli-services-s3-lifecycle-example-before)
+ [关于此示例](#cli-services-s3-lifecycle-example-about)
+ [文件](#cli-services-s3-lifecycle-example-files)
+ [引用](#cli-services-s3-lifecycle-example-references)

## 在您开始之前
<a name="cli-services-s3-lifecycle-example-before"></a>

您需要先满足以下条件，才能运行下文中的任何示例代码。
+ 安装和配置 AWS CLI。有关更多信息，请参阅 [安装或更新最新版本的 AWS CLI](getting-started-install.md) 和 [AWS CLI 身份验证和访问凭证](cli-chap-authentication.md)。
+ 您使用的配置文件必须具有允许示例代码执行AWS操作的权限。
+ 作为AWS的最佳实践，请授予此代码最低的权限，或者仅授予它执行任务所需的权限。有关更多信息，请参阅 *IAM 用户指南* 中的 [授予最低权限](https://docs.aws.amazon.com/IAM/latest/UserGuide/best-practices.html#grant-least-privilege)。
+ 此代码尚未在所有AWS区域中进行测试。有些 AWS 服务仅在特定区域中提供。有关更多信息，请参阅《AWS 一般参考指南》**中的[服务端点和配额](https://docs.aws.amazon.com/general/latest/gr/aws-service-information.html)。
+ 运行此代码可能会导致您的 AWS 账户产生相关费用。您有责任确保在使用完由此脚本创建的任何资源后删除这些资源。

Amazon S3 服务使用以下术语：
+ 存储桶 – 顶级 Amazon S3 文件夹。
+ 前缀 – 存储桶中的 Amazon S3 文件夹。
+ 对象 – Amazon S3 存储桶中托管的任何项。

## 关于此示例
<a name="cli-services-s3-lifecycle-example-about"></a>

此示例说明如何使用 shell 脚本文件中的一组函数与一些基本的 Amazon S3 操作进行交互。这些函数包含在名为 `bucket-operations.sh` 的 shell 脚本文件中。您可以在另一个文件中调用这些函数。每个脚本文件都包含了介绍每个函数的注释。

要查看每个步骤的中间结果，请使用 `-i` 参数运行脚本。您可以使用 Amazon S3 控制台查看存储桶或其内容的当前状态。仅当您在系统提示符处按 **Enter** 后，脚本才会继续执行下一步。

有关完整示例和可下载的脚本文件，请参阅 *GitHub* 上的 *AWS 代码示例存储库* 中的 [Amazon S3 存储桶生命周期操作](https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/aws-cli/bash-linux/s3/bucket-lifecycle-operations)。

## 文件
<a name="cli-services-s3-lifecycle-example-files"></a>

此示例包含以下文件：

**bucket-operations.sh**  
此主脚本文件可以从另一个文件中调取。它包含了执行以下任务的函数：  
+ 创建存储桶并验证它是否存在
+ 将文件从本地电脑复制到存储桶
+ 将文件从一个存储桶位置复制到另一个存储桶位置
+ 列出存储桶中的内容
+ 从存储桶中删除文件
+ 删除存储桶
在 *GitHub* 上查看 `[bucket-operations.sh](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/aws-cli/bash-linux/s3/bucket-lifecycle-operations/bucket_operations.sh)` 的代码。

**test-bucket-operations.sh**  
shell 脚本文件 `test-bucket-operations.sh` 演示了如何调用这些函数，即调取 `bucket-operations.sh` 文件然后调用其中的每个函数。调用函数后，该测试脚本会删除它创建的所有资源。  
在 *GitHub* 上查看 `[test-bucket-operations.sh](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/aws-cli/bash-linux/s3/bucket-lifecycle-operations/test_bucket_operations.sh)` 的代码。

**awsdocs-general.sh**  
脚本文件 `awsdocs-general.sh` 中包含了在 AWS CLI 的高级代码示例中使用的通用函数。  
在 *GitHub* 上查看 `[awsdocs-general.sh](https://github.com/awsdocs/aws-doc-sdk-examples/blob/main/aws-cli/bash-linux/s3/bucket-lifecycle-operations/awsdocs_general.sh)` 的代码。

## 引用
<a name="cli-services-s3-lifecycle-example-references"></a>

**AWS CLI 参考：**
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/index.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/index.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/create-bucket.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/create-bucket.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/copy-object.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/copy-object.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/delete-bucket.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/delete-bucket.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/delete-object.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/delete-object.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/head-bucket.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/head-bucket.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/list-objects.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/list-objects.html)
+ [https://docs.aws.amazon.com/cli/latest/reference/s3api/put-object.html](https://docs.aws.amazon.com/cli/latest/reference/s3api/put-object.html)

**其他参考资料：**
+ 《Amazon S3 开发人员指南》**中的[使用 Amazon S3 存储桶](https://docs.aws.amazon.com//AmazonS3/latest/userguide/UsingBucket.html)
+ 《Amazon S3 用户指南》**中的[使用 Amazon S3 对象](https://docs.aws.amazon.com//AmazonS3/latest/userguide/UsingObjects.html)
+ 要查看和贡献 AWS 开发工具包和 AWS CLI 代码示例，请参阅 *GitHub* 上的 [AWS 代码示例存储库](https://github.com/awsdocs/aws-doc-sdk-examples/)。