forked from labeebklatif/s3-up
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
161 lines (136 loc) · 4.39 KB
/
index.js
File metadata and controls
161 lines (136 loc) · 4.39 KB
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
"use strict";
const path = require("path");
const fs = require("fs");
const glob = require("glob");
const minimatch = require("minimatch");
const mime = require("mime-types");
const AWS = require("aws-sdk");
module.exports = class Uploader {
constructor({
bucket,
destination,
distribution,
clean = false,
fileProperties = {},
distributionPath = "/*",
}) {
this.s3 = new AWS.S3();
this.bucket = bucket;
this.bucketPath = destination;
this.files = [];
this.exclude = [];
this.clean = clean;
this.fileProperties = fileProperties;
if (distribution) {
this.invalidate = true;
this.cloudFront = new AWS.CloudFront();
this.distribution = distribution;
this.invalidationPath =
typeof distributionPath === "string"
? [distributionPath]
: distributionPath;
}
}
getFileProperties = (file) => {
// fileProperties are expected to be key-value pairs. key is expected to be a pattern that matches current file
// and value will be additional properties for the file matching the pattern.
const key = Object.keys(this.fileProperties).find((pattern) =>
minimatch(file.basePath, pattern)
);
return key ? this.fileProperties[key] : {};
};
validateFile = (file) => {
const notExist = !fs.existsSync(file.path);
if (notExist) {
console.warn(`${file.path} not found. Skipped`);
return false;
}
const notInclude = this.exclude.find((pattern) =>
minimatch(file.basePath, pattern)
);
if (notInclude) return false;
return true;
};
uploadFiles = async () => {
console.log("Starting upload");
for (let file of this.files) {
if (this.validateFile(file) === false) continue;
// additional file properties like headers.
const properties = this.getFileProperties(file);
const params = {
Bucket: this.bucket,
Key: this.bucketPath
? path.join(this.bucketPath, file.basePath)
: file.basePath,
Body: fs.readFileSync(file.path),
ContentType: mime.lookup(file.path),
...properties,
};
try {
await this.s3.putObject(params).promise();
console.log(
`Uploaded ${file.path} to bucket: ${params.Bucket}/${params.Key}`
);
} catch (error) {
console.error(
`Error while uploading ${file} to bucket: ${params.Bucket}/${params.Key}`,
error
);
}
}
console.log("Upload completed!");
};
cleanDestination = async () => {
if (this.clean === false) return;
console.log("Cleaning destination");
const listParams = { Bucket: this.bucket, Prefix: this.bucketPath };
const listedObjects = await this.s3.listObjectsV2(listParams).promise();
if (listedObjects.Contents.length === 0) return;
const deleteObjects = listedObjects.Contents.map(({ Key }) => ({ Key }));
const deleteParams = {
Bucket: this.bucket,
Delete: { Objects: deleteObjects },
};
await this.s3.deleteObjects(deleteParams).promise();
// s3 returns list as chunks which needs to be recursively fetched. isTruncated becomes false if all items are returned.
if (listedObjects.IsTruncated) await this.cleanDestination();
else console.log("Destination is now clean!");
};
invalidateDistribution = async () => {
if (this.invalidate !== true) return;
const params = {
DistributionId: this.distribution,
InvalidationBatch: {
CallerReference: `${+new Date()}`,
Paths: {
Items: this.invalidationPath,
Quantity: this.invalidationPath.length,
},
},
};
await this.cloudFront.createInvalidation(params).promise();
console.log("Distribution invalidation created!");
};
addFile = (file) => {
// add individual files by path;
const fileObj = {};
fileObj.path = file;
fileObj.basePath = path.basename(file);
this.files.push(fileObj);
};
addDir = (dir) => {
// add all files in the given directory path recursively
const files = glob.sync("*", { cwd: dir, nodir: true, matchBase: true });
files.forEach((file) => {
const fileObj = {};
fileObj.path = path.join(dir, file);
fileObj.basePath = file;
this.files.push(fileObj);
});
};
upload = async () => {
await this.cleanDestination();
await this.uploadFiles();
await this.invalidateDistribution();
};
};