diff --git a/components/log-viewer-webui/server/src/S3Manager.js b/components/log-viewer-webui/server/src/S3Manager.js index ee6077c3f..348db6a73 100644 --- a/components/log-viewer-webui/server/src/S3Manager.js +++ b/components/log-viewer-webui/server/src/S3Manager.js @@ -1,3 +1,5 @@ +import fastifyPlugin from "fastify-plugin"; + import { GetObjectCommand, S3Client, @@ -14,17 +16,22 @@ const PRE_SIGNED_URL_EXPIRY_TIME_SECONDS = 3600; * Class to manage Simple Storage Service (S3) objects. */ class S3Manager { - #s3Client; + #s3Client = null; /** - * @param {string} region + * @param {object} props + * @param {string | null} props.region */ - constructor ( - region, - ) { - this.#s3Client = new S3Client({ - region: region, - }); + constructor ({region}) { + if (null !== region) { + this.#s3Client = new S3Client({ + region: region, + }); + } + } + + isEnabled () { + return null !== this.#s3Client; } /** @@ -35,6 +42,9 @@ class S3Manager { * @throws {Error} If a pre-signed URL couldn't be generated. */ async getPreSignedUrl (s3UriString) { + if (false === this.isEnabled()) { + throw new Error("pre-signed URL cannot be generated when S3Manager is not enabled"); + } const s3Uri = new URL(s3UriString); const command = new GetObjectCommand({ Bucket: s3Uri.hostname, @@ -55,4 +65,6 @@ class S3Manager { } } -export default S3Manager; +export default fastifyPlugin(async (app, options) => { + await app.decorate("s3Manager", new S3Manager(options)); +}); diff --git a/components/log-viewer-webui/server/src/app.js b/components/log-viewer-webui/server/src/app.js index 5d351fd39..c6b37ebbf 100644 --- a/components/log-viewer-webui/server/src/app.js +++ b/components/log-viewer-webui/server/src/app.js @@ -6,6 +6,7 @@ import DbManager from "./DbManager.js"; import exampleRoutes from "./routes/example.js"; import queryRoutes from "./routes/query.js"; import staticRoutes from "./routes/static.js"; +import S3Manager from "./S3Manager.js"; /** @@ -41,6 +42,7 @@ const app = async ({ port: settings.MongoDbPort, }, }); + await server.register(S3Manager, {region: settings.StreamFilesS3Region}); } await server.register(staticRoutes); diff --git a/components/log-viewer-webui/server/src/routes/query.js b/components/log-viewer-webui/server/src/routes/query.js index c3b558928..37a457fbb 100644 --- a/components/log-viewer-webui/server/src/routes/query.js +++ b/components/log-viewer-webui/server/src/routes/query.js @@ -2,22 +2,15 @@ import {StatusCodes} from "http-status-codes"; import settings from "../../settings.json" with {type: "json"}; import {EXTRACT_JOB_TYPES} from "../DbManager.js"; -import S3Manager from "../S3Manager.js"; - - -const S3_MANAGER = ( - null === settings.StreamFilesS3PathPrefix || - null === settings.StreamFilesS3Region -) ? - null : - new S3Manager(settings.StreamFilesS3Region); /** * Submits a stream extraction job and returns the metadata of the extracted stream. * * @param {object} props - * @param {import("fastify").FastifyInstance | {dbManager: DbManager}} props.fastify + * @param {import("fastify").FastifyInstance | + * {dbManager: DbManager} | + * {s3Manager: S3Manager}} props.fastify * @param {EXTRACT_JOB_TYPES} props.jobType * @param {number} props.logEventIdx * @param {string} props.streamId @@ -63,7 +56,9 @@ const extractStreamAndGetMetadata = async ({ /** * Creates query routes. * - * @param {import("fastify").FastifyInstance | {dbManager: DbManager}} fastify + * @param {import("fastify").FastifyInstance | + * {dbManager: DbManager} | + * {s3Manager: S3Manager}} fastify * @param {import("fastify").FastifyPluginOptions} options * @return {Promise} */ @@ -96,12 +91,12 @@ const routes = async (fastify, options) => { }); } - if (null === S3_MANAGER) { - streamMetadata.path = `/streams/${streamMetadata.path}`; - } else { - streamMetadata.path = await S3_MANAGER.getPreSignedUrl( + if (fastify.s3Manager.isEnabled()) { + streamMetadata.path = await fastify.s3Manager.getPreSignedUrl( `s3://${settings.StreamFilesS3PathPrefix}${streamMetadata.path}` ); + } else { + streamMetadata.path = `/streams/${streamMetadata.path}`; } return streamMetadata;