Home » Software Testing » AWS CLI Commands- Create S3 Bucket command

AWS CLI Commands- Create S3 Bucket command

1.     Create S3 Bucket Commands

User can create S3 bucket by mb command, but have required permission for the same.

  • ‘mb’ stands for make bucket.

1.1 To Create Bucket

Below command will create bucket under the region which is mentioned in config file.

  • aws s3 mb s3://<bucket-name>

E.g.: Aws s3 mb s3://newbucket

1.2 To create a Bucket in a specified Region

Below command will create bucket which you mentioned with command

  • aws s3 mb s3://<bucket-name>  –region <Region-Name>

E.g.: aws s3 mb s3://newbucket –region us-west-2

2.     Delete S3 bucket

User can use rb command to delete specified bucket, but should have required permissions. ‘rb’ stands for remove bucket.

2.1 To delete Empty Bucket

 Below command will work if bucket is empty

  • aws s3 rb s3://<delete bucket name>

E.g.: aws s3 rb s3://mybucket

2.2 To delete s3 bucket and it’s all Objects

User can delete Bucket and their contents also in single command.

  • aws s3 rb s3://<delete bucket name> –force

E.g.: aws s3 rb s3://mybucket –force

3.     MV Command

mv is used to move files from local to s3 or vice versa.

3.1 Move Local File to S3 Bucket

By this command, user can move required file on desired S3 location

  • aws s3 mv <Local file>  <S3 Target Location>

3.2 Move file into S3 bcuket with different name

  • aws s3 mv <Local-file>  <S3 Target Location>/<Local-File1>

3.3 Move Files/Folder with all Files to S3 Bucket

In below command, all files from current folder or any subfolder is moved onto S3 location

  • aws s3 mv <Folder-name> <S3 Target Location> –recursive

If user wants to movve same folder on S3 location, then specify the folder name too

  • aws s3 cp<Folder-name> <S3 Target Location>/<Folder-name> –recursive

3.4 Move a File from S3 Bucket to local

Move a file form S3 bucket location to local machine

  • aws s3 mv <S3 Source Location> .

User can move the file to a local machine with different name

  • aws s3 mv <S3 source Location> <Desired-file-name>

3.5 Move All Files Recursively from a S3 bucket

User can also move all files from S3 location to local machine by below command:

  • aws s3 mv <S3 Source Location> . –recursive

3.6 Move All Files from One S3 Bucket to Another S3 Bucket

Use can move files/folder from one s3 location to another s3 location

  • aws s3 mv <S3 source location> <Target S3 location> –recursive

4.     Delete a file

User can delete file(s) from S3 location by rm command.

4.1 Delete single file from S3 location

Using below command, user can delete single file from s3 bucket

  • aws s3 rm s3://< s3 location>/<filename>

4.2 Delete all files from s3 location

To delete all files from s3 location, use –recursive option

aws s3 rm s3://<s3 location> –recursive

5.     Count number of lines of a File on S3 bucket

User can print number of lines of any file through CP and WC –l option

  • aws s3 cp < S3 bucket location>/<File-name> -|wc –l

E.g.: aws s3 cp s3://mybucket/myfolder/myfile.txt -|wc -l

6.     Read contents of files on s3 bucket without downloading

User is able to see contents of file with combination of CP and Cat Command; it would be good if file is small.

  • aws s3 cp <S3 bucket location>/<File-name> -|cat

E.g.: aws s3 cp s3://mybucket/myfolder/myfile.txt -|cat

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.