-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.js
99 lines (90 loc) · 3.09 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
var AWS = require('aws-sdk');
/**
* S3Sizer does the job of calculating the size of S3 folders.
* @param {object} params Options to be used when creating the object.
* @config {string} [params.configFile] File path of aws configuration file.
* @config {string} [params.accessKeyId] AWS access key id.
* @config {string} [params.secretAccessKey] AWS secret access key. If
* accessKeyId is included then secretAccessKey must also be included.
* @config {string} [params.region] The AWS region to make calls against.
*/
var S3Sizer = function(params) {
if(params.hasOwnProperty('s3')) {
this.s3 = params.s3;
} else{
if(params.hasOwnProperty('configFile')) {
AWS.config.loadFromPath(params.configFile);
}
if(params.hasOwnProperty('accessKeyId') && params.hasOwnProperty('secretAccessKey')) {
AWS.config.update({accessKeyId : params.accessKeyId, secretAccessKey : params.secretAccessKey});
}
if(params.hasOwnProperty('region')) {
AWS.config.update({region : params.region});
}
this.s3 = new AWS.S3();
}
};
/**
* Gets the combined file size of all the objects in a S3 folder.
* @param {string} bucket The bucket that the folder exists in.
* @param {string} folder The folder to calculate the size of.
* @param {function} callback Callback function that will be called once size
* has been calculated.
*/
S3Sizer.prototype.getFolderSize = function(bucket, folder, callback) {
this._getFolderSize(bucket, folder, callback);
};
/**
* Internal recursive get folder size function.
* @param {string} bucket The bucket that the folder exists in.
* @param {string} folder The folder to calculate the size of.
* @param {string} [marker] The object key to start with on request.
* @param {function} callback Callback function that will be called when an
* error occurs or size has been calculated.
*/
S3Sizer.prototype._getFolderSize = function(bucket, folder, marker, callback) {
var self = this;
var params = {
Bucket : bucket,
Prefix : folder + '/'
};
if(typeof marker === 'function') {
callback = marker;
marker = null;
}
if(marker !== null) {
params.Marker = marker;
}
(this.s3.client || this.s3).listObjects(params, function(err, data) {
if(err) {
return callback(err, null);
}
var size = 0;
if(data.hasOwnProperty('Contents')) {
size = self._calculateObjectsSize(data.Contents);
}
if(!data.IsTruncated) {
return callback(null, size);
}
marker = data.Contents[data.Contents.length - 1].Key;
self._getFolderSize(bucket, folder, marker, function(err, nsize) {
if(err) {
return callback(err, null);
}
return callback(null, size + nsize);
});
});
};
/**
* Calculates the size of the objects in the array passed in.
* @param {array} objects The objects to add up.
* @return {number} The total size of the objects.
*/
S3Sizer.prototype._calculateObjectsSize = function(objects) {
var size = 0;
for (var i = 0; i < objects.length; i++) {
size += objects[i].Size;
}
return size;
};
module.exports = S3Sizer;