mirror of
https://github.com/spacebarchat/server.git
synced 2026-03-30 13:55:39 +00:00
No s3 support by default
This commit is contained in:
BIN
package-lock.json
generated
BIN
package-lock.json
generated
Binary file not shown.
@@ -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",
|
||||
|
||||
@@ -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}`,
|
||||
|
||||
@@ -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 };
|
||||
|
||||
Reference in New Issue
Block a user