Skip to content

Instantly share code, notes, and snippets.

@mwarner1
Last active July 27, 2018 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwarner1/8ddb7fcdb391149ad65afc4b15e2526a to your computer and use it in GitHub Desktop.
Save mwarner1/8ddb7fcdb391149ad65afc4b15e2526a to your computer and use it in GitHub Desktop.
Copying files to S3rver for serverless-offline testing using serverless-s3-local
'use strict';
// Matt Warner, July 2018
// Use at your own risk, no warranties, no guarantees, etc.
// Code to copy local files into the format used by serverless-s3-local (which uses S3rver)
const {promisify} = require('util');
const fs = require('fs');
const readFileAsync = promisify(fs.readFile);
const readDirAsync = promisify(fs.readdir);
const AWS = require('aws-sdk');
const s3 = new AWS.S3({
s3ForcePathStyle: true,
endpoint: new AWS.Endpoint('http://localhost:8000'),
});
/**
* Copy files recursively to the serverless-s3-local emulated bucket
* We use the S3 calls because the emulated bucket stores files as directories with three dot files
* to emulate S3 behavior.
* So a file named index.html will end up like this:
* index.html/
* With three dot files named .dummys3_content, .dummys3_metadata, .dummys3_content.md5
* If I'm copying a lot of files, doing this by hand is painful, so the code below does it for me.
* @param prefix
* @param dirName
* @returns {Promise<void>}
*/
const main = async (prefix, dirName) => {
if (!dirName) dirName = prefix;
let listing = await readDirAsync(dirName);
console.log(listing);
let fileList = listing.map(entry => !entry.match(/^\./) && fs.statSync(dirName + "/" + entry).isFile() ? dirName + "/" + entry : null); // get only filenames
fileList = fileList.filter(item => item); // remove nulls
let dirList = listing.map(entry => !entry.match(/^\./) && !fs.statSync(dirName + "/" + entry).isFile() ? dirName + "/" + entry : null); // get only directories
dirList = dirList.filter(item => item); // remove nulls
console.log("files", fileList);
console.log("Directories", dirList);
let filesPromise = Promise.all(fileList.map(async item => {
let content = await readFileAsync(item);
let extension = item.replace(/^.*\.(.*)/, '$1').toLowerCase();
let contentType;
switch (extension) {
case "htm":
case "html": {
contentType = "text/html";
break;
}
case "jpg":
case "jpeg":
case "png":
case "gif": {
contentType = "image/" + extension.replace(/jpg/, 'jpeg');
break;
}
case "pdf": {
contentType = "application/pdf";
break;
}
case "css": {
contentType = "text/css";
break;
}
case "js": {
contentType = "application/javascript";
break;
}
default : {
contentType = "application/octet-stream";
}
}
let params = {
Bucket: 'YOUR_LOCAL_BUCKET_NAME',
Key: item.replace(prefix + "/", ''),
Body: content,
ContentType: contentType + ";charset=utf-8"
};
// console.log("params", params);
await s3.putObject(params).promise();
}));
let dirsPromise = Promise.all(dirList.map(async item => await main(prefix, item)));
await Promise.all([filesPromise, dirsPromise]);
};
main("/path/to/directory/with/files/to/copy");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment