This documentation is for Version 1 of the Amazon CLI only. For documentation related to Version 2 of the Amazon CLI, see the Version 2 User Guide.
Scripting example for the Amazon S3 bucket lifecycle in the Amazon CLI
This topic uses a bash scripting example for Amazon S3 bucket lifecycle operations using the
Amazon Command Line Interface (Amazon CLI). This scripting example uses the
aws
s3api
set of commands. Shell scripts are programs designed to run in a command line interface.
Before you start
Before you can run any of the below examples, the following things need to be completed.
Install and configure the Amazon CLI. For more information, see Installing, updating, and uninstalling the Amazon CLI and Authentication and access credentials for the Amazon CLI.
-
The profile that you use must have permissions that allow the Amazon operations performed by the examples.
-
As an Amazon best practice, grant this code least privilege, or only the permissions required to perform a task. For more information, see Grant Least Privilege in the IAM User Guide.
-
This code has not been tested in all Amazon Regions. Some Amazon services are available only in specific Regions. For more information, see Service Endpoints and Quotas in the Amazon General Reference Guide.
-
Running this code can result in charges to your Amazon account. It is your responsibility to ensure that any resources created by this script are removed when you are done with them.
The Amazon S3 service uses the following terms:
-
Bucket — A top level Amazon S3 folder.
-
Prefix — An Amazon S3 folder in a bucket.
-
Object — Any item hosted in an Amazon S3 bucket.
About this example
This example demonstrates how to interact with some of the basic Amazon S3 operations using a
set of functions in shell script files. The functions are located in the shell script file
named bucket-operations.sh
. You can call these functions in another file.
Each script file contains comments describing each of the functions.
To see the intermediate results of each step, run the script with a -i
parameter. You can view the current status of the bucket or its contents using the Amazon S3
console. The script only proceeds to the next step when you press enter at the prompt.
For the full example and downloadable script files, see Amazon S3 Bucket Lifecycle Operations
Files
The example contains the following files:
- bucket-operations.sh
-
This main script file can be sourced from another file. It includes functions that perform the following tasks:
-
Creating a bucket and verifying that it exists
-
Copying a file from the local computer to a bucket
-
Copying a file from one bucket location to a different bucket location
-
Listing the contents of a bucket
-
Deleting a file from a bucket
-
Deleting a bucket
View the code for
bucket-operations.sh
on GitHub. -
- test-bucket-operations.sh
-
The shell script file
test-bucket-operations.sh
demonstrates how to call the functions by sourcing thebucket-operations.sh
file and calling each of the functions. After calling functions, the test script removes all resources that it created.View the code for
test-bucket-operations.sh
on GitHub.
- awsdocs-general.sh
-
The script file
awsdocs-general.sh
holds general purpose functions used across advanced code examples for the Amazon CLI.View the code for
awsdocs-general.sh
on GitHub.
References
Amazon CLI reference:
Other reference:
-
Working with Amazon S3 buckets in the Amazon S3 User Guide
-
Working with Amazon S3 objects in the Amazon S3 User Guide
-
To view and contribute to Amazon SDK and Amazon CLI code examples, see the Amazon Code Examples Repository
on GitHub.