S3 CMD

CONFIGURE S3CMD IN LINUX

S3CMD is a command line utility.In this tutorial, We will learn on how to configure s3cmd command line utility to manage S3 Buckets.

For this, We need the following details of your AWS Account.

  • ACCESS KEY
  • SECRET KEY

If you don’t have the following details.You can get these details from AWS Security Credentials Page If prompted Login to your AWS account.You will see the following Page.

Expand Access Keys (access key ID and secret access key).

Click Create New Access key, You can note the keys from Show access key option or you can download the key file.

 

After that,  Login to your Linux Machine of any distribution, In this tutorial, I am using Ubuntu 16.04.

Run the following command:

DEBIAN BASED LINUX:

#sudo apt-get install s3cmd -y

RPM BASED LINUX:

#yum install s3cmd -y

Once installed, Run the following command

#s3cmd –configure

It will ask you for ACCESS KEY, SECRET KEY ID & ENCRYPTION PASSWORD [choose any password].

Provide the required details and press Enter.

After that, Press Enter for all the Options and Accept with Yes for the below questions.

All the S3 buckets associated with this account can be managed using S3CMD command line utility.

For more details of S3cmd command line usage, Type the following command:

#s3cmd – -help

1.TO LIST ALL THE S3 BUCKETS:

#s3cmd ls

2.CREATING NEW BUCKET:

#s3cmd mb s3://bucketname (A new S3 bucket will be created in your AWS Account)

EXAMPLE:

[email protected] ~ $ s3cmd mb s3://rahulknewbucket
Bucket ‘s3://rahulknewbucket/’ created

3.UPLOADING FILE INTO S3 BUCKET:

#s3cmd put filename s3://bucketname (You can choose a specific bucket where you want to upload the files)

EXAMPLE:

[email protected] ~/Documents $ s3cmd put all-tunnel-check2222.sh s3://rahulknewbucket/
all-tunnel-check2222.sh -> s3://rahulknewbucket/all-tunnel-check2222.sh [1 of 1]
3529 of 3529 100% in 1s 2.73 kB/s done

4.UPLOADING A DIRECTORY/FOLDER INTO S3 BUCKET:

If you need to upload entire directory use -r to upload it recursively.

Make sure you are using trailing slash when uploading directory else it will upload only the files within the directory.

#s3cmd put -r directoryname s3://bucketname

EXAMPLE:

[email protected] ~/Documents $ s3cmd put -r test s3://rahulknewbucket/
test/rahulk1 -> s3://rahulknewbucket/test/rahulk1 [1 of 3]
0 of 0 0% in 0s 0.00 B/s done
test/rahulk2 -> s3://rahulknewbucket/test/rahulk2 [2 of 3]
0 of 0 0% in 0s 0.00 B/s done
test/rahulk3 -> s3://rahulknewbucket/test/rahulk3 [3 of 3]
0 of 0 0% in 0s 0.00 B/s done

5.TO LIST ALL THE FILES IN S3 BUCKET:

#s3cmd ls s3://bucketname/

6.DOWNLOAD FILES AND DIRECTORY FORM S3 BUCKET:

#s3cmd get s3://bucketname/filename

#s3cmd get -r  s3://bucketname/directoryname (Don’t use trailing slash)

[email protected] ~/Documents $ s3cmd get -r s3://rahulknewbucket/test
s3://rahulknewbucket/test/rahulk1 -> ./test/rahulk1 [1 of 3]
0 of 0 0% in 0s 0.00 B/s done
s3://rahulknewbucket/test/rahulk2 -> ./test/rahulk2 [2 of 3]
0 of 0 0% in 0s 0.00 B/s done
s3://rahulknewbucket/test/rahulk3 -> ./test/rahulk3 [3 of 3]
0 of 0 0% in 0s 0.00 B/s done

7.REMOVING/DELETING FILES & DIRECTORY FROM S3 BUCKET:

#s3cmd del s3://bucketname/filename

Before deleting Folder make sure the folder is empty.

#s3cmd del s3://bucketname/foldername

8.TO DELETE THE S3 BUCKET:

Before deleting the s3 bucket, Make sure the bucket is empty, Or else You will face the following error:

[email protected] ~/Documents $ s3cmd rb s3://rahulknewbucket
ERROR: S3 error: 409 (BucketNotEmpty): The bucket you tried to delete is not empty

After deleting all the files and folders in the S3 bucket, Run the below command to delete the S3 Bucket.

[email protected] ~/Documents $ s3cmd rb s3://bucketname

Thanks for reading this article.Hope it helped you a lot.