mirror of
https://github.com/cupcakearmy/morphus.git
synced 2025-09-06 08:10:39 +00:00
google cloud storage
This commit is contained in:
40
src/storage/gcs.ts
Normal file
40
src/storage/gcs.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { Bucket, Storage as GCStorage } from '@google-cloud/storage'
|
||||
|
||||
import { Storage } from '.'
|
||||
|
||||
export type GCSConfig = {
|
||||
bucket: string
|
||||
keyFilename: string
|
||||
}
|
||||
|
||||
export class GCS implements Storage {
|
||||
client: GCStorage
|
||||
bucket: Bucket
|
||||
|
||||
constructor(private options: GCSConfig) {
|
||||
this.client = new GCStorage(options)
|
||||
this.bucket = this.client.bucket(options.bucket)
|
||||
}
|
||||
|
||||
async init(): Promise<void> {
|
||||
await this.client.bucket(this.options.bucket).getFiles({ maxResults: 1 })
|
||||
}
|
||||
|
||||
async readStream(path: string): Promise<NodeJS.ReadableStream> {
|
||||
if (!(await this.exists(path))) throw new Error(`File ${path} does not exist`)
|
||||
return this.bucket.file(path).createReadStream()
|
||||
}
|
||||
|
||||
async writeStream(path: string): Promise<NodeJS.WritableStream> {
|
||||
return this.bucket.file(path).createWriteStream()
|
||||
}
|
||||
|
||||
async exists(path: string): Promise<boolean> {
|
||||
const [exists] = await this.bucket.file(path).exists()
|
||||
return exists
|
||||
}
|
||||
|
||||
async delete(path: string): Promise<void> {
|
||||
await this.bucket.file(path).delete()
|
||||
}
|
||||
}
|
@@ -1,14 +1,13 @@
|
||||
import { FastifyInstance } from 'fastify'
|
||||
|
||||
import { Config, StorageType } from '../config'
|
||||
import { GCS } from './gcs'
|
||||
import { Local } from './local'
|
||||
import { Minio } from './minio'
|
||||
|
||||
export abstract class Storage {
|
||||
abstract init(): Promise<void>
|
||||
|
||||
abstract read(path: string): Promise<Buffer>
|
||||
abstract write(path: string, data: Buffer): Promise<void>
|
||||
|
||||
abstract readStream(path: string): Promise<NodeJS.ReadableStream>
|
||||
abstract writeStream(path: string): Promise<NodeJS.WritableStream>
|
||||
|
||||
@@ -26,12 +25,6 @@ export async function init(App: FastifyInstance) {
|
||||
storage = new Local(Config.localAssets)
|
||||
break
|
||||
case StorageType.S3:
|
||||
// storage = new S3({
|
||||
// accessKeyId: Config.s3.accessKey,
|
||||
// secretAccessKey: Config.s3.secretKey,
|
||||
// bucket: Config.s3.bucket,
|
||||
// region: Config.s3.region,
|
||||
// })
|
||||
storage = new Minio({
|
||||
accessKey: Config.s3.accessKey,
|
||||
secretKey: Config.s3.secretKey,
|
||||
@@ -49,6 +42,12 @@ export async function init(App: FastifyInstance) {
|
||||
bucket: Config.minio.bucket,
|
||||
})
|
||||
break
|
||||
case StorageType.GCS:
|
||||
storage = new GCS({
|
||||
bucket: Config.gcs.bucket,
|
||||
keyFilename: Config.gcs.keyFilename,
|
||||
})
|
||||
break
|
||||
default:
|
||||
throw new Error(`Unknown storage type: ${Config.storage}`)
|
||||
}
|
||||
@@ -56,6 +55,7 @@ export async function init(App: FastifyInstance) {
|
||||
await storage.init()
|
||||
App.log.debug(`Storage initialized: ${Config.storage}`)
|
||||
} catch (e) {
|
||||
App.log.error((e as Error).message)
|
||||
App.log.error(`Storage initialization failed: ${Config.storage}`)
|
||||
process.exit(1)
|
||||
}
|
||||
|
@@ -1,5 +1,5 @@
|
||||
import { resolve, join } from 'path'
|
||||
import fs from 'fs'
|
||||
import { join, resolve } from 'path'
|
||||
import { promisify } from 'util'
|
||||
|
||||
import { Storage } from './'
|
||||
@@ -13,30 +13,6 @@ export class Local implements Storage {
|
||||
await promisify(fs.mkdir)(this.root, { recursive: true })
|
||||
}
|
||||
|
||||
read(path: string): Promise<Buffer> {
|
||||
const file = join(this.root, path)
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readFile(file, (err, data) => {
|
||||
if (err) {
|
||||
return reject(err)
|
||||
}
|
||||
resolve(data)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
write(path: string, data: Buffer): Promise<void> {
|
||||
const file = join(this.root, path)
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.writeFile(file, data, (err) => {
|
||||
if (err) {
|
||||
return reject(err)
|
||||
}
|
||||
resolve()
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
exists(path: string): Promise<boolean> {
|
||||
const file = join(this.root, path)
|
||||
return new Promise((resolve, reject) => {
|
||||
|
@@ -2,7 +2,6 @@ import { Client } from 'minio'
|
||||
import { PassThrough } from 'stream'
|
||||
|
||||
import { Storage } from '.'
|
||||
import { StreamUtils } from '../utils/utils'
|
||||
|
||||
export type MinioConfig = {
|
||||
accessKey: string
|
||||
@@ -30,15 +29,6 @@ export class Minio implements Storage {
|
||||
await this.client.bucketExists(this.options.bucket)
|
||||
}
|
||||
|
||||
async read(path: string): Promise<Buffer> {
|
||||
const stream = await this.client.getObject(this.options.bucket, path)
|
||||
return StreamUtils.toBuffer(stream)
|
||||
}
|
||||
async write(path: string, data: Buffer): Promise<void> {
|
||||
const stream = await StreamUtils.fromBuffer(data)
|
||||
await this.client.putObject(this.options.bucket, path, stream)
|
||||
}
|
||||
|
||||
async readStream(path: string): Promise<NodeJS.ReadableStream> {
|
||||
const stream = await this.client.getObject(this.options.bucket, path)
|
||||
return stream
|
||||
|
Reference in New Issue
Block a user