AWS CDK: Building a REST API to Upload and Download Files from Amazon S3

GitHub: CDK Stack

Today, we’ll build an API to upload and download files to and from Amazon Simple Service (S3) respectively using direct service integration, AWS CDK to provision our infrastructure, and typescript as the programming language.

Here are the endpoints we’ll build

  • GET: /{bucketName} to list objects in a bucket
  • GET: /{bucketName}/{fileName} to get/download a given file
  • PUT: /{bucketName}/{fileName} to upload a file to the specified bucket.

1) Requirements:

2) Initialize a CDK application

We have chosen Amazon S3 Standard to build this workshop, as it is the best option for a simple and beginner-friendly experience.

Let’s create a new CDK application. Create an empty folder (cdk-s3-upload-download in my case). Feel free to use a name of your choice. Open the folder in your terminal and run the following command:

cdk init app --language=typescript

You should have this folder structure created by default, once you open up the application in an IDE. I recommend VS Code for its awesomeness.

cdk-s3-upload-download
├── bin
│ └── cdk-s3-upload-download.ts
├── lib
│ └── cdk-s3-upload-download-stack.ts
├── package.json
├── tsconfig.json
└── README.md
  • The bin directory contains the entry point for your CDK app. This is the file that will be executed when you run cdk deploy.
  • The lib directory contains the source code for your CDK app. This is where you will define your stacks, resources, and other constructs.
  • The package.json file contains the dependencies for your CDK app. This file is used by npm to install the dependencies when you run npm install.
  • The tsconfig.json file contains the TypeScript configuration for your CDK app. This file tells TypeScript how to compile your code.
  • The README.md file is a README file for your CDK app. This file should contain information about your app, such as how to install and run it.

Here is a brief explanation of each file:

bin/cdk-s3-upload-download.ts: This is the entry point for your CDK app. This file will be executed when you run cdk deploy. It imports the cdk-s3-upload-download-stack from the lib directory and deploys it.

lib/cdk-s3-upload-download-stack.ts: This is the source code for your CDK app. This file defines the CdkS3UploadDownloadStack class, which is a custom stack that you can use to deploy your cloud infrastructure.

package.json: This file contains the dependencies for your CDK app. This file is used by npm to install the dependencies when you run npm install.

tsconfig.json: This file contains the TypeScript configuration for your CDK app. This file tells TypeScript how to compile your code.

README.md: This is a README file for your CDK app. This file should contain information about your app, such as how to install and run it.

3) Build the stack

Open the file cdk-s3-upload-download-stack.ts found in thelib directory.

Copy into it the content found here

Change bucket name and region

4) Deployment

For a successful deployment to an AWS account, run the following commands:

npm run build

This builds the project and generates the corresponding *.js and *.d.ts files from the provided *.ts files.

cdk bootstrap

This command bootstraps your code and creates a deployment environment.

cdk deploy

This command generates a CloudFormation template based on the resources defined within the CDK stack and deploys them to your AWS account.

5) Validating

5.1) Configure AWS Signature in postman

To configure aws credential in Postman, do the following

  • Launch post
  • Navigate to the authorization tab.
  • Click on the type dropdown and select AWS Signature
  • Provide the AccessKey, SecretKey, and AWS Region
Image 1 — Configure aws credential in Postman

5.2) List S3 objects

Send a GET request to the provided URL

https://<randomKey>.execute-api.<region>.amazonaws.com/prod/workshop-storage-bucket/

The response will contain the bucket name and content of the bucket

Image 2 — GET request

5.3) Upload image object to S3

Send a PUT request to the API endpoint.

https://2m40aionu8.execute-api.us-east-1.amazonaws.com/prod/workshop-storage-bucket/<key>

Provide the key for the object you want to upload. The key takes any string for example

https://2m40aionu8.execute-api.us-east-1.amazonaws.com/prod/workshop-storage-bucket/image

Sending a PUT request to the above API endpoint will create an object with the key image in an S3 bucket with the name workshop-storage-bucket.

Note: The extension of the file you are uploading should match what was defined in the project stack. .jpeg

Image 3 — PUT request

5.4) Get image object from s3

To get an object from S3, send a GET request to the API endpoint providing the name of the bucket and the key of the object you want to get.

Image 4 — Get image

Learn more AWS CDK: Building a REST API to Upload and Download Files from Amazon S3

Leave a Reply