|
| 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