Giter Club home page Giter Club logo

nest-tencent-cloud-sdk's Introduction

@cardbrother/nestjs-tencent-cloud-sdk

CI Release & Publish package version NPM Downloads

GitHub commit activity (branch) GitHub commit activity (branch)

🚨tencentcloud-sdk-nodejs 🚨cos-nodejs-sdk-v5

GitHub Tag GitHub Tag

Language: δΈ­ζ–‡

tencentcloud-sdk-nodejs & cos-nodejs-sdk-v5 for NestJS developers

πŸ”¨ Installation

  • npm
npm install @cardbrother/nest-tencent-cloud-sdk
  • yarn
$ yarn add @cardbrother/nest-tencent-cloud-sdk
  • πŸš€ pnpm Recommended
$ pnpm add @cardbrother/nest-tencent-cloud-sdk

🍚 Usage

In your AppModuleor FeatureModule module, import the TencentCloudModule and call the forRoot method to configure the Tencent Cloud SDK with your secretId and secretKey:

Basic usage:

import { Module } from '@nestjs/common';
import { TencentCloudModule } from '@cardbrother/nest-tencent-cloud-sdk';

@Module({
  imports: [
    TencentCloudModule.forRoot({
      apiId: 'TENCENT_API_ID',
      apiSecret: 'TENCENT_API_SECRET',
      region: 'TENCENT_REGION',
      cos: {
        Bucket: 'COS_BUCKET',
        Region: 'COS_REGION',
        SecretId: 'COS_SECRET_ID',
        SecretKey: 'COS_SECRET_KEY',
      },
      global: true, // if true, the TencentCloudService will be a global service, default is false
    }),
  ],
})
export class AppModule {}

Dynamic useage

If you want to use a dynamic configuration, you can use the forRootAsync method to configure the Tencent Cloud SDK with your secretId and secretKey:

import { TencentCloudModule } from '@cardbrother/nestjs-tencent-cloud-sdk';
import { Module } from '@nestjs/common';
import { ConfigModule, ConfigService } from '@nestjs/config';

@Module({
  imports: [
    ConfigModule.forRoot({
      isGlobal: true,
    }),
    TencentCloudModule.forRootAsync({
      useFactory: async (configService: ConfigService) => ({
        apiId: configService.get<string>('TENCENT_API_ID'),
        apiSecret: configService.get<string>('TENCENT_API_SECRET'),
        region: configService.get<string>('TENCENT_REGION', 'ap-shanghai'),
        cos: {
          Bucket: configService.get<string>('COS_BUCKET'),
          Region: configService.get<string>('COS_REGION'),
          SecretId: configService.get<string>('COS_SECRET_ID'),
          SecretKey: configService.get<string>('COS_SECRET_KEY'),
        },
        global: true, // if true, the TencentCloudService will be a global service, default is false
      }),
      imports: [ConfigModule],
      inject: [ConfigService],
      global: true,
    }),
  ],
})
export class AppModule {}

Use TencentCloudService

import { Injectable } from '@nestjs/common';
import { TencentCloudService } from '@cardbrother/nest-tencent-cloud-sdk';

@Injectable()
export class AppService {
  constructor(
    private readonly tencentCloudService: TencentCloudService,
    @Inject(TENCENT_CLOUD_MODULE_OPTIONS_TOKEN)
    private readonly options: TencentCloudModuleOptions, // If u want to get the options u can use this
  ) {}

  // imgData is a base64 string
  async useOCR(imgData: string) {
    // Here the encapsulated object is dynamically instantiated based on the enumeration
    const ocrClient = await this.tencentCloudService.useClient('OCR'); // or SMS,COS,etc
    return ocrClient.fastOcr({
      ImageBase64: imgData,
    });
  }
}

Usage Tencent COS

πŸ’‘ Tips: If you pass Bucket, Region, or any parameter here it overrides the configuration at the time of imported module the reference, if you don't pass it then the import TencentCloudModule.forRoot configuration is used.

const cosClient = await this.tencentCloudService.useClient('COS');
cosClient.upload({
  Bucket: this.options.cos.Bucket, // It's not required. Default use the configuration at the Module Import time
  Region: this.options.cos.Region, // It's not required. Default use the configuration at the Module Import time
  Key: 'test.txt',
  Body: 'hello world',
  FilePath: 'test.txt',
});

Usage Tencent STS

const sts_client = await tencentCloudService.useClient(
  TencentCloudClientType.STS,
);
expect(sts_client).toBeDefined();
expect(sts_client).toBeInstanceOf(StsProvider);
const tempSignature = await sts_client.createTemporary({
  Name: 'Test',
  Policy: encodeURI(
    JSON.stringify({
      version: '2.0',
      statement: [
        {
          effect: 'allow',
          action: ['name/cos:PutObject'],
          resource: ['*'],
        },
      ],
    }),
  ),
  DurationSeconds: 1800,
});

πŸ“ CHANGELOG

License MIT

nest-tencent-cloud-sdk's People

Contributors

github-actions[bot] avatar guotingchao avatar

Stargazers

Arlen MacGyver avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.