Utilizzo di coppie chiave-valore in una richiesta visualizzatore di Funzioni CloudFront - Amazon CloudFront

Utilizzo di coppie chiave-valore in una richiesta visualizzatore di Funzioni CloudFront

L’esempio di codice seguente mostra come utilizzare le coppie chiave-valore in una richiesta visualizzatore di Funzioni CloudFront.

JavaScript
Ambiente runtime JavaScript 2.0 per Funzioni CloudFront
Nota

Ulteriori informazioni su GitHub. Trova l’esempio completo e scopri come eseguire la configurazione e l’esecuzione nel repository degli esempi di Funzioni CloudFront.

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

Per un elenco completo delle guide per gli sviluppatori di SDK AWS ed esempi di codice, consulta la sezione Utilizzo di CloudFront con un SDK AWS. Questo argomento include anche informazioni su come iniziare e dettagli sulle versioni precedenti dell'SDK.