Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I return a json file from s3 to a specific url, but only that url

I have a special URL that has been set somewhere else and CANNOT be changed.

mydomain.com/discountCards

I need to return a JSON, from my s3 bucket whenever a server sends a fetch call to mydomain.com/discountCards

The issue is:

I'm using a react app on mydomain.com with React-Router. So going to mydomain.com/discountCards displays a white react page (since nothing is at that url)

I'm trying to set up Cloudfront to directly link to the s3 JSON file which works - but the issue is:

it only works for mydomain.com/discountCards.json not mydomain.com/discountCards

So.... right now my s3 bucket has a file called discountCards.json - but I need that to be discountCard without the ".json".

Or something else?

How do I return this .json from my s3, as a specific domain, using AWS s3, cloudfront, and route 53?

Like I said, I got it all set up with cloudfront pointing to my s3 file, but it requires I include the file extension in the URL (which I cannot do, the url is already locked in stone at mydomain.com/discountCards not discountCards.json)

This is so that other websites and apps can fetch that URL and get a json returned. Any other way to achieve this?

like image 385
Zach Avatar asked Mar 04 '23 00:03

Zach


1 Answers

So.... right now my s3 bucket has a file called discountCards.json - but I need that to be discountCard without the ".json"

Save your file discountCard.json as discountCard without .json extension. See the image below

enter image description here

Then on properties under metadata add content-type to application/json see image below

enter image description here

Accessing the bucket is now possible through this url https://s3.amazonaws.com/stackkkover/discountCards

This answer was inspired by this post https://stackoverflow.com/a/27046083/3521330

I hope this answer will be helpful. Thank you.

like image 161
muya.dev Avatar answered Apr 08 '23 16:04

muya.dev