import axios from "axios"; import { S3, CloudFront } from "aws-sdk"; declare const Buffer: any; const s3 = new S3({ apiVersion: "2006-03-01" }); const cloudfront = new CloudFront({ apiVersion: "2020-05-31" }); const handler = async function () { // Gets repo structure from github const res = await axios.get( "https://api.github.com/repos/GovTechSG/dev-console-products-info/git/trees/main" ); // Extract only json files const filePaths = res.data.tree.filter((item: any) => item.path.includes(".json") ); let blobResList = await Promise.all( filePaths.map((file: any) => axios .get(file.url) .then((res) => ({ path: file.path, content: res.data.content })) ) ); async function emptyS3Directory(bucket: string, dir: string) { const listParams = { Bucket: bucket, Prefix: dir, }; const listedObjects = await s3.listObjectsV2(listParams).promise(); if (listedObjects?.Contents?.length === 0) return; const deleteParams: any = { Bucket: bucket, Delete: { Objects: [] }, }; listedObjects?.Contents?.forEach(({ Key }) => { deleteParams.Delete.Objects.push({ Key }); }); await s3.deleteObjects(deleteParams).promise(); if (listedObjects.IsTruncated) await emptyS3Directory(bucket, dir); } // Default deletion with recursion await emptyS3Directory("dev-console-widget-test", "products-details/"); // Upload new files to s3 await Promise.all( blobResList.map(async (file: any) => { let fileBuffer = Buffer.from(file.content, "base64"); let s3UploadParams = { Bucket: "dev-console-widget-test", Key: `products-details/${file.path}`, Body: fileBuffer, }; return new Promise((resolve, reject) => { s3.upload(s3UploadParams, (err: any, data: any) => { if (err) { return reject(err); } resolve(data); }); }); }) ); // Invalidate cloudfront cache const currentDate = new Date(); const timestamp = currentDate.getTime().toString(); const params = { DistributionId: "E2CY2A0UJXPD9W" /* required */, InvalidationBatch: { /* required */ CallerReference: timestamp /* required */, Paths: { /* required */ Quantity: 1 /* required */, Items: ["/*"], }, }, }; await cloudfront.createInvalidation(params).promise(); }; export { handler };