🚨tencentcloud-sdk-nodejs 🚨cos-nodejs-sdk-v5
Language: English
适用于NestJS开发者的 tencentcloud-sdk-nodejs & cos-nodejs-sdk-v5
- npm
npm install @cardbrother/nest-tencent-cloud-sdk
- yarn
$ yarn add @cardbrother/nest-tencent-cloud-sdk
- 🚀 pnpm 推荐
$ pnpm add @cardbrother/nest-tencent-cloud-sdk
在
AppModule
或FeatureModule
模块中,导入TencentCloudModule
并调用forRoot
方法配置 Tencent Cloud SDK 的secretId
和secretKey
:
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, // 如果为 true,则 TencentCloudService 将是全局服务,默认为 false
}),
],
})
export class AppModule {}
如果您想使用动态配置,可以使用
forRootAsync
方法将您的secretId
和secretKey
配置为腾讯云 SDK:
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, // 如果为 true,则 TencentCloudService 将是全局服务,默认为 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, // 如果您想获取选项,可以使用这个
) {}
// 使用 OCR 服务
async useOCR(imgData: string) {
// 封装对象是智能的根据枚举动态实例化的
const ocrClient = await this.tencentCloudService.useClient('OCR'); // or SMS,COS,etc
return ocrClient.fastOcr({
ImageBase64: imgData,
});
}
}
💡 提示:如果在此处传递 Bucket、Region 或任何参数,则会覆盖导入模块时的配置;如果不传递,则会使用导入的
TencentCloudModule.forRoot
配置。
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,
});