首页 > 解决方案 > 使用 CloudFront 功能时如何进行 base64 编码/解码?

问题描述

Amazon CloudFront Function 是 AWS 推出的一项新功能。
CloudFront 函数只能使用 JavaScript 编写。
https://aws.amazon.com/blogs/aws/introducing-cloudfront-functions-run-your-code-at-the-edge-with-low-latency-at-any-scale/

我们的网站生成时间戳并使用 btoa()(Base64 编码)对其进行编码。
然后,网站向 CloudFront 函数发送包含编码时间戳的 HTTP GET 请求。

var sending_time        = new Date().getTime();
var enc_sending_time    = btoa(sending_time);

function generate_http_request(enc_sending_time)
{
...
}

CloudFront 函数收到 HTTP 请求后,
应使用 atob() 解码时间戳。

但是,CloudFront 函数不支持 atob()。 https://docs.aws.amazon.com/AmazonCloudFront/latest/DeveloperGuide/functions-javascript-runtime-features.html

我们如何在 不使用 btoa() 和 atob()的情况下将 base64 编码为整数
,然后在 CloudFront 函数端进行 base64 解码?
(仅限 JavaScript)

标签: javascriptjqueryamazon-web-servicesencoding

解决方案


CloudFront 不支持 btoa() 和 atob() 函数,
因此我们无法使用这些函数进行 Base 64 编码/解码。

因此,作为替代方案,我们可以使用以下内容:
https ://gist.github.com/oeon/0ada0457194ebf70ec2428900ba76255

奇迹般有效!


推荐阅读