Usar pares de chave-valor em uma função - Amazon CloudFront

Usar pares de chave-valor em uma função

É possível usar pares de chave-valor de um armazenamento de chave-valor em uma função.

O exemplo a seguir mostra uma função que usa o conteúdo do URL na solicitação HTTP para pesquisar um caminho personalizado no armazenamento de chave-valor. Depois, o CloudFront usa esse caminho personalizado para fazer a solicitação. Essa função ajuda a gerenciar os vários caminhos que fazem parte de um site.

import cf from 'cloudfront'; ​ // Declare the ID of the key value store that you have associated with this function // The import fails at runtime if the specified key value store is not associated with the function const kvsId = "a1b2c3d4-5678-90ab-cdef-EXAMPLE11111"; ​ const kvsHandle = cf.kvs(kvsId); ​ 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; }