Skip to content

Commit

Permalink
initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
joao-viegas committed Aug 18, 2019
1 parent ef6f38c commit 0e04362
Show file tree
Hide file tree
Showing 9 changed files with 2,424 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,5 @@ typings/

# next.js build output
.next
.idea
aws.sh
19 changes: 19 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
language: node_js

services:
- docker

before_install:
- docker pull localstack/localstack

node_js:
- stable

install:
- npm install

script:
- npm test

# Send coverage data to Coveralls
after_script: "cat coverage/lcov.info | node_modules/coveralls/bin/coveralls.js"
35 changes: 33 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,33 @@
# bucket-wrapper
...it's exactly that, a wrapper for a bucket-like store
[![Build Status](https://travis-ci.org/jtviegas/bucket-wrapper.svg?branch=master)](https://travis-ci.org/jtviegas/bucket-wrapper)
[![Coverage Status](https://coveralls.io/repos/github/jtviegas/bucket-wrapper/badge.svg?branch=master)](https://coveralls.io/github/jtviegas/bucket-wrapper?branch=master)

BUCKET WRAPPER
=========

...it's exactly that, a wrapper library for a bucket-like store

## Installation

`npm install @jtviegas/bucket-wrapper`

## Usage

```javascript
var bw = require('@jtviegas/bucket-wrapper');
bw.listObjects = ("bucket", "bucketPrefix", (e,r) => {
if(e){
//...do your error handling
}
else {
// ... do whatever you want
}
});
```

## Tests

`npm test`

## Contributing

In lieu of a formal style guide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code.
137 changes: 137 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
'use strict';

const winston = require('winston');
const aws = require('aws-sdk');

const bucketWrapper = (config) => {

if (!config)
throw new Error('!!! must provide config to initialize module !!!');

const logger = winston.createLogger(config['WINSTON_CONFIG']);

let s3;
if( config.test && config.test.aws_s3_endpoint ) {
logger.info("[bucketWrapper] using specific url: %s", config.test.aws_s3_endpoint);
s3 = new aws.S3({apiVersion: config.AWS_API_VERSION, endpoint: config.test.aws_s3_endpoint, region: config.AWS_REGION, s3ForcePathStyle: true});
}
else
s3 = new aws.S3({apiVersion: config.AWS_API_VERSION});

const listObjects = (bucketName, bucketNamePrefix, callback) => {
logger.debug("[listObjects|in] (%s, %s)", bucketName, bucketNamePrefix);
try{
s3.listObjectsV2({ Bucket: bucketName, Prefix: bucketNamePrefix + '/' }, function(e, d) {
logger.debug("[s3.listObjectsV2|in] (%o,%o)", e, d);
if (e){
logger.error(e);
callback(e);
}
else
callback(null, d.Contents);
});
}
catch(e){
logger.error(e);
callback(e);
}

logger.debug("[listObjects|out]");
};

const createObject = (bucket, key, binaryString, callback) => {
logger.debug("[createObject|in] (%s, %s, <binaryString>)", bucket, key);
try{
let params = {
Body: binaryString,
Bucket: bucket,
Key: key
};
s3.putObject(params, function(e, r) {
logger.debug("[s3.putObject|in] (%o,%o)", e, r);
if (e){
logger.error(e);
callback(e);
}
else
callback(null, r);
});
}
catch(e){
logger.error(e);
callback(e);
}

logger.debug("[createObject|out]");
};

const deleteObjects = (bucket, keys, callback) => {
logger.debug("[deleteObjects|in] (%s, %o)", bucket, keys);

let _keys = [];
if( Array.isArray(keys) ){
for( let i=0; i < keys.length; i++ ){
let _k = keys[i];
_keys.push({Key: _k});
}
}
else {
_keys.push({Key: keys});
}

try{
let params = {
Bucket: bucket,
Delete: {
Objects: _keys
}
};
s3.deleteObjects(params, function(e, r) {
logger.debug("[s3.deleteObjects|in] (%o,%o)", e, r);
if (e){
logger.error(e);
callback(e);
}
else
callback(null, r);
});
}
catch(e){
logger.error(e);
callback(e);
}
logger.debug("[deleteObjects|out]");
};

const getObject = (bucket, key, callback) => {
logger.debug("[getObject|in] (%s, %s)", bucket, key);
try{
let params = {
Bucket: bucket,
Key: key
};
s3.getObject(params, function(e, r) {
logger.debug("[s3.getObject|in] (%o,%o)", e, r);
if (e){
logger.error(e);
callback(e);
}
else
callback(null, r);
});
}
catch(e){
logger.error(e);
callback(e);
}

logger.debug("[getObject|out]");
};

return {listObjects: listObjects
, createObject: createObject
, deleteObjects: deleteObjects
, getObject: getObject};
};

module.exports = bucketWrapper;
Loading

0 comments on commit 0e04362

Please sign in to comment.