import cos from 'cos-nodejs-sdk-v5' ; import path from 'path' ; import fs from 'fs-extra' ; export interface TencentOSSOption { SecretId : string ; SecretKey : string ; Region : string ; Bucket : string ; } export class TencentOSS { SecretId : string ; SecretKey : string ; Region : string ; Bucket : string ; constructor ( opt ) { this . SecretId = opt . SecretId ; this . SecretKey = opt . SecretKey ; this . Region = opt . Region ; this . Bucket = opt . Bucket ; } async _upload ( imgPath ) { const COS = new cos ( { SecretId : this . SecretId , SecretKey : this . SecretKey , } ) ; const p = path . parse ( imgPath ) ; let data = await new Promise ( ( res , rej ) => { COS . putObject ( { Bucket : this . Bucket , Region : this . Region , Key : 'media/image/' + p . base , Body : fs . createReadStream ( imgPath ) , } , function ( err , data ) { if ( err ) { rej ( err ) ; } else { res ( data ) ; } } ) ; } ) ; return data ; } async upload ( imgPath ) { const res = await this . _upload ( imgPath ) ; return 'https://' + res . Location ; } }