Upload image to S3 using AWS CLI

1 minute read

Published:

Prerequisites

Unix-based OS.

Install

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

Click here for detail.

Configure

Configure AWS CLI using following command

aws configure

This initiates an interactive configuration process that prompts us for our settings

AWS Access Key ID [None]: Get access key guide.
AWS Secret Access Key [None]: Get access key guide.
Default region name [None]: ap-southeast-1 (You can type any valid region name)
Default output format [None]: json

Copy file to bucket

Get list AWS S3 bucket

aws s3 ls

Get list folder in your AWS S3 bucket

aws s3 ls s3://<S3_BUCKET_NAME> --recursive --human-readable --summarize | awk '{print $5}' | awk -F '/' '/\// {print $1}' | sort -u

Copy file from local to bucket

aws s3 cp <path-to-file-from-local> s3://<S3_BUCKET_NAME>/<folder-name> --acl public-read

The above command don’t return object’s url for you but you can construct object’s url according to path style access as follows

https://<region>.amazonaws.com/<bucket-name>/<key>

Where <region> is something like s3-ap-southeast-2

Note that path style url will be replaced by virtual hosted‐style and deprecated soon . See more here.


Opinion are my own. Thank you for reading.