Home Blogs How to Upload Objects Using both the CLI and API methods in S3 bucket

How to Upload Objects Using both the CLI and API methods in S3 bucket

by Anup Maurya
2 minutes read
How to Upload Objects Using both the CLI and API methods in S3 bucket

Amazon Simple Storage Service (S3) is a highly scalable and reliable cloud storage service provided by Amazon Web Services (AWS). It allows businesses and developers to store and retrieve any amount of data from anywhere on the web.

In this article, we’ll learn about how to Create an S3 bucket and upload objects using both the Command Line Interface (CLI) and Application Programming Interface (API) methods.

Prerequisites for the lab,

  1. AWS Account: Sign up for an AWS account if you haven’t already done so.
  2. AWS CLI: Install and configure the AWS CLI on your local machine.
  3. Programming Language: Choose a programming language (such as Python, Java, or Node.js) to interact with the S3 API.

Installation Guide for AWS CLI for Linux , Windows and Mac Machine .

Creating an S3 Bucket via CLI

The AWS CLI provides a command-line interface to interact with various AWS services, including S3. Follow these steps to create an S3 bucket using the CLI:

Step 1: Open your terminal or command prompt and verify that the AWS CLI is installed by running the following command:

aws  version

Step 2: Configure the AWS CLI by executing the following command and providing your AWS access key ID, secret access key, and preferred AWS region:

aws configure

Step 3: Create an S3 bucket by running the following command, replacing “your-bucket-name” with your desired bucket name:

aws s3 mb s3://your-bucket-name

Congratulations! You have successfully created an S3 bucket using the AWS CLI. Now let’s proceed to upload objects.

Uploading Objects via CLI

To upload objects to your S3 bucket using the CLI, follow these steps:

Step 1: Navigate to the directory containing the file you want to upload.

Step 2: Use the following command to upload a file to your S3 bucket, replacing “your-bucket-name” and “your-file-name” with the appropriate values:

aws s3 cp your-file-name s3://your-bucket-name

That’s it! You have now uploaded an object to your S3 bucket using the CLI.

Creating an S3 Bucket via API

In addition to the CLI, you can also create an S3 bucket programmatically using the AWS S3 API. Here’s an example using Python and the AWS SDK (boto3):

Step 1: Install the AWS SDK for Python (boto3) by executing the following command:

pip install boto3

Step 2: Import the necessary libraries and create an S3 client instance using your AWS credentials:

import boto3

s3_client = boto3.client(‘s3’, aws_access_key_id=’YOUR_ACCESS_KEY’,
aws_secret_access_key=’YOUR_SECRET_KEY’)

Step 3: Create an S3 bucket by calling the create_bucket method on the S3 client:

s3_client.create_bucket(Bucket=’your-bucket-name’)

Uploading Objects via API

Step 4: Use the upload_file method to upload a file to your S3 bucket

s3_client.upload_file(‘your-file-name’, ‘your-bucket-name’, ‘your-object-key’)

WHoa!! You have now uploaded an object to your S3 bucket using the Python and the AWS SDK (boto3) API.

related posts

Leave a Comment

Enable Notifications OK No thanks