如何使用meteor在base64中编码字符串

3 base64 amazon-s3 meteor

我正在尝试使用表单将文件上传到使用Meteor的s3存储桶.我正在关注这篇亚马逊文章.在"签署您的S3 POST表单",接近结尾,我需要将字符串编码为base64,但我一直无法找到这样做的方法.谁能告诉我怎么做?请注意,首先需要对字符串进行编码然后进行签名.这是在python中完成的:

import base64
import hmac, hashlib

policy = base64.b64encode(policy_document)
signature = base64.b64encode(hmac.new(AWS_SECRET_ACCESS_KEY, policy, hashlib.sha1).digest())
Run Code Online (Sandbox Code Playgroud)

Mor*_*ter 9

你可以在没有NodeJS加密模块的情况下做到这一点,创建一个看起来有点像打破飞轮的包给我,所以我想出了这个:

if (Meteor.isServer) {
  Meteor.methods({
    'base64Encode':function(unencoded) {
      return new Buffer(unencoded || '').toString('base64');
    },
    'base64Decode':function(encoded) {
      return new Buffer(encoded || '', 'base64').toString('utf8');
    },
    'base64UrlEncode':function(unencoded) {
      var encoded = Meteor.call('base64Encode',unencoded);
      return encoded.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
    },
    'base64UrlDecode':function(encoded) {
      encoded = encoded.replace(/-/g, '+').replace(/_/g, '/');
      while (encoded.length % 4)
        encoded += '=';
      return Meteor.call('base64Decode',encoded);
    }
    console.log(Meteor.call('base64Encode','abc'));
});
Run Code Online (Sandbox Code Playgroud)

这是基于John Hurliman在https://gist.github.com/jhurliman/1250118上找到的base64.js.注意,这将像服务器上的一个魅力一样,但是为了将它移植到客户端,你已经调用了方法一个回调函数,用于将结果存储为会话变量.