From 157ecc5f4dc23f53bc0e0e10ae480cd777091d48 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Wed, 2 Aug 2023 11:44:22 +0800 Subject: [PATCH] feat: support create multi oss client (#26) --- src/client/Client.ts | 20 +++++++++++++++----- test/unit/client/Oss.test.ts | 10 ++++++++++ 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/client/Client.ts b/src/client/Client.ts index 99c2ceb..d4050f7 100644 --- a/src/client/Client.ts +++ b/src/client/Client.ts @@ -40,6 +40,7 @@ export interface ClientOptions { export default class Client { readonly host: string; readonly port: string; + private readonly _address: string; private readonly _runtime: RuntimeClient; private readonly _ossClient: ObjectStorageServiceClient; private readonly _ossOptions: OssOptions; @@ -61,21 +62,22 @@ export default class Client { host: string = process.env.runtime_GRPC_HOST ?? '127.0.0.1', options?: ClientOptions) { this.host = host; this.port = port; - const clientCredentials = ChannelCredentials.createInsecure(); let address = `${this.host}:${this.port}`; // Support UDS if (this.host.startsWith('unix://')) { address = this.host; } - this._runtime = new RuntimeClient(address, clientCredentials); - debug('Start connection to %o', address); + this._address = address; + const clientCredentials = ChannelCredentials.createInsecure(); + this._runtime = new RuntimeClient(this._address, clientCredentials); + debug('Start connection to %o', this._address); if (options?.ossEnable || options?.oss) { this._ossOptions = options?.oss || {}; - this._ossClient = new ObjectStorageServiceClient(address, clientCredentials); + this._ossClient = new ObjectStorageServiceClient(this._address, clientCredentials); } if (options?.cryption?.componentName) { this._cryptionOptions = options.cryption; - this._cryptionClient = new CryptionServiceClient(address, clientCredentials); + this._cryptionClient = new CryptionServiceClient(this._address, clientCredentials); } } @@ -134,6 +136,14 @@ export default class Client { return this._oss; } + /** + * Create new oss client instance + */ + createOSSClient(options: OssOptions = {}) { + const ossClient = new ObjectStorageServiceClient(this._address, ChannelCredentials.createInsecure()); + return new Oss(ossClient, options); + } + get cryption() { if (!this._cryption) { if (!this._cryptionClient) { diff --git a/test/unit/client/Oss.test.ts b/test/unit/client/Oss.test.ts index 1461c07..5d2e192 100644 --- a/test/unit/client/Oss.test.ts +++ b/test/unit/client/Oss.test.ts @@ -122,4 +122,14 @@ describe.skip('client/Oss.test.ts', () => { }); assert(res); }); + + it('test new oss client', async () => { + const ossClient = client.createOSSClient(); + const res = await ossClient.list({ + storeName: 'oss_demo', + bucket: 'antsys-tnpmbuild', + prefix: 'test_', + }); + assert(res); + }); });