How to map subdomain to specific path in s3 bucket.

0

I have s3 bucket storing json file on folder named content and images on image folder under content folder, so the object tree inside bucket is my_s3_bucket->content/a.json and my_s3_bucket/content/image/a.png. I want to browse json file while https://text.example.com/abc/a.json and image in https://image.example.com/a.png

2 Answers
0

You can use Amazon CloudFront with S3 bucket as the origin, and then set up different behaviors based on the subdomain. https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/DownloadDistS3AndCustomOrigins.html

Here are the steps:

  • Go to the CloudFront console and create a new distribution.

    • Choose Web distribution.
    • For the origin, select your S3 bucket.
  • Navigate to behaviors tab, for the first behavior (default), the path pattern can be * which will match everything. Set the origin path to /content.

    • Create a second behavior with the path pattern image/*. Set the origin path to /content/image. In your AWS Route 53, you need to create two A records:
      1. text.example.com – Points to the CloudFront distribution you set up.
      2. image.example.com – Points to the same CloudFront distribution.
  • Wait for the CloudFront distribution to be deployed and test.

AWS
Rumaisa
answered 9 months ago
0

origin JSON file: my_s3_bucket->content/a.json

origin PNG file: my_s3_bucket/content/image/a.png

requesting JSON URL: https://text.example.com/abc/a.json

requesting PNG URL: https://image.example.com/a.png

there is no way you can use simply use origin and behavior configurations to achieve the result.

You can consider to use a CloudFront Functions (CFF) to rewrite the requesting path without modifying the requesting URL. You can use a single CFF or separate CFF.

In the behavior settings, create two behaviors, one behavior is " *.json" and another one is " *.png" (applying without double quote). Document source here. the origin can be the same, the S3 bucket.

In each behavior, associate with the CFF Viewer Function, based on the suffix, json or png, modify the path to match the path of file in origin S3 bucket.

for example, if the requesting path is content/a.json before be processed by CFF , the outcome path will be modified to 'abc/a.json'.

CFF code example for reference.

answered 9 months ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions