# Cheat Sheet #day17 - Amazon S3 Command Line

### Amazon S3 Command Line Cheat Sheet

AWS S3 (Amazon Simple Storage Service) is a highly scalable, reliable, and low-latency data storage service. Below is a cheat sheet of common AWS S3 commands using the AWS CLI (Command Line Interface).

#### Setup

* **Configure AWS CLI**
    
    ```bash
    aws configure
    ```
    
    This command will prompt you to enter your AWS Access Key, Secret Key, region, and output format.
    

#### Bucket Operations

* **Create a New Bucket**
    
    ```bash
    aws s3 mb s3://my-new-bucket
    ```
    
* **List Buckets**
    
    ```bash
    aws s3 ls
    ```
    
* **Delete a Bucket**
    
    ```bash
    aws s3 rb s3://my-bucket --force
    ```
    
    The `--force` option deletes all objects within the bucket before deleting the bucket itself.
    

#### Object Operations

* **Upload a File to a Bucket**
    
    ```bash
    aws s3 cp myfile.txt s3://my-bucket/
    ```
    
* **Download a File from a Bucket**
    
    ```bash
    aws s3 cp s3://my-bucket/myfile.txt .
    ```
    
* **Delete a File from a Bucket**
    
    ```bash
    aws s3 rm s3://my-bucket/myfile.txt
    ```
    
* **Copy a File between Buckets**
    
    ```bash
    aws s3 cp s3://source-bucket/myfile.txt s3://destination-bucket/myfile.txt
    ```
    

#### Directory Operations

* **Upload a Directory to a Bucket**
    
    ```bash
    aws s3 cp my-directory/ s3://my-bucket/my-directory/ --recursive
    ```
    
* **Download a Directory from a Bucket**
    
    ```bash
    aws s3 cp s3://my-bucket/my-directory/ my-directory/ --recursive
    ```
    
* **Synchronize a Local Directory with a Bucket**
    
    ```bash
    aws s3 sync my-directory/ s3://my-bucket/my-directory/
    ```
    
* **Synchronize a Bucket with a Local Directory**
    
    ```bash
    aws s3 sync s3://my-bucket/my-directory/ my-directory/
    ```
    

#### List and Manage Objects

* **List Objects in a Bucket**
    
    ```bash
    aws s3 ls s3://my-bucket/
    ```
    
* **List Objects in a Bucket with a Prefix**
    
    ```bash
    aws s3 ls s3://my-bucket/prefix/
    ```
    
* **List All Versions of Objects in a Bucket**
    
    ```bash
    aws s3api list-object-versions --bucket my-bucket
    ```
    
* **Get Object Metadata**
    
    ```bash
    aws s3api head-object --bucket my-bucket --key myfile.txt
    ```
    

#### Permissions and ACLs

* **Set Bucket Policy**
    
    ```bash
    aws s3api put-bucket-policy --bucket my-bucket --policy file://policy.json
    ```
    
* **Get Bucket Policy**
    
    ```bash
    aws s3api get-bucket-policy --bucket my-bucket
    ```
    
* **Delete Bucket Policy**
    
    ```bash
    aws s3api delete-bucket-policy --bucket my-bucket
    ```
    
* **Set Object ACL**
    
    ```bash
    aws s3api put-object-acl --bucket my-bucket --key myfile.txt --acl public-read
    ```
    

#### Miscellaneous

* **Generate a Pre-signed URL**
    
    ```bash
    aws s3 presign s3://my-bucket/myfile.txt --expires-in 3600
    ```
    
* **Enable Versioning on a Bucket**
    
    ```bash
    aws s3api put-bucket-versioning --bucket my-bucket --versioning-configuration Status=Enabled
    ```
    
* **Enable Logging on a Bucket**
    
    ```bash
    aws s3api put-bucket-logging --bucket my-bucket --bucket-logging-status file://logging.json
    ```
    

#### Useful Options

* **Specify a Region**
    
    ```bash
    aws s3 ls --region us-west-2
    ```
    
* **Include and Exclude Patterns**
    
    ```bash
    aws s3 cp my-directory/ s3://my-bucket/my-directory/ --recursive --exclude "*.tmp" --include "*.txt"
    ```
    

This cheat sheet provides a quick reference to the most commonly used AWS S3 commands. For more detailed information and additional commands, refer to the [official AWS CLI documentation](https://docs.aws.amazon.com/cli/latest/reference/s3/).
