CloudFront Functions 뷰어 요청에서 키-값 페어 사용 - AWS SDK 코드 예제

AWS SDK 예제 GitHub 리포지토리에 더 많은 AWS문서 SDK 예제가 있습니다.

CloudFront Functions 뷰어 요청에서 키-값 페어 사용

다음 코드 예제에서는 CloudFront Functions 뷰어 요청에서 키-값 페어를 사용하는 방법을 보여줍니다.

JavaScript
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; }