Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

 

(s3) commands with the AWS CLI


This topic describes how you can manage Amazon S3 buckets and objects using the aws
s3 commands in the AWS CLI.

Create a bucket
$ aws s3 mb <target> [--options]
s3 mb examples
The following example creates the s3://bucket-name bucket.

$ aws s3 mb s3://bucket-name

List buckets and objects


Syntax

$ aws s3 ls <target> [--options]


s3 ls examples
The following example lists all of your Amazon S3 buckets.

$ aws s3 ls
2018-12-11 17:08:50 my-bucket
2018-12-14 14:55:44 my-bucket2

Delete buckets
Syntax

$ aws s3 rb <target> [--options]


s3 rb examples
The following example removes the s3://bucket-name bucket.

$ aws s3 rb s3://bucket-name
Delete objects
Syntax

$ aws s3 rm <target> [--options]

s3 rm examples

The following example deletes filename.txt from s3://bucket-name/example.

$ aws s3 rm s3://bucket-name/example/filename.txt --recursive

Move objects
Use the s3 mv command to move objects from a bucket or a local directory.

Syntax

$ aws s3 mv <source> <target> [--options]

s3 mv examples

The following example moves all objects from s3://bucket-name/example to s3://my-


bucket/.

$ aws s3 mv s3://bucket-name/example s3://my-bucket/

Copy objects
Use the s3 cp command to copy objects from a bucket or a local directory.

Syntax

$ aws s3 cp <source> <target> [--options]


s3 cp examples
The following example copies all objects from s3://bucket-name/example to s3://my-
bucket/.

$ aws s3 cp s3://bucket-name/example s3://my-bucket/

The following example copies a local file from your current working directory to the Amazon S3
bucket with the s3 cp command.

$ aws s3 cp filename.txt s3://bucket-name

The following example copies a file from your Amazon S3 bucket to your current working
directory, where ./ specifies your current working directory.

$ aws s3 cp s3://bucket-name/filename.txt ./

The following example uses echo to stream the text "hello world" to the s3://bucket-
name/filename.txt file.

$ echo "hello world" | aws s3 cp - s3://bucket-name/filename.txt

The following example streams the s3://bucket-name/filename.txt file to stdout and


prints the contents to the console.

$ aws s3 cp s3://bucket-name/filename.txt -
hello world

The following example streams the contents of s3://bucket-name/pre to stdout, uses


the bzip2 command to compress the files, and uploads the new compressed file
named key.bz2 to s3://bucket-name.

$ aws s3 cp s3://bucket-name/pre - | bzip2 --best | aws s3 cp - s3://bucket-


name/key.bz2

Sync objects
Syntax

$ aws s3 sync <source> <target> [--options]


s3 sync examples
The following example synchronizes the contents of an Amazon S3 prefix named path in the
bucket named my-bucket with the current working directory.

s3 sync updates any files that have a size or modified time that are different from files with the
same name at the destination. The output displays specific operations performed during the sync.
Notice that the operation recursively synchronizes the subdirectory MySubdirectory and its
contents with s3://my-bucket/path/MySubdirectory.

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


upload: MySubdirectory\MyFile3.txt to s3://my-
bucket/path/MySubdirectory/MyFile3.txt
upload: MyFile2.txt to s3://my-bucket/path/MyFile2.txt
upload: MyFile1.txt to s3://my-bucket/path/MyFile1.txt

The following example, which extends the previous one, shows how to use the --
delete option.

// Delete local file


$ rm ./MyFile1.txt

// Attempt sync without --delete option - nothing happens


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

// Sync with deletion - object is deleted from bucket


$ aws s3 sync . s3://my-bucket/path --delete
delete: s3://my-bucket/path/MyFile1.txt

// Delete object from bucket


$ aws s3 rm s3://my-bucket/path/MySubdirectory/MyFile3.txt
delete: s3://my-bucket/path/MySubdirectory/MyFile3.txt

// Sync with deletion - local file is deleted


$ aws s3 sync s3://my-bucket/path . --delete
delete: MySubdirectory\MyFile3.txt

// Sync with Infrequent Access storage class


$ aws s3 sync . s3://my-bucket/path --storage-class STANDARD_IA
***************My work

AWS account: 471611652126

User name: demoCLIUser1

Access key ID: AKIAW3TR6XAPNBPPPO7S

Secret access key: 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

C:\Users\Laxminanda>aws configure

AWS Access Key ID [****************PO7S]: AKIAW3TR6XAPNBPPPO7S

AWS Secret Access Key [****************5Oyk]: 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

Default region name [Ohio]: us-east-2

Default output format [key]: json

C:\Users\Laxminanda>aws s3 mb s3://myclidemobucket

make_bucket: myclidemobucket

C:\Users\Laxminanda>aws s3 cp C://Users/Laxminanda/Desktop/largefile.txt s3://myclidemobucket/

Completed 11.0 MiB/1.0 GiB (195.3 KiB/s) with 1 file(s) remaining

********************

You might also like