DEV Community

vaibhavi_shah for AWS Community ASEAN

Posted on

Let's Play with bucket using CP command

Prerequisites:

  1. An AWS Acoount
  2. Two bucket one for source and one for destination
  3. Aws CLI configured.

For demonstration, I created to new bucket which is publicly accessible.

bucket created

Uploading Individual Files to S3

To upload a file to S3, you’ll need to provide two arguments (source and destination) to the aws s3 cp command.

Upload file

Note: S3 bucket names are always prefixed with S3:// when used with AWS CLI

Use the command below to list the objects at the root of the S3 bucket.
aws s3 ls s3://bucket-name

list bucket content

Uploading Multiple Files and Folders to S3

What if you need to upload multiple files from a folder and sub-folders? Surely you wouldn’t want to run the same command multiple times for different filenames, right?

The aws s3 cp command has an option to process files and folders recursively, and this is the --recursive option.

Upload muliple files and folder

If we don't use --recursive option it shows an error.

*Uploading Multiple Files and Folders to S3 Selectively
*

In some cases, uploading ALL types of files is not the best option. Like, when you only need to upload files with specific file extensions.

Options available to the cp command is the --include and --exclude.

--include --exclude

Downloading Objects from S3

Copying from S3 to local would require you to switch the positions of the source and the destination. The source being the S3 location, and the destination is the local path, like the one shown below.

Download from bucket

You can also use --recursive option for multiple download.

Recursive download

Copy from one bucket to oneother bucket.

aws s3 cp source-bucket destination-bucket

copy bucket to bucket

Discussion (2)

Collapse
raphael_jambalos profile image
Raphael Jambalos

Hi Havi! Interesting experiment on the S3 cp commands. I found this command very handy when moving around files from one s3 bucket to another, instead of download it in my local and reuploading it back to s3.

Collapse
vaibhavi_shah profile image
vaibhavi_shah Author

Thank you.