简体   繁体   English

将Node.js中的s3存储桶策略从JSON转换为base64

[英]Converting an s3 bucket policy to base64 from JSON in node.js

I'm trying to encode an Amazon s3 bucket policy in base64 in node.js for the signature (more specifically the StringToSign), but can't seem to get the code right. 我正在尝试在node.js中的base64中将Amazon s3存储桶策略编码为签名(更具体地说是StringToSign),但是似乎无法正确获取代码。

I'm using a default policy provided by amazon to test with because they provide the correct base64 encoding. 我正在使用亚马逊提供的默认策略进行测试,因为它们提供了正确的base64编码。 ( The example given by amazon ) 亚马逊给出的例子

Their output is eyAiZXhwaXJhdGlvbiI6ICIyMDE1........ (Keeping it short because it's quite long) 他们的输出是eyAiZXhwaXJhdGlvbiI6ICIyMDE1........ (将其保持较短,因为它很长)

I keep getting eyJleHBpcmF0aW9uIjoiMjAxNS0xMi0zMFQx..... 我不断收到eyJleHBpcmF0aW9uIjoiMjAxNS0xMi0zMFQx.....

my code to generate this is: 我生成此代码是:

var policyString = JSON.stringify(policy)
var policyBytes = utf8.encode(policyString)
var stringToSign = base64.encode(policyBytes)

where policy is a variable holding the amazon policy in JSON format, and utf8 and base64 are node modules. 其中policy是一个以JSON格式保存亚马逊策略的变量,而utf8base64是节点模块。

This might sound like a novice question but I've been staring at it for awhile now and any hlep would be appreciated! 这听起来像是一个新手问题,但是我已经盯着它看了一段时间了,任何欢呼都将不胜感激!

I decoded the current partial values you have provided 我解码了您提供的当前部分值

eyAiZXhwaXJhdGlvbiI6ICIyMDE1
eyJleHBpcmF0aW9uIjoiMjAxNS0xMi0zMFQx

Respective outputs were: 各自的产出是:

{ "expiration": "2015
{"expiration":"2015-12-30T1

As you can see, in your policyString you have removed some spaces. 如您所见,您在policyString中删除了一些空格。 This is the reason for you to get a different output 这就是您获得不同输出的原因

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM