🚨tencentcloud-sdk-nodejs 🚨cos-nodejs-sdk-v5
Language: 中文
tencentcloud-sdk-nodejs & cos-nodejs-sdk-v5 for NestJS developers
- 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
In your
AppModule
orFeatureModule
module, import theTencentCloudModule
and call theforRoot
method to configure the Tencent Cloud SDK with yoursecretId
andsecretKey
:
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 {}
If you want to use a dynamic configuration, you can use the
forRootAsync
method to configure the Tencent Cloud SDK with yoursecretId
andsecretKey
:
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 {}
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,
});
}
}
💡 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',
});
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,
});