1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-08-17 08:50:50 +00:00
TiddlyWiki5/plugins/tiddlywiki/aws/modules/utils.js
Jermolene 509a474f4a AWS Plugin: Add s3-load and s3-rendertiddlers commands
With these commands we can do static rendering straight to an S3 bucket
2017-08-15 15:48:12 +01:00

68 lines
1.3 KiB
JavaScript

/*\
title: $:/plugins/tiddlywiki/aws/utils.js
type: application/javascript
module-type: library
AWS utility functions
\*/
(function(){
/*jslint node: true, browser: true */
/*global $tw: false */
"use strict";
/*
Get a file from an S3 bucket
region:
bucketName:
title:
callback: invoked with (err,{body:,type:}
*/
function getTextFile(region,bucketName,title,callback) {
console.log("Reading file from S3",bucketName,title)
var AWS = require("aws-sdk"),
s3bucket = new AWS.S3({
region: region
}),
params = {
Bucket: bucketName,
Key: title
};
s3bucket.getObject(params,function(err,data) {
if(err) {
return callback(err);
}
callback(null,{
etag: data.ETag,
version: data.VersionId,
type: data.ContentType,
body: data.Body.toString()
});
});
}
/*
Put a file to an S3 bucket
*/
function putTextFile(region,bucketName,title,text,type,callback) {
console.log("Writing file to S3",bucketName,title,type)
var AWS = require("aws-sdk"),
s3bucket = new AWS.S3({
region: region
}),
encoding = ($tw.config.contentTypeInfo[type] || {encoding: "utf8"}).encoding,
params = {
Bucket: bucketName,
Key: title,
Body: new Buffer(text,encoding),
ContentType: type || "text/plain"
};
s3bucket.upload(params,callback);
}
exports.putTextFile = putTextFile;
exports.getTextFile = getTextFile;
})();