-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresize.js
69 lines (60 loc) · 2.08 KB
/
resize.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
const AWS = require('aws-sdk');
const sharp = require('sharp');
const s3 = new AWS.S3();
const INPUT_BUCKET = "stanforddailyarchive";
const DEST_BUCKET = "stanforddailyarchive-resized"
function gmToBuffer (data) {
return new Promise((resolve, reject) => {
data.stream((err, stdout, stderr) => {
if (err) { return reject(err) }
const chunks = []
stdout.on('data', (chunk) => { chunks.push(chunk) })
// these are 'once' because they can and do fire multiple times for multiple errors,
// but this is a promise so you'll have to deal with them one at a time
stdout.once('end', () => { resolve(Buffer.concat(chunks)) })
stderr.once('data', (data) => { reject(String(data)) })
})
})
}
async function processFile(key) {
let params = {
Bucket: INPUT_BUCKET,
Key: key
}
let response = await s3.getObject(params).promise();
let keyMatch = key.match(/.*\/(.*)?$/);
let newKey = key;
if (keyMatch && keyMatch.length >= 2) {
newKey = keyMatch[1];
}
console.log(newKey);
if (key.endsWith(".jp2")) {
newKey = newKey.replace(".jp2", ".jpg");
let result = sharp(response.Body).jpeg({quality: 80}).toBuffer();
let buffer = await result;
let params = {Bucket: DEST_BUCKET, Key: newKey, Body: buffer, ContentType: "image/jpeg"};
await s3.putObject(params).promise();
}
else if (key.endsWith(".xml")) {
let params = {Bucket: DEST_BUCKET, Key: newKey, Body: response.Body, ContentType: "text/xml"};
await s3.putObject(params).promise();
}
else if (key.endsWith(".pdf")) {
}
return async () => await null;
}
async function listObjects(params) {
let data = await s3.listObjectsV2(params).promise();
let ContinuationToken = data["NextContinuationToken"];
for (i in data.Contents) {
await processFile(data.Contents[i].Key);
}
await listObjects({ContinuationToken, ...params});
}
async function main() {
var params = {
Bucket: INPUT_BUCKET
}
await listObjects(params);
}
main();