How to Copy file / folder from EC2 to S3 and vice-versa?

What is the way to copy data from EC2 to S3 and S3 to EC2?

You can copy any file or folder from EC2 to S3 and from S3 to EC2. This can be done easily using AWS CLI command. To check whether you have installed AWS CLI or not use below command

aws --version

Output will be something like: aws-cli/2.1.29 Python/3.7.4 Linux/10 botocore/2.0.0

If not already installed use below command to install it.

sudo apt-get install awscli  on Linux terminal

Now use below command to copy file from EC2 to S3.

aws s3 cp test.txt s3://mybucket/


upload: test.txt to s3://mybucket/test.txt

Use command to copy S3 to EC2.

aws s3 cp s3://mybucket/test.txt /home/ubuntu/


download: s3://mybucket/test.txt to /home/ubuntu/


Recursively copying S3 objects to a local directory:

When passed with the parameter --recursive, the following cp command recursively copies all objects under a specified prefix and bucket to a specified directory. In this example, the bucket mybucket has the objects test1.txt and test2.txt:

aws s3 cp s3://mybucket /home/ubuntu/ --recursive


download: s3://mybucket/test1.txt to test1.txt

download: s3://mybucket/test2.txt to test2.txt


Recursively copying local files to S3:

When passed with the parameter --recursive, the following cp command recursively copies all files under a specified directory to a specified bucket and prefix while excluding some files by using an --exclude parameter. In this example, the directory myDir has the files test1.txt and test2.jpg:

aws s3 cp myDir s3://mybucket/ --recursive --exclude "*.jpg"


upload: myDir/test1.txt to s3://mybucket/test1.txt


Hope it will help you to solve this issue of copying files to and from S3 or S3 to S3 etc.


For more details you can refer to below article as well: