選取您的 Cookie 偏好設定

我們使用提供自身網站和服務所需的基本 Cookie 和類似工具。我們使用效能 Cookie 收集匿名統計資料,以便了解客戶如何使用我們的網站並進行改進。基本 Cookie 無法停用,但可以按一下「自訂」或「拒絕」以拒絕效能 Cookie。

如果您同意,AWS 與經核准的第三方也會使用 Cookie 提供實用的網站功能、記住您的偏好設定,並顯示相關內容,包括相關廣告。若要接受或拒絕所有非必要 Cookie,請按一下「接受」或「拒絕」。若要進行更詳細的選擇,請按一下「自訂」。

根據 CloudFront Functions 檢視器請求事件的 KeyValueStore 組態重寫請求 URI

焦點模式
根據 CloudFront Functions 檢視器請求事件的 KeyValueStore 組態重寫請求 URI - Amazon CloudFront

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

下列程式碼範例示範如何根據 CloudFront Functions 檢視器請求事件的 KeyValueStore 組態重寫請求 URI。

JavaScript
CloudFront Functions 的 JavaScript 執行期 2.0
注意

GitHub 上提供更多範例。尋找完整的範例,並了解如何在 CloudFront Functions 範例儲存庫中設定和執行 。

import cf from 'cloudfront'; // (Optional) Replace KVS_ID with actual KVS ID const kvsId = "KVS_ID"; // enable stickiness by setting a cookie from origin or using another edge function const stickinessCookieName = "appversion"; // set to true to enable console logging const loggingEnabled = false; // function rewrites the request uri based on configuration in KVS // example config in KVS in key:value format // "latest": {"a_weightage": .8, "a_url": "v1", "b_url": "v2"} // given above key and value in KVS the request uri will be rewritten // for example http(s)://domain/latest/something/else will be rewritten as http(s)://domain/v1/something/else or http(s)://domain/v2/something/else depending on weightage // if no configuration is found, then the request is returned as is async function handler(event) { // NOTE: This example function is for a viewer request event trigger. // Choose viewer request for event trigger when you associate this function with a distribution. const request = event.request; const pathSegments = request.uri.split('/'); const key = pathSegments[1]; // if empty path segment or if there is valid stickiness cookie // then skip call to KVS and let the request continue. if (!key || hasValidSticknessCookie(request.cookies[stickinessCookieName], key)) { return event.request; } try { // get the prefix replacement from KVS const replacement = await getPathPrefixByWeightage(key); if (!replacement) { return event.request; } //Replace the first path with the replacement pathSegments[1] = replacement; log(`using prefix ${pathSegments[1]}`) const newUri = pathSegments.join('/'); log(`${request.uri} -> ${newUri}`); request.uri = newUri; return request; } catch (err) { // No change to the path if the key is not found or any other error log(`request uri: ${request.uri}, error: ${err}`); } // no change to path - return request return event.request; } // function to get the prefix from KVS async function getPathPrefixByWeightage(key) { const kvsHandle = cf.kvs(kvsId); // get the weightage config from KVS const kvsResponse = await kvsHandle.get(key); const weightageConfig = JSON.parse(kvsResponse); // no configuration - return null if (!weightageConfig || !isFinite(weightageConfig.a_weightage)) { return null; } // return the url based on weightage // return null if no url is configured if (Math.random() <= weightageConfig.a_weightage) { return weightageConfig.a_url ? weightageConfig.a_url: null; } else { return weightageConfig.b_url ? weightageConfig.b_url : null; } } // function to check if the stickiness cookie is valid function hasValidSticknessCookie(stickinessCookie, pathSegment) { // if the value exists and it matches pathSegment return (stickinessCookie && stickinessCookie.value === pathSegment) } function log(message) { if (loggingEnabled) { console.log(message); } }
CloudFront Functions 的 JavaScript 執行期 2.0
注意

GitHub 上提供更多範例。尋找完整的範例,並了解如何在 CloudFront Functions 範例儲存庫中設定和執行 。

import cf from 'cloudfront'; // (Optional) Replace KVS_ID with actual KVS ID const kvsId = "KVS_ID"; // enable stickiness by setting a cookie from origin or using another edge function const stickinessCookieName = "appversion"; // set to true to enable console logging const loggingEnabled = false; // function rewrites the request uri based on configuration in KVS // example config in KVS in key:value format // "latest": {"a_weightage": .8, "a_url": "v1", "b_url": "v2"} // given above key and value in KVS the request uri will be rewritten // for example http(s)://domain/latest/something/else will be rewritten as http(s)://domain/v1/something/else or http(s)://domain/v2/something/else depending on weightage // if no configuration is found, then the request is returned as is async function handler(event) { // NOTE: This example function is for a viewer request event trigger. // Choose viewer request for event trigger when you associate this function with a distribution. const request = event.request; const pathSegments = request.uri.split('/'); const key = pathSegments[1]; // if empty path segment or if there is valid stickiness cookie // then skip call to KVS and let the request continue. if (!key || hasValidSticknessCookie(request.cookies[stickinessCookieName], key)) { return event.request; } try { // get the prefix replacement from KVS const replacement = await getPathPrefixByWeightage(key); if (!replacement) { return event.request; } //Replace the first path with the replacement pathSegments[1] = replacement; log(`using prefix ${pathSegments[1]}`) const newUri = pathSegments.join('/'); log(`${request.uri} -> ${newUri}`); request.uri = newUri; return request; } catch (err) { // No change to the path if the key is not found or any other error log(`request uri: ${request.uri}, error: ${err}`); } // no change to path - return request return event.request; } // function to get the prefix from KVS async function getPathPrefixByWeightage(key) { const kvsHandle = cf.kvs(kvsId); // get the weightage config from KVS const kvsResponse = await kvsHandle.get(key); const weightageConfig = JSON.parse(kvsResponse); // no configuration - return null if (!weightageConfig || !isFinite(weightageConfig.a_weightage)) { return null; } // return the url based on weightage // return null if no url is configured if (Math.random() <= weightageConfig.a_weightage) { return weightageConfig.a_url ? weightageConfig.a_url: null; } else { return weightageConfig.b_url ? weightageConfig.b_url : null; } } // function to check if the stickiness cookie is valid function hasValidSticknessCookie(stickinessCookie, pathSegment) { // if the value exists and it matches pathSegment return (stickinessCookie && stickinessCookie.value === pathSegment) } function log(message) { if (loggingEnabled) { console.log(message); } }

如需 AWS SDK 開發人員指南和程式碼範例的完整清單,請參閱 搭配 AWS SDK 使用 CloudFront 。此主題也包含有關入門的資訊和舊版 SDK 的詳細資訊。

隱私權網站條款Cookie 偏好設定
© 2025, Amazon Web Services, Inc.或其附屬公司。保留所有權利。