DEV Community

Rashwan Lazkani
Rashwan Lazkani

Posted on • Updated on

Managing AWS S3 using AWS CLI

AWS S3

Amazon Simple Storage Service (Amazon S3) is an object storage service offering industry-leading scalability, data availability, security, and performance. S3 has 99.999999999% (11 9s) of data durability. You can use S3 for object storage, host a website or redirect request.

In this article I will show you how you easily from your local environment can manage your S3 bucket when using the AWS CLI.

Prerequisites

  1. AWS CLI installed - tutorial from AWS of how to install the AWS CLI
  2. Configure AWS CLI - tutorial from AWS of how to configure your AWS CLI

Using AWS CLI with AWS S3

There are multiple commands you can use with S3 and the AWS CLI. The default structure is:

s3 <subcommand> [parameters]
Enter fullscreen mode Exit fullscreen mode

I will show you some commands that you might find useful in your daily work and of course there are plenty more.

Create a Bucket
aws s3 mb s3://mybucket
aws s3 mb s3://mybucket --region eu-north-1
Enter fullscreen mode Exit fullscreen mode
Remove a Bucket
aws s3 rb s3://mybucket
aws s3 rb s3://mybucket --force
Enter fullscreen mode Exit fullscreen mode
Deploy

This will use the default profile in your .aws credentials file

aws s3 sync build/ s3://mybucket
Enter fullscreen mode Exit fullscreen mode

If you want to deploy using a custom profile you can use the following command

aws s3 sync build/ s3://mybucket --profile mybucketapp
Enter fullscreen mode Exit fullscreen mode
ls commands
aws s3 ls
aws s3 ls s3://mybucket
aws s3 ls s3://mybucket --recursive
aws s3 ls s3://mybucket --recursive  --human-readable --summarize
Enter fullscreen mode Exit fullscreen mode
Copy a local file to Bucket
aws s3 cp index.html s3://mybucket
Enter fullscreen mode Exit fullscreen mode
Download a file
aws s3 cp s3://mybucket/index.html <path where you want to download the file to>
Enter fullscreen mode Exit fullscreen mode
Delete file/s

Delete a file from Bucket

aws s3 rm s3://mybucket/index.html
Enter fullscreen mode Exit fullscreen mode

Delete all files from a bucket

aws s3 rm s3://mybucket --recursive
Enter fullscreen mode Exit fullscreen mode
Delete Bucket

Delete an empty Bucket. rb = remove bucket

aws s3 rb s3://mybucket
Enter fullscreen mode Exit fullscreen mode

Delete a Bucket and all it's objects

aws s3 rb s3://mybucket --force
Enter fullscreen mode Exit fullscreen mode
Set bucket as a website
aws s3 website s3://mybucket/ --index-document index.html --error-document error.html
Enter fullscreen mode Exit fullscreen mode

Summary

Hope you found these commands useful. Do you have other commands you use? Please share them in the comments.

Top comments (0)