다음 코드 예제에서는 CloudFront Functions 뷰어 요청에서 키-값 페어를 사용하는 방법을 보여줍니다.
- CloudFront Functions를 위한 JavaScript 런타임 2.0
-
참고
GitHub에 더 많은 내용이 있습니다. CloudFront Functions 예제
리포지토리에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. import cf from 'cloudfront'; // This fails if there is no key value store associated with the function const kvsHandle = cf.kvs(); // Remember to associate the KVS with your function before referencing KVS in your code. // https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/kvs-with-functions-associate.html async function handler(event) { const request = event.request; // Use the first segment of the pathname as key // For example http(s)://domain/<key>/something/else const pathSegments = request.uri.split('/') const key = pathSegments[1] try { // Replace the first path of the pathname with the value of the key // For example http(s)://domain/<value>/something/else pathSegments[1] = await kvsHandle.get(key); const newUri = pathSegments.join('/'); console.log(`${request.uri} -> ${newUri}`) request.uri = newUri; } catch (err) { // No change to the pathname if the key is not found console.log(`${request.uri} | ${err}`); } return request; }
AWS SDK 개발자 가이드 및 코드 예시의 전체 목록은 AWS SDK와 함께 CloudFront 사용 섹션을 참조하세요. 이 주제에는 시작하기에 대한 정보와 이전 SDK 버전에 대한 세부 정보도 포함되어 있습니다.