diff --git a/package-lock.json b/package-lock.json index 6c0a33cde..e8efb1e46 100644 Binary files a/package-lock.json and b/package-lock.json differ diff --git a/package.json b/package.json index 14b4c5fd4..98d4a54fc 100644 --- a/package.json +++ b/package.json @@ -73,7 +73,6 @@ "typescript-json-schema": "^0.65.1" }, "dependencies": { - "@aws-sdk/client-s3": "^3.953.0", "@toondepauw/node-zstd": "^1.2.0", "ajv": "^8.17.1", "ajv-formats": "^3.0.1", diff --git a/src/cdn/util/S3Storage.ts b/src/cdn/util/S3Storage.ts index f3ac0b445..f657fd299 100644 --- a/src/cdn/util/S3Storage.ts +++ b/src/cdn/util/S3Storage.ts @@ -16,7 +16,6 @@ along with this program. If not, see . */ -import { S3 } from "@aws-sdk/client-s3"; import { Readable } from "stream"; import { Storage } from "./Storage"; @@ -29,11 +28,15 @@ const readableToBuffer = (readable: Readable): Promise => }); export class S3Storage implements Storage { + private client: unknown; public constructor( - private client: S3, + private region: string, private bucket: string, private basePath?: string, - ) {} + ) { + const { S3 } = require("@aws-sdk/client-s3"); + this.client = new S3({ region }); + } /** * Always return a string, to ensure consistency. @@ -43,6 +46,8 @@ export class S3Storage implements Storage { } async set(path: string, data: Buffer): Promise { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error await this.client.putObject({ Bucket: this.bucket, Key: `${this.bucketBasePath}${path}`, @@ -52,6 +57,8 @@ export class S3Storage implements Storage { async clone(path: string, newPath: string): Promise { // TODO: does this even work? + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error await this.client.copyObject({ Bucket: this.bucket, CopySource: `/${this.bucket}/${this.bucketBasePath}${path}`, @@ -61,6 +68,8 @@ export class S3Storage implements Storage { async get(path: string): Promise { try { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error const s3Object = await this.client.getObject({ Bucket: this.bucket, Key: `${this.bucketBasePath ?? ""}${path}`, @@ -79,6 +88,8 @@ export class S3Storage implements Storage { } async delete(path: string): Promise { + // eslint-disable-next-line @typescript-eslint/ban-ts-comment + // @ts-expect-error await this.client.deleteObject({ Bucket: this.bucket, Key: `${this.bucketBasePath}${path}`, diff --git a/src/cdn/util/Storage.ts b/src/cdn/util/Storage.ts index 6ec1b796a..1184bde8d 100644 --- a/src/cdn/util/Storage.ts +++ b/src/cdn/util/Storage.ts @@ -19,8 +19,7 @@ import { FileStorage } from "./FileStorage"; import path from "path"; import fs from "fs"; -import { S3 } from "@aws-sdk/client-s3"; -import { S3Storage } from "./S3Storage"; +import { red } from "picocolors"; process.cwd(); export interface Storage { @@ -46,6 +45,13 @@ if (process.env.STORAGE_PROVIDER === "file" || !process.env.STORAGE_PROVIDER) { storage = new FileStorage(); } else if (process.env.STORAGE_PROVIDER === "s3") { + try { + require("@aws-sdk/client-s3"); + } catch (e) { + console.error(red(`[CDN] AWS S3 SDK not installed. Please run 'npm install --no-save @aws-sdk/client-s3' to use the S3 storage provider.`)); + process.exit(1); + } + const region = process.env.STORAGE_REGION, bucket = process.env.STORAGE_BUCKET; @@ -67,9 +73,8 @@ if (process.env.STORAGE_PROVIDER === "file" || !process.env.STORAGE_PROVIDER) { location = undefined; } - const client = new S3({ region }); - - storage = new S3Storage(client, bucket, location); + const { S3Storage } = require("S3Storage"); + storage = new S3Storage(region, bucket, location); } export { storage };