Skip to content

Commit

Permalink
feat: support create multi oss client (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 authored Aug 2, 2023
1 parent 6c6b352 commit 157ecc5
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
20 changes: 15 additions & 5 deletions src/client/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/client/Oss.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

0 comments on commit 157ecc5

Please sign in to comment.