Answers for "etworking-and-content-delivery/leverage-amazon-cloudfront-geolocation-headers-for-state-level-geo-targeting/"

0

etworking-and-content-delivery/leverage-amazon-cloudfront-geolocation-headers-for-state-level-geo-targeting/

import os
from urllib.parse import urlparse
 
def lambda_handler(event, context):
   
    request = event['Records'][0]['cf']['request']
   
    parsed_uri = urlparse(request['uri']).path
   
    root = build_root_path(parsed_uri)
    file_name= os.path.basename(parsed_uri).split(".")[0]
    suffix = build_suffix(request['headers']);
    extension = os.path.splitext(parsed_uri)[1]
    
    request['uri'] = root + file_name + suffix + extension;
 
    return request
  
def build_root_path(parsed_uri):
    root = os.path.split(parsed_uri)[0]
    return root if (root == "/") else rootDir + "/"
   
def build_suffix(headers):
    country = headers['cloudfront-viewer-country'][0]['value']
    if (country == 'US'):
        return '_' + headers['cloudfront-viewer-country-region'][0]['value'].lower()
    else:
        return ''
Posted by: Guest on September-13-2021

Browse Popular Code Answers by Language