The repository showcases how to create a secured S3 bucket and utilize it through a CloudFront distribution using an Express server written in TypeScript. The server leverages an API call to acquire a signed URL with a 5-minute validity, granting access to an image file uploaded in the S3 bucket.
- Clone this repository:
git clone https://github.com/Muhsin-42/S3Bucket-CloudFront.git
- Install the dependencies:
npm install
- Create a private S3 bucket and upload an image file to it.
- Create a CloudFront distribution, setting the S3 bucket as the origin.
- Obtain the CloudFront distribution domain name.
- Generate an RSA 2048 public key to be used as a key pair on CloudFront. Save the public key in a file named
public_key.pem
and private key asprivate_key.pem
. - Create
.env
file with the following variables:CLOUDFRONT_URL
: The CloudFront distribution domain URL.CLOUDFRONT_KEY_PAIR_ID
: Your CloudFront key pair ID.CLOUDFRONT_PRIVATE_KEY
: Your CloudFront private key.
- Start the Express server:
npm run dev
- API EndPoint:
http://localhost:8800/api/image
. - The server will make an API call to the CloudFront distribution to obtain a signed URL for the image file with a 5-minute expiry time.
- The signed URL will be logged in the server console.
- Copy the signed URL and paste it into a browser to view the image file.
- The URL is valid till 5 minutes only.
npm run dev