0.1.1 • Published 7 years ago

storage-s3 v0.1.1

Weekly downloads
2
License
Apache-2.0
Repository
github
Last release
7 years ago

storage-s3

A small module for backing up values in AWS S3. Useful in the event of a server crash or just as a way to maintain state over time.

This module maintains an object called 'storage' locally and backs up each user-specified property of that object to S3. Each value of storageobject_name in memory will have an associated object_name.json file in S3.

setup establishes S3 configuration and can initialize local values from S3.

getData returns value from memory.

setData updates a value in S3 and local memory.

refresh can update local memory from S3.

Usage

setup

Initializes S3 bucket name and AWS region. Can get initial values from S3 for local memory.

Parameters:

  • bucket_name (string) - Required - Name of S3 bucket to use
  • object_names_to_get_on_setup (array of strings)
  • region (string) - AWS region, defaults to process.env.REGION, then us-west-2
const storage = require('storage-s3');
storage.setup('some-bucket-name', ['missions', 'logs']);

Returns a Promise upon successful setup.

In mocha testing setups, something like this might be common:

const storage = require('storage-s3');

describe('Main set of tests', function () {
  before(done => {
    /* Set up local test server */
    
    // Set up storage
    storage.setup('some-bucket-name', ['missions', 'logs'])
        .then(() => done());
  });
  
  /* Some tests */
});

getData

A synchronous function that returns a clone of storageobject_name from local memory.

Parameters:

  • object_name (string) - Required
/* After already calling storage.setup() at some point */

const missions = storage.getData('missions');

setData

An asynchronous function that updates object_name.json in S3, then updates storageobject_name locally. Returns a promise.

Parameters:

  • updated_object (any type serializable to JSON) - Required - The value to save
  • object_name (string) - Required
/* After already calling storage.setup() at some point */

const missions = storage.getData('missions'); //Some array for example purposes
const some_new_value = {};
missions.push(some_new_value);

storage.setData(missions, 'missions')
    .then(() => console.log('Yay!'));

refresh

An asynchronous function that pulls from S3 and updates local memory. For each value in object_names, refresh will attempt to get object_name.json from the S3 bucket specified upon setup and then to update storageobject_name. Returns a Promise.

Parameters:

  • object_names (array of strings) - Required
/* After already calling storage.setup() at some point */

storage.refresh(['missions', 'logs']);