Prerequisites
This has been tested on Ubuntu 12.04 and 14.04, but may also work on others.
Steps
Installation
Install the tools with the following commands:
sudo pip install awscli
Personal Configuration
If you just want to use this for a single account with manual commands (e.g. not scripts), then perform the following steps. If you want to configure this for multiple accounts and/or scripts, then it is probably best to run the “Setup For Scripts” section instead.
Run the following command and answer the questions
aws configure
When it asks for the default output format, your choices are
,
, or
. Personally, I use
Setup For Scripts
If you followed the previous section, then you do not need to run this one.
Now we need to set up our AWS credentials for automatic authentication
vim $HOME/.aws/config
File contents
aws_access_key_id = [ID HERE]
aws_secret_access_key = [KEY HERE]
region = eu-west-1
The configuration needs to be there, otherwise you need to set the AWS_CONFIG_FILE environment variable to its location e.g.
export AWS_CONFIG_FILE="`echo $HOME`/.aws/config"
Protect the file from other users who have access to the same machine.
Testing
You can test it’s working with a simple command t fetch the regions from AWS:
Now you can use the CLI to transfer files to and from s3 like so:
or the other way around
References
AWS CLI S3 Cheatsheet
It’s much faster to interface with S3 files through the CLI than it is to use the web browser. Here is a cheatsheet of useful S3 based CLI commands.
Remove All Files In A Bucket
aws s3 rm --recursive s3://my-bucket
One cannot use the
character as a wildcard. S3 considers this a valid character for a filename. Hence the use of the
flag.
Copy A Bucket
aws s3 cp --recursive s3://my-bucket s3://my-second-bucket
Move A Bucket’s Contents
Same as before but with “mv” instead of “cp”
Upload To Bucket
aws s3 cp /path/to/local/file.txt s3://my-bucket/sub-folder/file.txt
Download File From Bucket
aws s3 cp s3://my-bucket/sub-folder/file.txt /path/to/local/file.txt