How to Copy Files from AWS S3 to an Amazon Web Services (AWS) EC2 Instance

In this tutorial I will show you how you can copy files from S3 to an Amazon Web Services EC2 instance using the AWS Command Line Interface (CLI).

The basic concept of the AWS CLI S3 commands is that you have two types of path arguments: LocalPath and S3Uri.

The LocalPath is the local file or directory. The S3Uri is the location of a S3 object or bucket written in the form S3://mybucket/myobjectkey.

In each command, one path argument represents the source and the other the destination.

Prerequisites

  1. You have an EC2 instance running and can get access to it using SSH
  2. You have configured the AWS CLI on your instance using keys with read/write access to S3 – see How to Configure the AWS CLI
  3. You have a bucket or buckets set up containing some test files or objects

Copying a file to S3

$ aws s3 cp MyFile.txt s3://my-bucket/path/

Moving all files from S3

$ aws s3 mv s3://my-bucket/path ./MyDirectory --recursive

Removing a file from S3

$ aws s3 rm s3://my-bucket/path/MyFile.txt

Syncing current directory with a S3 bucket

$ aws s3 sync . s3://my-bucket/path

Delete all contents of a bucket

$ aws s3 rm s3://my-bucket/path --recursive

List all buckets

$ aws s3 ls

List contents of a bucket

$ aws s3 ls s3://bucket-name

Leave a Reply