Uploading or Downloading Objects from S3 Bucket using AWS CLI Commands

The aws s3 cp command can be used for both uploading files to S3 bucket and downloading files from S3 bucket.

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

  • source:
    • For uploading files to s3 bucket this option set to local path that you want to upload to s3.
    • For downloading files from s3 bucket this set to S3 URN.
  • target:
    • For uploading files to s3 bucket this set to URN of S3 bucket.
    • For downloading the files from s3 bucket this is set to local path where you want to save the downloaded file.
  • options: These are optional parameters to modify the original behavior of the command. Here are some commonly used optionsal parameters
    • –recursive: If <source> or <target> is a directory then this parameter will recursively upload/downloads the files.
    • –acl: This parameter specifies the access control list for object
    • –sse: Specifies the server-side encryption method for the uploaded object.

Upload The Objects In S3

Step 1: In this example we are uploading the all the files present in directory to s3 bucket(gfg-example)

Objects in s3 before uploading

aws s3 cp <directory-name> s3://gfg-example --recursive

Command execution

Objects in s3 after uploading

Download the Objects from S3

Step 1: In this example we just download the one file from above s3 bucket.

aws s3 cp s3://gfg-example/1.here-there-and-everywhere.pdf C:\Users\ravis\Desktop

Step 2: The above command downloads the 1.here-there-and-everywhere.pdf from gfg-example bucket to local Desktop.

Downloading file

The aws s3 rb command is used to delete s3 bucket and aws s3 rm is used to delete the objects from s3 bucket.

aws s3 rb <S3 URN>

The aws s3 rb deletes the s3 bucket if and only if bucket is empty

aws s3 em <S3 URN>

We can delete all the objects from s3 using –recursive option parameter.

Deleting S3 bucket using S3 cmd

Step 1: Deleting the one object from s3 bucket(gfg-example)

aws s3 rm s3://gfg-example/1.here-there-and-everywhere.pdf

Step 2: The above command will delete object named with 1.here-there-and-everywhere.pdf

Deleting

Step 3: In this example first we try to delete the s3 bucket which is empty(gfg-example) then we will empty the bucket and then delete the s3 bucket(gfg-example)

aws s3 rb s3://gfg-example

Step 4: If the bucket is empty then above cmb directly delete bucket without any error. other wise it will give BucketNotEmptyError

aws s3 rm s3://gfg-example --recursive  #first delete all the objects present in given bucket
aws s3 rb s3://gfg-example #delete the bucket

Delete Example

How to Create AWS S3 Bucket using CLI ?

Amazon S3 is a Simple Storage Service in AWS that stores files of different types like Photos, Audio, and videos, as objects, providing more scalability and security. It allows users to store and retrieve any amount of data at any point in time from anywhere on the web. It facilitates features such as extremely high availability, security, and simple connections to other AWS Services. In this article, we will see how to create s3 buckets using AWS CLI

Similar Reads

Prerequisites

Introduction to s3 AWS CLI...

Creating S3 Bucket Using CLI

The AWS S3 command is part of the AWS Command Line Interface (CLI) and serves as a tool for interacting with Amazon Simple Storage Service (S3). The AWS s3 mb command creates a new bucket in Amazon S3. It’s a straightforward way to initiate the creation of a storage container within AWS. mb stands for make bucket The bucket name should be globally unique...

Uploading or Downloading Objects from S3 Bucket using AWS CLI Commands

The aws s3 cp command can be used for both uploading files to S3 bucket and downloading files from S3 bucket....

Creating S3 Bucket with low level S3 API Command

The s3api is a command that allows users to interact with Amazon Simple Storage Service (S3) through the AWS CLI. With s3api, users can perform advanced operations on S3 buckets and objects such are configure bucket policies, set access control lists (ACLs), manage versioning, configure lifecycle policies, and more....

Create AWS S3 Bucket Using CLI – FAQ’s

What is the use of an AWS S3 Bucket?...