跳到主要内容
版本:2.0.0

对象存储(COS)

本文介绍了如何使用 midway 接入腾讯云 COS。

安装依赖

$ npm i @midwayjs/cos@2 --save

引入组件

首先,引入 组件,在 configuration.ts  中导入:

import { Configuration } from '@midwayjs/decorator';
import * as cos from '@midwayjs/cos';
import { join } from 'path';

@Configuration({
imports: [
cos, // 导入 cos 组件
],
importConfigs: [join(__dirname, 'config')],
})
export class ContainerLifeCycle {}

配置

比如:

单客户端配置

export const cos = {
client: {
SecretId: '***********',
SecretKey: '***********',
},
};

多个客户端配置,需要配置多个

export const cos = {
clients: {
instance1: {
SecretId: '***********',
SecretKey: '***********',
},
instance2: {
SecretId: '***********',
SecretKey: '***********',
},
},
};

更多参数可以查看 cos-nodejs-sdk-v5 文档。

使用 COS 服务

我们可以在任意的代码中注入使用。

import { Provide, Controller, Inject, Get } from '@midwayjs/decorator';
import { CosService } from '@midwayjs/cos';

@Provide()
export class UserService {

@Inject()
cosService: COSService;

async invoke() {
await this.cosService.sliceUploadFile({
Bucket: 'test-1250000000',
Region: 'ap-guangzhou',
Key: '1.zip',
FilePath: './1.zip'
},
}
}

可以使用 COSServiceFactory 获取不同的实例。

import { COSServiceFactory } from '@midwayjs/cos';
import { join } from 'path';

@Provide()
export class UserService {
@Inject()
cosServiceFactory: COSServiceFactory;

async save() {
const redis1 = await this.cosServiceFactory.get('instance1');
const redis2 = await this.cosServiceFactory.get('instance3');

//...
}
}