Skip to main content

Command Palette

Search for a command to run...

Cheat Sheet #day17 - Amazon S3 Command Line

Published
3 min readView as Markdown
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

      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

      aws s3 mb s3://my-new-bucket
    
  • List Buckets

      aws s3 ls
    
  • Delete a Bucket

      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

      aws s3 cp myfile.txt s3://my-bucket/
    
  • Download a File from a Bucket

      aws s3 cp s3://my-bucket/myfile.txt .
    
  • Delete a File from a Bucket

      aws s3 rm s3://my-bucket/myfile.txt
    
  • Copy a File between Buckets

      aws s3 cp s3://source-bucket/myfile.txt s3://destination-bucket/myfile.txt
    

Directory Operations

  • Upload a Directory to a Bucket

      aws s3 cp my-directory/ s3://my-bucket/my-directory/ --recursive
    
  • Download a Directory from a Bucket

      aws s3 cp s3://my-bucket/my-directory/ my-directory/ --recursive
    
  • Synchronize a Local Directory with a Bucket

      aws s3 sync my-directory/ s3://my-bucket/my-directory/
    
  • Synchronize a Bucket with a Local Directory

      aws s3 sync s3://my-bucket/my-directory/ my-directory/
    

List and Manage Objects

  • List Objects in a Bucket

      aws s3 ls s3://my-bucket/
    
  • List Objects in a Bucket with a Prefix

      aws s3 ls s3://my-bucket/prefix/
    
  • List All Versions of Objects in a Bucket

      aws s3api list-object-versions --bucket my-bucket
    
  • Get Object Metadata

      aws s3api head-object --bucket my-bucket --key myfile.txt
    

Permissions and ACLs

  • Set Bucket Policy

      aws s3api put-bucket-policy --bucket my-bucket --policy file://policy.json
    
  • Get Bucket Policy

      aws s3api get-bucket-policy --bucket my-bucket
    
  • Delete Bucket Policy

      aws s3api delete-bucket-policy --bucket my-bucket
    
  • Set Object ACL

      aws s3api put-object-acl --bucket my-bucket --key myfile.txt --acl public-read
    

Miscellaneous

  • Generate a Pre-signed URL

      aws s3 presign s3://my-bucket/myfile.txt --expires-in 3600
    
  • Enable Versioning on a Bucket

      aws s3api put-bucket-versioning --bucket my-bucket --versioning-configuration Status=Enabled
    
  • Enable Logging on a Bucket

      aws s3api put-bucket-logging --bucket my-bucket --bucket-logging-status file://logging.json
    

Useful Options

  • Specify a Region

      aws s3 ls --region us-west-2
    
  • Include and Exclude Patterns

      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.