This page looks best with JavaScript enabled

Using Amazon S3 with the AWS CLI

 ·  β˜• 3 min read

Create IAM user

First, create a IAM user


click iam

Click Access management/Users


click iam user

Click Add user

click iam create user

Add user

Fill in User name

Access type: Programmatic access


click iam create user 1

Set permissions

Select Attach existing policies directly

Select AdministratorAccess

Set permissions boundary / Create user without a permissions boundary


click iam create user 2

Add tags (optional)

Skip it


click iam create user 3

Review

Confirm the information is correct


click iam create user 4

Success

Download the CSV


click iam create user 5

AWS CLI

Install AWS CLI

Next, install AWS CLI


Run CMD
Enter aws --version to check information


aws version

Configuration

Input aws configure, check the CSV file, Enter the following in order

  • AWS Access Key ID (The string that only contain uppercase characters)
  • AWS Secret Access Key (The longest string)
  • Default region name (ap-northeast-1 (Tokyo) or others)
  • Default output format (json)

aws configure

You can use AWS CLI now.

Basic

Use command aws s3 interact with S3


s3 mb Create Bucket

1
2
aws s3 mb s3://BACKET_NAME
aws s3 mb s3://BACKET_NAME --region REGION_NAME

s3 ls list bucket name or files and folders in bucket

1
2
3
aws s3 ls	# list buckets
aws s3 ls s3://BACKET_NAME
aws s3 ls s3://BACKET_NAME/FOLDER_NAME/

s3 rb delete bucket

1
2
aws s3 rb s3://BACKET_NAME	# bucket must be empty
aws s3 rb s3://BACKET_NAME --force	# force to delete

Others s3 cp、s3 mv、s3 rm、s3 sync
Similar with Windows and Linux commands

Use it

Use AWS CLI to upload

Use s3 sync command to sync
Any file with the same file name as the destination file but with a different file size or modification time will be updated.

1
2
3
4
5
6
7
8
aws s3 sync ORIGIN DESTINATION

# e.g.
aws s3 sync "D:\Blog\public" s3://blog.mytest.com --delete --grants read=uri=http://acs.amazonaws.com/groups/global/AllUsers full=id=YOUR_CANONICAL_USER_ID

# that means D:\Blog\public sync to S3 blog.mytest.com bucket
# delete the objects in the bucket that do not exist in the D:\Blog\public folder
# give everyone read access and full access to your AWS account

Canonical User ID can find from My security Credentials/Account identifiers


aws id

Just one line, it can replace a series of actions of opening a browser, logging into AWS, and uploading files to S3.

Not over yet, write it as Batch file
Copy and paste the new file below and change the variables LOCAL、BUCKET、AWS_ID
Save as a new file with the extension .bat

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@ECHO off
mode con:cols=100 lines=25

::----------
:Start
set LOCAL=LOCAL_PATH
set BUCKET=S3_BUCKET_NAME
set AWS_URI=http://acs.amazonaws.com/groups/global/AllUsers
set AWS_ID=YOUR_CANONICAL_USER_ID

::----------
CLS
ECHO -----------------------------------------------------------------------------------------------
ECHO LOCAL     %LOCAL%
ECHO BUCKET    %bucket%
ECHO -----------------------------------------------------------------------------------------------
ECHO READY...
PAUSE
goto S3_Sync

::----------
:S3_Sync
ECHO ----------
aws s3 sync %LOCAL% %BUCKET% --delete --grants read=uri=%AWS_URI% full=id=%AWS_ID%
ECHO ----------
ECHO DONE!
PAUSE

::----------
:End
exit

Just execute this Batch file in the future.


demo

Done

Share on
Support the author with

JIHONGO
WRITTEN BY
JIHONGO
A Person