grunt + s3

Накатал небольшой плагинчик под grunt который подписывает настройки для amazon


"use strict";

var crypto = require("crypto");

function base64_encode(string){
    return new Buffer(string).toString("base64");
}

module.exports = function(grunt) {
    grunt.registerMultiTask("gen_signed_s3_policy", "Grunt plugin for getting signed s3 policy", function() {

        var date = new Date(0);
        date.setYear(new Date().getFullYear() + 10);
        var policy = {
            expiration: date.toISOString(),
            conditions: [
                {bucket: this.data.options.S3_BUCKET },
                ["starts-with", "$key", ""],
                ["starts-with", "$Content-Type", ""]
            ]
        };

        var policy_b64 = base64_encode(JSON.stringify(policy));
        var encrypred = crypto.createHmac("sha1", this.data.options.AWS_SECRET_KEY).update(policy_b64).digest("base64");
        this.data.callback(JSON.stringify({
            bucket: this.data.options.S3_BUCKET,
            AWSAccessKeyId: this.data.options.AWS_ACCESS_KEY,
            policy: policy_b64,
            signature: encrypred
        }));
    });
};

module.exports = function(grunt) {
	var options = {
		S3_BUCKET: "",
		AWS_ACCESS_KEY: "",
		AWS_SECRET_KEY: ""
	};

	grunt.initConfig({
		gen_signed_s3_policy: {
			default: {
				options: options,
				callback: function(data){
					console.log("gen_signed_s3_policy", data);
				}
			}
		}
	});

	grunt.loadTasks("./path/to/plugin");
};