2017-07-03 19:34:58 +00:00
|
|
|
/*\
|
|
|
|
title: $:/plugins/tiddlywiki/aws/utils.js
|
|
|
|
type: application/javascript
|
|
|
|
module-type: library
|
|
|
|
|
|
|
|
AWS utility functions
|
|
|
|
|
|
|
|
\*/
|
|
|
|
/*jslint node: true, browser: true */
|
|
|
|
/*global $tw: false */
|
|
|
|
"use strict";
|
|
|
|
|
2017-08-15 14:48:12 +00:00
|
|
|
/*
|
|
|
|
Get a file from an S3 bucket
|
|
|
|
region:
|
|
|
|
bucketName:
|
|
|
|
title:
|
|
|
|
callback: invoked with (err,{body:,type:}
|
|
|
|
*/
|
2017-08-16 17:10:52 +00:00
|
|
|
function getFile(region,bucketName,title,callback) {
|
2018-02-19 09:11:48 +00:00
|
|
|
// console.log("Reading file from S3",bucketName,title);
|
2017-08-15 14:48:12 +00:00
|
|
|
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()
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-03 19:34:58 +00:00
|
|
|
/*
|
|
|
|
Put a file to an S3 bucket
|
|
|
|
*/
|
2017-08-16 17:10:52 +00:00
|
|
|
function putFile(region,bucketName,title,text,type,callback) {
|
2017-08-29 20:41:50 +00:00
|
|
|
// Log the write
|
|
|
|
if($tw["lambda-result"]) {
|
|
|
|
$tw["lambda-result"]["files-written"].push({bucket: bucketName,key: title});
|
|
|
|
}
|
2018-02-19 09:11:48 +00:00
|
|
|
// console.log("Writing file to S3",bucketName,title,type);
|
2017-07-03 19:34:58 +00:00
|
|
|
var AWS = require("aws-sdk"),
|
|
|
|
s3bucket = new AWS.S3({
|
2017-08-15 14:48:12 +00:00
|
|
|
region: region
|
|
|
|
}),
|
2017-07-03 19:34:58 +00:00
|
|
|
encoding = ($tw.config.contentTypeInfo[type] || {encoding: "utf8"}).encoding,
|
|
|
|
params = {
|
|
|
|
Bucket: bucketName,
|
|
|
|
Key: title,
|
|
|
|
Body: new Buffer(text,encoding),
|
|
|
|
ContentType: type || "text/plain"
|
|
|
|
};
|
2017-09-07 09:55:42 +00:00
|
|
|
s3bucket.upload(params,function(err,data) {
|
|
|
|
if(err) {
|
|
|
|
return callback(err + " (writing " + title + " to " + bucketName + ", type " + type + ")");
|
|
|
|
}
|
|
|
|
callback(null,data);
|
|
|
|
});
|
2017-07-03 19:34:58 +00:00
|
|
|
}
|
|
|
|
|
2017-08-16 17:10:52 +00:00
|
|
|
exports.putFile = putFile;
|
|
|
|
exports.getFile = getFile;
|