# Cloud Uploader CLI

## Objective

The aim is to create a bash-based CLI tool that allows users to quickly upload files to a specified cloud storage solution. This is part of a learning phase from [https://learntocloud.guide/](https://learntocloud.guide/), a free guide to learn cloud computing. AWS S3 Storage was used in this mini project.

The tool should be able to upload files to cloud provider storage when the command below is ran

```bash
clouduploader /path/to/file.txt
```

## Setup

1. Download AWS Command Line Interface (CLI)
    
2. Prepare your AWS Access Key ID and AWS Secret Access Key
    
3. Once 1 and 2 are ready, enter the following command in command prompt
    
    ```bash
    aws configure
    ```
    
    You will be prompted to enter your AWS Access Key ID, AWS Secret Access Key, Default region name and Default output format.
    

## Create S3 Bucket

Create a new bucket programmatically by entering the following command. The bucket name must be globally unique. In this scenario, the bucket name is wj-demo-bucket1

```bash
aws s3 mb s3://wj-demo-bucket1
```

The following output is seen after the bucket is created successfully.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707612250852/6af800a1-e619-4766-a656-0a1400661b37.png align="left")

## Bash script to upload file to S3 bucket

Create a directory and save this script in this directory before writing the script. In this case, the name of the script is s3\_uploader\_cli.sh

The bash script takes in 2 arguments which are filename and the destination bucket. If either of them is not set, an error message will be printed and script will not be executed.

```bash
#!/bin/bash

filename=$1
dest_bucket=$2


if [ -z "$filename" ]; then
    echo "File not specified!"
    exit 1
elif [ -z "$dest_bucket" ]; then
    echo "Destination bucket not specified!"
    exit 1    
fi
```

An error message of file not specified when filename is missing

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707614580665/335fcec4-070a-4cb0-a660-daa2c80d5252.png align="left")

An error message of destination bucket not specified when bucket name is missing

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707614703019/041ab456-b3a2-4c2f-8747-db7766d8edef.png align="left")

Next, the script checks if the file exists before uploading. A message will be displayed to indicate if the attempt to upload the file is successful.

```bash
if [ ! -f "$filename" ]; then
    echo "File not found!"
    exit 1
elif aws s3 cp $filename $dest_bucket; then
    echo "File uploaded successfully!"
else
    echo "File upload failed!"
    exit 1
fi
```

A failed attempt to upload a file which does not exist

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707616405960/6c2eb079-e24a-4ad3-8235-6c2d6bd90fcd.png align="left")

A successful attempt to upload a file with filename of README.md

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707616356389/3c3a9e83-104e-409f-b942-6d6f350df9e0.png align="left")

The file can be seen by listing the files or objects of this bucket

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1707616553012/e35dd0b2-3f3b-444d-bb9e-3122236371bc.png align="left")

The full bash script is as shown below

```bash
#!/bin/bash

filename=$1
dest_bucket=$2


if [ -z "$filename" ]; then
    echo "File not specified!"
    exit 1
elif [ -z "$dest_bucket" ]; then
    echo "Destination bucket not specified!"
    exit 1    
fi


if [ ! -f "$filename" ]; then
    echo "File not found!"
    exit 1
elif aws s3 cp $filename $dest_bucket; then
    echo "File uploaded successfully!"
else
    echo "File upload failed!"
    exit 1
fi
```

## Conclusion

Bash scripting is a basic and essential skill to develop in the sphere of cloud computing.

Happy bash scripting :)

## My Github Repo

[https://github.com/pohwj/wj\_bash\_scripts](https://github.com/pohwj/wj_bash_scripts)
