1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | 1x 1x 19x 19x 19x 19x 19x 19x 34x 34x 34x 34x 34x 34x 205x 34x 34x 34x 24x 24x 407x 407x 407x 205x 819x 819x 819x 1x 818x 818x 206x 206x 206x 206x 206x 206x 206x 206x 15x 15x 15x 15x 15x 2x 1x 1x 1x 1x 15x 204x 203x 203x 203x 203x 203x 15x 204x 1x 1x 1x 1x 1x 15x 1x | "use strict"; const AWS = require('aws-sdk'); const Storage = require('./Storage'); /** * Use a AWS S3 bucket as storage. * * Requires ```aws-sdk``` package. */ class S3Storage extends Storage { /** * @param {StorageOptions} options see fields */ constructor(options) { super(options) /** AWS region */ this.region = options.region; /** reference to driver object */ this.s3 = new AWS.S3({ apiVersion: '2006-03-01', endpoint: this.connectionString, region: this.region, s3ForcePathStyle: true }); } /** @override */ connect() { return new Promise((resolve, reject) => { this.listItems() .then(list => resolve(this.list = list), reject); }); } /** s3 list gives us last modified date; use that to efficiently check for updates */ listItems() { const that = this; return new Promise((resolve, reject) => { let output = {}; function iterate(ContinuationToken) { that.s3.listObjectsV2({ Bucket: that.collectionName, ContinuationToken }, (err, data) => { Iif (err) { return reject(err); } data.Contents.forEach(item => { output[item.Key] = item.LastModified; }); Eif (!data.IsTruncated) { resolve(output); } else { iterate(data.NextContinuationToken); } }); } iterate(); }); } /** @override */ readAllRecords() { return new Promise((resolve, reject) => { Promise.all(Object.keys(this.list) .map(item => { let query = {}; query[this.primaryKey] = item; return this.readRecord(query); })) .then(resolve, reject); }); } /** @override */ createRecord(record) { return this.updateRecord(record); } /** @override */ readRecord(record) { return new Promise((resolve, reject) => { this.s3.getObject({ Bucket: this.collectionName, Key: record[this.primaryKey] }, (err, data) => { if (err) { reject(err); } else { this.list[record[this.primaryKey]] = data.LastModified; resolve(JSON.parse(data.Body)); } }); }); } /** @override */ updateRecord(record) { return new Promise((resolve, reject) => { this.s3.upload({ Bucket: this.collectionName, Key: record[this.primaryKey], Body: JSON.stringify(record) }, (err, data) => { Iif (err) { reject(err); } else { resolve(this.readRecord(record)); } }); }); } /** @override */ deleteRecord(record) { return new Promise((resolve, reject) => { this.s3.deleteObject({ Bucket: this.collectionName, Key: record[this.primaryKey] }, (err, data) => { Iif (err) { reject(err); } else { resolve(record); } }); }); } /** @override */ async updateCheckImpl() { let updated = false; let newlist = await this.listItems(); let list = this.list, record = {}; // check for deleted item for (let item in list) { if (!newlist[item]) { record[this.primaryKey] = item; this.emit('delete', record); delete list[item]; updated = true; } } // check for new items for (let item in newlist) { if (!list[item]) { record[this.primaryKey] = item; this.readRecord(Object.assign({}, record)) .then(record => { this.emit('create', record); updated = true; }, x => x); list[item] = newlist[item]; } } // check for modified items for (let item in list) { if (list[item].getTime() !== newlist[item].getTime()) { record[this.primaryKey] = item; this.readRecord(Object.assign({}, record)) .then(record => { this.emit('update', record); updated = true; }, x => x); list[item] = newlist[item]; } } return updated; } } module.exports = S3Storage; |