Amazon s3 如何使用meteor对base64中的字符串进行编码
我正在尝试使用表单将文件上载到s3存储桶。我正在关注这个亚马逊。在“为S3帖子表单签名”的末尾,我需要将一个字符串编码为base64,但我一直找不到一种方法。谁能告诉我怎么做?请注意,字符串首先需要编码,然后签名。在python中是这样做的:Amazon s3 如何使用meteor对base64中的字符串进行编码,amazon-s3,meteor,base64,Amazon S3,Meteor,Base64,我正在尝试使用表单将文件上载到s3存储桶。我正在关注这个亚马逊。在“为S3帖子表单签名”的末尾,我需要将一个字符串编码为base64,但我一直找不到一种方法。谁能告诉我怎么做?请注意,字符串首先需要编码,然后签名。在python中是这样做的: import base64 import hmac, hashlib policy = base64.b64encode(policy_document) signature = base64.b64encode(hmac.new(AWS_SECRET_
import base64
import hmac, hashlib
policy = base64.b64encode(policy_document)
signature = base64.b64encode(hmac.new(AWS_SECRET_ACCESS_KEY, policy, hashlib.sha1).digest())
您需要NodeJS加密模块来执行这些任务 首先在meteor项目的根目录下创建一个“packages”目录,然后创建一个“MyPackage”目录。 在它里面,您需要两个文件:“package.js”和“my package.js” package.js应该如下所示:
Package.describe({
summary:"MyPackage doing amazing stuff with AWS."
});
Package.on_use(function(api){
// add your package file to the server app
api.add_files("my-package.js","server");
// what we export outside of the package
// (this is important : packages have their own scope !)
api.export("MyPackage","server");
});
var crypto=Npm.require("crypto");
MyPackage={
myFunction:function(arguments){
// here you can use crypto functions !
}
};
my-package.js应该如下所示:
Package.describe({
summary:"MyPackage doing amazing stuff with AWS."
});
Package.on_use(function(api){
// add your package file to the server app
api.add_files("my-package.js","server");
// what we export outside of the package
// (this is important : packages have their own scope !)
api.export("MyPackage","server");
});
var crypto=Npm.require("crypto");
MyPackage={
myFunction:function(arguments){
// here you can use crypto functions !
}
};
您可能需要的功能是。
下面是我如何在base64中编码JSON安全策略,然后在我自己的应用程序中使用它生成安全签名的示例代码:
encodePolicy:function(jsonPolicy){
// stringify the policy, store it in a NodeJS Buffer object
var buffer=new Buffer(JSON.stringify(jsonPolicy));
// convert it to base64
var policy=buffer.toString("base64");
// replace "/" and "+" so that it is URL-safe.
return policy.replace(/\//g,"_").replace(/\+/g,"-");
},
encodeSignature:function(policy){
var hmac=crypto.createHmac("sha256",APP_SECRET);
hmac.update(policy);
return hmac.digest("hex");
}
这将允许您在Meteor应用程序的服务器端调用MyPackage.myFunction。
最后但不是最后,不要忘记“流星添加我的包”以便使用它 你可以不用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'));
});
这是基于John Hurliman在注意中发现的base64.js,这将在服务器上像一个符咒一样工作,但要将其移植到客户端,您必须使用回调函数调用方法,该函数将结果存储为会话变量。您可以使用包
这是一个赢家!工作正常,但如果您的唯一任务是base64编码/解码,则会被真正压倒