mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
feat: minio storage support framework
This commit is contained in:
1 parent
fbd7601419
commit
c30fbe1e6b
34 files changed
+161
-467
No files matched your search
@@ -1,34 +1,43 @@
|
||||
import mongodb, { Collection } from 'mongodb';
|
||||
import { Collection, Db, MongoClient } from 'mongodb';
|
||||
import * as bus from './bus';
|
||||
import options from '../options';
|
||||
import { Collections } from '../interface';
|
||||
|
||||
const opts = options();
|
||||
|
||||
let mongourl = 'mongodb://';
|
||||
if (opts.username) mongourl += `${opts.username}:${opts.password}@`;
|
||||
mongourl += `${opts.host}:${opts.port}/${opts.name}`;
|
||||
|
||||
// eslint-disable-next-line import/no-mutable-exports
|
||||
export let db: mongodb.Db = null;
|
||||
// eslint-disable-next-line import/no-mutable-exports
|
||||
export let db2: mongodb.Db = null;
|
||||
|
||||
mongodb.MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true })
|
||||
.then((Client) => {
|
||||
db = Client.db(opts.name);
|
||||
global.Hydro.service.db.db = db;
|
||||
mongodb.MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true })
|
||||
.then((Client1) => {
|
||||
db2 = Client1.db(opts.name);
|
||||
global.Hydro.service.db.db2 = db2;
|
||||
bus.parallel('database/connect', db);
|
||||
});
|
||||
});
|
||||
|
||||
export function collection<K extends keyof Collections>(c: K): Collection<Collections[K]> {
|
||||
if (opts.prefix) return db.collection(`${opts.prefix}.${c}`);
|
||||
return db.collection(c);
|
||||
interface MongoConfig {
|
||||
username?: string,
|
||||
password?: string,
|
||||
host?: string,
|
||||
port?: string,
|
||||
name?: string,
|
||||
url?: string,
|
||||
prefix?: string,
|
||||
}
|
||||
|
||||
global.Hydro.service.db = { collection, db, db2 };
|
||||
class MongoService {
|
||||
public client: MongoClient;
|
||||
public client2: MongoClient;
|
||||
public db: Db;
|
||||
public db2: Db;
|
||||
private opts: MongoConfig;
|
||||
|
||||
async start(opts: MongoConfig) {
|
||||
this.opts = opts;
|
||||
let mongourl = 'mongodb://';
|
||||
if (opts.username) mongourl += `${opts.username}:${opts.password}@`;
|
||||
mongourl += `${opts.host}:${opts.port}/${opts.name}`;
|
||||
if (opts.url) mongourl = opts.url;
|
||||
this.client = await MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true });
|
||||
this.db = this.client.db(opts.name);
|
||||
this.client2 = await MongoClient.connect(mongourl, { useNewUrlParser: true, useUnifiedTopology: true });
|
||||
this.db2 = this.client2.db(opts.name);
|
||||
bus.parallel('database/connect', this.db);
|
||||
}
|
||||
|
||||
public collection<K extends keyof Collections>(c: K): Collection<Collections[K]> {
|
||||
if (this.opts.prefix) return this.db.collection(`${this.opts.prefix}.${c}`);
|
||||
return this.db.collection(c);
|
||||
}
|
||||
}
|
||||
|
||||
const service = new MongoService();
|
||||
global.Hydro.service.db = service;
|
||||
export = service;
|
||||
@@ -1,8 +1,8 @@
|
||||
import { GridFSBucket } from 'mongodb';
|
||||
import { db2 } from './db';
|
||||
import db from './db';
|
||||
import { HydroFileSystem } from '../interface';
|
||||
|
||||
const fs = new GridFSBucket(db2);
|
||||
const fs = new GridFSBucket(db.db2);
|
||||
|
||||
// @ts-ignore
|
||||
global.Hydro.service.fs = fs;
|
||||
|
||||
@@ -2,7 +2,7 @@ import cluster from 'cluster';
|
||||
import crypto from 'crypto';
|
||||
import axios from 'axios';
|
||||
import { safeDump } from 'js-yaml';
|
||||
import * as db from './db';
|
||||
import db from './db';
|
||||
import * as bus from './bus';
|
||||
import * as sysinfo from '../lib/sysinfo';
|
||||
|
||||
|
||||
@@ -0,0 +1,68 @@
|
||||
import { Readable } from 'stream';
|
||||
import Minio, { BucketItem, ItemBucketMetadata } from 'minio';
|
||||
|
||||
interface StorageOptions {
|
||||
host?: string,
|
||||
port?: number,
|
||||
useSSL?: boolean,
|
||||
accessKey?: string,
|
||||
secretKey?: string,
|
||||
bucket?: string,
|
||||
region?: string,
|
||||
}
|
||||
|
||||
const defaultConfig: StorageOptions = {
|
||||
host: 'play.min.io',
|
||||
port: 9000,
|
||||
useSSL: true,
|
||||
accessKey: 'Q3AM3UQ867SPQQA43P2F',
|
||||
secretKey: 'zuf+tfteSlswRu7BJ86wekitnifILbZam1KYY3TG',
|
||||
bucket: 'hydro',
|
||||
region: 'us-east-1',
|
||||
};
|
||||
|
||||
class StorageService {
|
||||
public client: Minio.Client;
|
||||
private opts: StorageOptions;
|
||||
|
||||
async start(opts: StorageOptions) {
|
||||
this.opts = { ...defaultConfig, ...opts };
|
||||
this.client = new Minio.Client({
|
||||
endPoint: this.opts.host,
|
||||
port: this.opts.port,
|
||||
useSSL: this.opts.useSSL,
|
||||
accessKey: this.opts.accessKey,
|
||||
secretKey: this.opts.secretKey,
|
||||
});
|
||||
const exists = await this.client.bucketExists(this.opts.bucket);
|
||||
if (!exists) await this.client.makeBucket(this.opts.bucket, this.opts.region);
|
||||
}
|
||||
|
||||
async put(target: string, file: string | Buffer | Readable, meta: ItemBucketMetadata = {}) {
|
||||
if (typeof file === 'string') return await this.client.fPutObject(this.opts.bucket, target, file, meta);
|
||||
return await this.client.putObject(this.opts.bucket, target, file, meta);
|
||||
}
|
||||
|
||||
async get(target: string) {
|
||||
return await this.client.getObject(this.opts.bucket, target);
|
||||
}
|
||||
|
||||
async del(target: string | string[]) {
|
||||
if (typeof target === 'string') return await this.client.removeObject(this.opts.bucket, target);
|
||||
return await this.client.removeObjects(this.opts.bucket, target);
|
||||
}
|
||||
|
||||
async list(target: string, recursive = false) {
|
||||
const stream = this.client.listObjects(this.opts.bucket, target, recursive);
|
||||
return await new Promise<BucketItem[]>((resolve, reject) => {
|
||||
const results: BucketItem[] = [];
|
||||
stream.on('data', (result) => results.push(result));
|
||||
stream.on('end', () => resolve(results));
|
||||
stream.on('error', reject);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
const service = new StorageService();
|
||||
global.Hydro.service.storage = service;
|
||||
export = service;
|
||||
Reference in new issue
Block a user