No s3 support by default

This commit is contained in:
Rory&
2025-12-25 03:57:01 +01:00
parent 0cf765fa9f
commit b0105d7d80
4 changed files with 24 additions and 9 deletions

BIN
package-lock.json generated

Binary file not shown.

View File

@@ -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",

View File

@@ -16,7 +16,6 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import { S3 } from "@aws-sdk/client-s3";
import { Readable } from "stream";
import { Storage } from "./Storage";
@@ -29,11 +28,15 @@ const readableToBuffer = (readable: Readable): Promise<Buffer> =>
});
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<void> {
// 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<void> {
// 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<Buffer | null> {
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<void> {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
await this.client.deleteObject({
Bucket: this.bucket,
Key: `${this.bucketBasePath}${path}`,

View File

@@ -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 };