Skip to content

Commit e0d93ed

Browse files
committed
Added AWS sdk functionality and instance parameters
1 parent f9e06a4 commit e0d93ed

7 files changed

Lines changed: 205 additions & 3 deletions

File tree

index.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
const GCLOUD = require('./lib/GoogleCloudStorageSystem');
22
const NFS = require('./lib/NFSStorageSystem');
3+
const AWS3 = require('./lib/S3StorageSystem');
34

45
class StorageFactory {
5-
static getInstance(storageMode) {
6+
static getInstance(storageMode, Region) {
67
if (!storageMode) {
78
throw new Error('Please add a storage Mode');
89
}
910
if (storageMode === 'GCLOUD') {
1011
return new GCLOUD();
1112
}
13+
if (storageMode === 'AWS') {
14+
return new AWS3(Region);
15+
}
1216
if (storageMode === 'NFS') {
1317
return new NFS();
1418
}

lib/GoogleCloudStorageSystem.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,22 @@ class GoogleCloudStorageSystem extends StorageSystem {
8888
throw new Error(err)
8989
}
9090
}
91+
92+
async listBuckets(){
93+
try {
94+
return await storage.getBuckets();
95+
}catch (err) {
96+
throw new Error(err)
97+
}
98+
}
99+
100+
async listFiles(bucketName){
101+
try {
102+
return await storage.bucket(bucketName).getFiles();
103+
}catch (err) {
104+
throw new Error(err)
105+
}
106+
}
91107
}
92108

93109
module.exports = GoogleCloudStorageSystem;

lib/S3StorageSystem.js

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
const StorageSystem = require('./storage-system');
2+
const AWS = require('aws-sdk');
3+
const fs = require('fs');
4+
5+
class S3StorageSystem extends StorageSystem {
6+
constructor(Region) {
7+
super();
8+
this.Region = Region;
9+
AWS.config.update({region: this.Region});
10+
this.s3 = new AWS.S3({apiVersion: '2006-03-01'});
11+
}
12+
13+
async deleteBucket(bucketName) {
14+
this.s3.deleteBucket({Bucket: bucketName}, function (err, data) {
15+
if (err) {
16+
return err
17+
} else {
18+
return data
19+
}
20+
});
21+
}
22+
23+
async listBuckets() {
24+
// Call S3 to list the buckets
25+
this.s3.listBuckets(function (err, data) {
26+
if (err) {
27+
return err
28+
} else {
29+
return data.Buckets;
30+
}
31+
});
32+
}
33+
34+
async listFiles(bucketName) {
35+
// Call S3 to obtain a list of the objects in the bucket
36+
this.s3.listObjects({Bucket: bucketName}, function (err, data) {
37+
if (err) {
38+
return err
39+
} else {
40+
return data
41+
}
42+
});
43+
}
44+
45+
async createBucket(bucketName, ACL) {
46+
// call S3 to create the bucket
47+
this.s3.createBucket({Bucket: bucketName, ACL: ACL}, function (err, data) {
48+
if (err) {
49+
return (err);
50+
} else {
51+
return (data.Location);
52+
}
53+
});
54+
}
55+
56+
async upload(bucketName, filename, key) {
57+
fs.readFile(filename, (err, data) => {
58+
if (err) throw err;
59+
this.s3.upload({Bucket: bucketName, Key: key, Body: JSON.stringify(data, null, 2)}, function (s3Err, data) {
60+
if (s3Err) throw s3Err;
61+
return (`File uploaded successfully at ${data.Location}`)
62+
});
63+
});
64+
}
65+
66+
67+
/**
68+
* @memberof GoogleCloudStorageSystem
69+
* @name Download
70+
* @params filename, destination
71+
* @description Serves as General Download SDK for GCLOUD Storage
72+
*/
73+
async download(bucketName, filename, destination) {
74+
this.s3.getObject({Bucket: bucketName, Key: filename}, (err, data) => {
75+
if (err) console.error(err);
76+
fs.writeFileSync(destination, data.Body.toString());
77+
return (`${filename} has been Downloaded!`);
78+
});
79+
}
80+
81+
async deleteFile(buckName, filename) {
82+
this.s3.deleteObject({Bucket: buckName, Key: filename}, function (err, data) {
83+
if (err) return (err.stack); // an error occurred
84+
else return (data); // successful response
85+
});
86+
}
87+
}
88+
89+
module.exports = S3StorageSystem;

lib/storage-system.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ class StorageSystem {
1818
deleteBucket(){
1919
throw new Error('concrete implementation should be used');
2020
}
21+
listBuckets(){
22+
throw new Error('concrete implementation should be used');
23+
}
24+
listFiles(){
25+
throw new Error('concrete implementation should be used');
26+
}
2127
}
2228

2329
module.exports = StorageSystem;

package-lock.json

Lines changed: 87 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
"homepage": "https://github.com/9trocode/node_storage_manager#readme",
3030
"dependencies": {
3131
"@google-cloud/storage": "^4.3.1",
32+
"aws-sdk": "^2.626.0",
3233
"chai": "^4.2.0",
3334
"fs-extra": "^8.1.0",
3435
"path": "^0.12.7"

test/sdk.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ describe('Simple Storage Test', () => {
66

77
//First stage Unit Test
88
it('should run', () => {
9-
let StorageInstance = storageSystem.getInstance('GCLOUD');
9+
let StorageInstance = storageSystem.getInstance('AWS', '');
1010
StorageInstance.download('smc-v1-dev', 'dev_key.key', '/Users/nitrocode/tmp/');
1111
console.log(StorageInstance);
1212
});

0 commit comments

Comments
 (0)