Verwenden von Schlüssel-Wert-Paaren in einem Viewer-Anforderungsereignis von CloudFront-Funktionen - AWS-SDK-Codebeispiele

Weitere AWS-SDK-Beispiele sind im GitHub-Repository Beispiele für AWS Doc SDKs verfügbar.

Verwenden von Schlüssel-Wert-Paaren in einem Viewer-Anforderungsereignis von CloudFront-Funktionen

Das folgende Codebeispiel zeigt, wie Schlüssel-Wert-Paare in einem Viewer-Anforderungsereignis von CloudFront-Funktionen verwendet werden.

JavaScript
JavaScript-Laufzeitumgebung 2.0 für CloudFront-Funktionen
Anmerkung

Auf GitHub finden Sie noch mehr. Das vollständige Beispiel finden Sie im Repository mit Beispielen für CloudFront-Funktionen, wo Sie auch erfahren, wie Sie diese einrichten und ausführen.

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