Skip to main content

s3 commands with awscli

This is quick tutorial to understand basic s3 operations using awscli. It is meant for beginners who are looking to get started with s3 or it can also be used as refresher to go through basic s3 commands.

Install aws command line

On ubunutu : sudo apt-get install awscli

Configure awscli

aws configure

Some settings

  • It will prompt for access_key, secret_key, region and default output.
  • Create access_key/secret_key for user through AWS console IAM services.
  • Default region is us-east-1
  • For output format, choose json

S3 Commands

Create s3 bucket

Both s3 and s3api can be used for the same:

  • With s3 : aws s3 mb s3://<unique_bucket_name>
  • With s3api : aws s3api create-bucket --bucket <unique_bucket_name>

The default region is us-east-1, so if the list is to be created in any other region then --region option is to be used with bucket configuration

  • aws s3api create-bucket --bucket <unique_bucket_name> --region us-west-1 --create-bucket-configuration LocationConstraint=us-west-1

Copy file to bucket

Copy test.txt to bucket 'test-bucket' :

  • aws s3 cp ./test.txt s3://test-bucket/

Copy directory contents to bucket

Copy all directory test/ content to bucket 'test-bucket' :

  • aws s3 sync ./test/ s3://test-bucket/ Another way to copy is:
  • aws s3 cp ./test/ s3://test-bucket/ --recursive

Remember this will copy the content in bucket directly. If you wish to move the directory as well, the name of the directory should be appended in the s3 url:

  • aws s3 cp ./test/ s3://test-bucket/test/ --recursive

Delete files from bucket

Delete a file test.txt from bucket 'test-bucket':

  • aws s3 rm s3://test-bucket/test.txt

Delete directory test/ from bucket 'test-bucket':

  • aws s3 rm s3://test-bucket/test/ --recursive

Delete bucket

  • Using s3 : aws s3 rb s3://test-bucket/
Bucket can only be delete when it is empty. If you try to delete non-empty bucket, following error is received :
An error occurred (BucketNotEmpty) when calling the DeleteBucket operation: The bucket you tried to delete is not empty