add listing and caching of S3 objects

This commit is contained in:
Kentai Radiquum 2025-01-29 23:25:02 +05:00
parent 3f9927532e
commit 168baad986
Signed by: Radiquum
GPG key ID: 858E8EE696525EED
3 changed files with 69 additions and 5 deletions

View file

@ -10,6 +10,62 @@ if (environment == "dev") {
if (!fs.existsSync("out")) fs.mkdirSync("out"); if (!fs.existsSync("out")) fs.mkdirSync("out");
// List Objects from S3 bucket
import { S3Client, ListObjectsV2Command } from "@aws-sdk/client-s3";
const S3 = new S3Client({
region: "auto",
endpoint: `${process.env.ENDPOINT}`,
credentials: {
accessKeyId: `${process.env.ACCESS_KEY_ID}`,
secretAccessKey: `${process.env.SECRET_ACCESS_KEY}`,
},
});
let items: string[] = [];
async function listAllObjects(bucketName: string, prefix: string) {
let isTruncated: boolean = true;
let continuationToken: string = "";
while (isTruncated) {
const params = {
Bucket: bucketName,
Prefix: prefix,
ContinuationToken: continuationToken,
};
const response = await S3.send(new ListObjectsV2Command(params));
if (response && response.Contents) {
response.Contents.forEach((item) => {
if (item.Key != undefined) {
const ext = item.Key.split(".")[item.Key.split(".").length - 1];
if (["png", "jpg", "mp4", "jpeg"].includes(ext.toLowerCase())) {
items.push(item.Key);
}
}
});
isTruncated = response.IsTruncated as boolean;
continuationToken = response.NextContinuationToken as string;
}
}
}
if (!fs.existsSync(".cache")) fs.mkdirSync(".cache");
if (!fs.existsSync(".cache/objects.json")) {
log.info("Listing all objects in S3 bucket . . .");
await listAllObjects(process.env.BUCKET as string, "red_panda");
fs.writeFileSync(".cache/objects.json", JSON.stringify({ items }));
log.info(`Total: ${items.length}`);
} else {
log.info("Using S3 Bucket cache . . .");
items = JSON.parse(
fs.readFileSync(".cache/objects.json", { encoding: "utf-8" })
)["items"];
log.info(`Total: ${items.length}`);
}
import { renderToString } from "react-dom/server"; import { renderToString } from "react-dom/server";
import fs from "fs"; import fs from "fs";
function App(props: {}) { function App(props: {}) {
@ -17,7 +73,11 @@ function App(props: {}) {
<html> <html>
<head> <head>
<link href="./static/tailwind.css" rel="stylesheet" /> <link href="./static/tailwind.css" rel="stylesheet" />
{environment == "dev" ? <script src="./static/dev/hotreload.js"></script> : ""} {environment == "dev" ? (
<script src="./static/dev/hotreload.js"></script>
) : (
""
)}
<title>Bun Render Test</title> <title>Bun Render Test</title>
</head> </head>
<body> <body>

View file

@ -568,6 +568,10 @@
font-size: var(--text-6xl); font-size: var(--text-6xl);
line-height: var(--tw-leading, var(--text-6xl--line-height)); line-height: var(--tw-leading, var(--text-6xl--line-height));
} }
.text-8xl {
font-size: var(--text-8xl);
line-height: var(--tw-leading, var(--text-8xl--line-height));
}
.text-green-400 { .text-green-400 {
color: var(--color-green-400); color: var(--color-green-400);
} }

View file

@ -33,7 +33,7 @@ export class Log {
return `${time}:${level} - ${message}`; return `${time}:${level} - ${message}`;
} }
debug(message: string, isConnected = false) { debug(message: string | any, isConnected = false) {
if (this._level == 0) { if (this._level == 0) {
if (isConnected) { if (isConnected) {
message = `${message}`; message = `${message}`;
@ -43,7 +43,7 @@ export class Log {
} }
} }
info(message: string, isConnected = false) { info(message: string | any, isConnected = false) {
if (this._level <= 1) { if (this._level <= 1) {
if (isConnected) { if (isConnected) {
message = `${message}`; message = `${message}`;
@ -53,7 +53,7 @@ export class Log {
} }
} }
warn(message: string, isConnected = false) { warn(message: string | any, isConnected = false) {
if (this._level <= 2) { if (this._level <= 2) {
if (isConnected) { if (isConnected) {
message = `${message}`; message = `${message}`;
@ -63,7 +63,7 @@ export class Log {
} }
} }
error(message: string, isConnected = false) { error(message: string | any, isConnected = false) {
if (this._level <= 3) { if (this._level <= 3) {
if (isConnected) { if (isConnected) {
message = `${message}`; message = `${message}`;