Skip to content

Commit 278a2cf

Browse files
committed
Added digital ocean spaces support
1 parent 090e218 commit 278a2cf

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ Cloudinary Declaration
4949
export CLOUDINARY_URL=cloudinary://4737858435783453:3827489jksdhfjasfhjBB@nitrocode
5050
```
5151

52+
Digital Ocean Spaces
53+
```bash
54+
export DG_ACCESS_KEY=284893748923yuwfhsdkfjshkfjh
55+
export DG_SECRET_KEY=982u289432u48jsdfkjsr3894
56+
```
57+
5258
Local NFS Declaration
5359
```bash
5460
export MOUNT_POINT=/Users/nitrocode/bucket/
@@ -154,6 +160,15 @@ node_storage_manager allows you to switch between clients easily without reconfi
154160
This contains a reference to the storage-pipe module. It is a valid use case to use
155161
both this module and all it's functions
156162

163+
`Note to specify region on S3 and DigitalOcean Spaces you need to pass it parameter on getInstance `
164+
```javascript
165+
// Imports the node_storage_manager library
166+
const Storage = require('node_storage_manager');
167+
let StorageInstance = Storage.getInstance('NFS', 'Asia');
168+
StorageInstance.upload()
169+
}
170+
```
171+
157172
### StorageInstance.download()
158173
Download file from S3, AWS & NFS using storage pipe
159174

lib/DigitalOceanStorageSystem.js

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

0 commit comments

Comments
 (0)