forked from base-org/onchainsummer.xyz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cdnsync.js
61 lines (52 loc) · 1.84 KB
/
cdnsync.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
const AWS = require('aws-sdk');
const { google } = require('googleapis')
const s3bucket = process.env.S3_BUCKET;
const apiKey = process.env.GOOGLE_DRIVE_API_KEY;
const folderId = process.env.GOOGLE_DRIVE_FOLDER_ID;
const S3 = new AWS.S3();
const drive = google.drive({ version: "v3", auth: apiKey })
const main = async () => {
try {
var params = {
Bucket: s3bucket,
MaxKeys: 1000
};
const s3Response = await S3.listObjectsV2(params).promise();
const s3Contents = s3Response.Contents.map(file => file.Key);
const driveResponse = await drive.files.list({
maxResults: 1000,
q: `'${folderId}' in parents`
});
let driveFiles = driveResponse.data.files.map(file => ({ id: file.id, name: file.name }));
driveFiles = driveFiles.filter(file => {
const found = s3Contents.indexOf(file.name) >= 0
if (found) console.log("Ignoring", file.name);
return !found;
});
for (const file of driveFiles) {
console.log("Downloading", file.name);
const fileResponse = await drive.files.get({
fileId: file.id,
alt: "media"
});
const arrayBuffer = await fileResponse.data.arrayBuffer();
console.log("Uploading", file.name);
let params = {
Bucket: s3bucket,
Key: file.name,
Body: Buffer.from(arrayBuffer)
};
await S3.upload(params).promise();
console.log("public-read", file.name);
params = {
Bucket: s3bucket,
Key: file.name,
ACL: "public-read"
};
await S3.putObjectAcl(params).promise();
}
} catch (err) {
console.error(err);
}
};
main();