@express-augment/storage-manager
v1.0.1
Published
A storage manager to help you manage storage (s3, fs, etc.)
Downloads
6
Maintainers
Readme
Augment Storage Manager
A storage manager for expressjs
The augment provides you with a facade to manage different storage options like AWS S3 or file system. ( currently only the s3 and file system storage options are added but more will be added later on. )
Concept
In different environment of your application, you may require different storage options to store your files. Eg. If your file is running on the cloud, you may need to store them on S3 buckets but doing the in a local setup may not be very efficient. It would be helpful to use the local file system instead of the buckets here to manage files during local development. The Augment storage manager lets you do just that. It provides you an easy option to switch between different storage providers or simple setup different providers for different environments. you can setup an environment variable for each environment with the name of the provider you want to use for that environment and then let storage manager take care of the rest.
Initialize an fs provider
const StorageManagerFactory = require('@express-augment/storage-manager');
const storageManager = new StorageManagerFactory('fs');
const fileStorageManager = storageManager.getManager();
Initialize an s3 (aws) provider
const StorageManagerFactory = require('@express-augment/storage-manager');
// if you have dotend setup, you can put these credentials there and storage manager will pick them up
// from process.env variable.
const storageManager = new StorageManagerFactory('aws_s3', {
AWS_S3_REGION: 'us-east-2',
AWS_ACCESS_KEY_ID: 'test',
AWS_SECRET_ACCESS_KEY: 'test',
AWS_S3_ENDPOINT: 'http://localhost:4566'
});
const s3StorageManager = storageManager.getManager();
list files/objects
// the target directory.
storageManager.setTarget('some-random-path');
// list files at path: some-random-path
storageManager.ls().then( data => console.log(data) )
read a file/object
// the target directory.
storageManager.setTarget('some-random-path');
// get file content at path: some-random-path/testfile.txt and convert it to string + log the output.
storageManager.get('testfile.txt').then( file => file.toString().then(console.log) )
copy from a path
// the target directory.
storageManager.setTarget('some-random-path');
// copy a file from one path to another. NOTE for s3, it will look for the from path in the same bucket.
// to copy between different buckets or upload from local filesystem to s3, use the put method.
storageManager.copy('from-path', 'to-path')
delete from a path
// the target directory.
storageManager.setTarget('some-random-path');
// delete file/object at path.
storageManager.rm('delete-path')
get size for a given path
// the target directory.
storageManager.setTarget('some-random-path');
// get the total size at a given path. returns promise with result in bytes.
storageManager.size('delete-path')
write to a file
// the target directory.
storageManager.setTarget('some-random-path');
// write to file.
// NOTE:- for s3, if the argument for content is passed as string, storage manager will look for the file locally and read
// from it and publish it's content to s3 path.
storageManager.put('path', 'some-example-content')