-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
169 lines (143 loc) · 4.17 KB
/
index.js
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
var AWS = require('aws-sdk');
function S3Context(options) {
this.readOnly = options.isReadOnly;
this.keyPrefix = options.keyPrefix;
this.s3bucket = options.s3bucket;
}
function prefixKey(prefix, key) {
return prefix + "/" + key;
}
function _put(s3bucket, keyPrefix, key, value, length, ContentType, callback) {
if(this.readOnly) {
return callback(new Error("Write operation on readOnly context."));
}
var keyPath = prefixKey(keyPrefix, key);
var params = {
Key: keyPath,
Body: value,
ContentLength: length,
ContentType: ContentType
};
s3bucket.putObject(params, function(err) {
if(err) {
return callback(err);
}
callback();
});
}
S3Context.prototype.putObject = function(key, value, callback) {
var json = JSON.stringify(value);
_put(this.s3bucket, this.keyPrefix, key, json, Buffer.byteLength(json), "application/json", callback);
};
S3Context.prototype.putBuffer = function(key, value, callback) {
_put(this.s3bucket, this.keyPrefix, key, value, value.length, "application/octet-stream", callback);
};
S3Context.prototype.delete = function (key, callback) {
if(this.readOnly) {
return callback(new Error("Write operation on readOnly context."));
}
key = prefixKey(this.keyPrefix, key);
this.s3bucket.deleteObject({Key: key}, function(err) {
if(err) {
return callback(err);
}
callback();
});
};
S3Context.prototype.clear = function(callback) {
if(this.readOnly) {
return callback(new Error("Write operation on readOnly context."));
}
var options = {
Prefix: this.keyPrefix
};
getAllObjects(options, callback, []);
var s3bucket = this.s3bucket;
function getAllObjects(options, callback, aggregate) {
s3bucket.listObjects(options, function(err, data) {
if(err) {
return callback(err);
}
if(data.Contents.length === 0) {
return callback();
}
aggregate = aggregate.concat(data.Contents.map(function(content) {
return { Key: content.Key };
}));
// We have to check whether or not this object listing is complete.
if (data.IsTruncated) {
options.Marker = data.Contents[data.Contents.length - 1].Key;
getAllObjects(options, callback, aggregate);
}
var params = {
Delete: {
Objects: aggregate
}
};
s3bucket.deleteObjects(params, function(err) {
if(err) {
return callback(err);
}
callback();
});
});
}
};
function _get(s3bucket, keyPrefix, type, key, callback) {
var keyPath = prefixKey(keyPrefix, key);
var params = {
Key: keyPath,
ResponseContentType: type
};
s3bucket.getObject(params, function(err, data) {
if(err && err.code !== "NoSuchKey") {
return callback(err);
}
callback(null, data && data.Body || null);
});
}
S3Context.prototype.getObject = function(key, callback) {
_get(this.s3bucket, this.keyPrefix, "application/json", key, function(err, data) {
if(err) {
return callback(err);
}
if(data) {
try {
data = JSON.parse(data);
} catch(e) {
return callback(e);
}
}
callback(null, data);
});
};
S3Context.prototype.getBuffer = function(key, callback) {
_get(this.s3bucket, this.keyPrefix, "application/octet-stream", key, callback);
};
function S3Provider(options) {
this.keyPrefix = options.keyPrefix;
this.bucket = options.bucket;
this.key = options.key;
this.secret = options.secret;
}
S3Provider.isSupported = function() {
return (typeof module !== 'undefined' && module.exports);
};
S3Provider.prototype.open = function(callback) {
if(!this.keyPrefix) {
return callback(new Error("Missing keyPrefix"));
}
AWS.config.update({
accessKeyId: this.key,
secretAccessKey: this.secret
});
this.s3bucket = new AWS.S3({params: {Bucket: this.bucket}});
callback();
};
S3Provider.prototype.getReadOnlyContext = function() {
return new S3Context({isReadOnly: true, keyPrefix: this.keyPrefix, s3bucket: this.s3bucket});
};
S3Provider.prototype.getReadWriteContext = function() {
return new S3Context({isReadOnly: false, keyPrefix: this.keyPrefix, s3bucket: this.s3bucket});
};
module.exports = S3Provider;