使用CloudFront函数时如何进行base64编码/解码?

qwe*_*rty 5 javascript jquery encoding amazon-web-services

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

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

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

function generate_http_request(enc_sending_time)
{
...
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

Dar*_*ngo 7

CloudFront添加了一些非标准方法,其中包括一种从 Base64 解码字符串的方法:

var decoded = String.bytesFrom(str, 'base64');

它还接受base64url编码版本。

他们在一个在边缘解码 JWT 的示例中展示了这一点。

不幸的是,他们似乎没有相当于进行 Base64 编码的功能。


qwe*_*rty 3

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

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

奇迹般有效!