-
Notifications
You must be signed in to change notification settings - Fork 7
/
s3.js
71 lines (62 loc) · 1.86 KB
/
s3.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
// AWS wrapper.
// Currently requires environment variables to be set
var mode = require('./mode');
var aws = require('aws-sdk');
var s3Client = null;
var DEFAULT_ACL = 'bucket-owner-full-control';
exports.ACL_PUBLIC_READ = 'public-read';
exports.BUCKET_ID = process.env.S3_BUCKET_ID;
exports.S3_SUBSCRIBERS_BUCKET_KEY = process.env.S3_SUBSCRIBERS_BUCKET_KEY + '/subscribers.json';
exports.QUESTION_EXPORT_FILE_PREFIX = "backups/questions/crisis-";//Plus the ID of the Crisis.
aws.config.update({
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_KEY,
region: 'eu-west-1',
sslEnabled: true
});
// S3 client singleton.
exports.client = function() {
if (!s3Client) {
// Create S3 client.
s3Client = new aws.S3({
apiVersion: '2006-03-01'
});
}
return s3Client;
};
// Higher-level API for uploading using AWS SDK putObject method.
exports.put = function(key, body, acl, callback) {
if (!acl) {
acl = DEFAULT_ACL;
}
var params = {
Bucket: exports.BUCKET_ID,
Key: key,
ACL: acl,
Body: body
};
exports.client().putObject(params, function(err, data) {
callback(err, data);
});
};
// Higher-level API for downloading using AWS SDK getObject method.
exports.get = function(key, callback) {
var params = {
Bucket: exports.BUCKET_ID,
Key: key,
};
exports.client().getObject(params, function(err, data) {
callback(err, data);
});
};
// Higher-level API for downloading list of objects using AWS SDK listObjects method.
exports.list = function(prefix, callback) {
//Bucket ID available on environment variables
var params = {
Bucket: exports.BUCKET_ID,
Prefix: prefix
};
exports.client().listObjects(params, function(err, data) {
callback(err, data);
});
};