import * as OSS from 'ali-oss' ; import fetch from './fetch' ; let ossClient ; let endpoint = '/' ; export async function initOSS ( ) { const [ , token ] = await fetch ( 'getSTS' ) ; if ( token ?. enable ) { ossClient = new OSS ( { region : token . region , accessKeyId : token . AccessKeyId , accessKeySecret : token . AccessKeySecret , stsToken : token . SecurityToken , bucket : token . bucket , } ) ; if ( token . endpoint ) { endpoint = `//${ token . endpoint }/` ; } const OneHour = 1000 * 60 * 60 ; setInterval ( async ( ) => { const [ , refreshToken ] = await fetch ( 'getSTS' ) ; if ( refreshToken ?. enable ) { ossClient = new OSS ( { region : refreshToken . region , accessKeyId : refreshToken . AccessKeyId , accessKeySecret : refreshToken . AccessKeySecret , stsToken : refreshToken . SecurityToken , bucket : refreshToken . bucket , } ) ; } } , OneHour ) ; } } export function getOSSFileUrl ( url = '' , process = '' ) { const [ rawUrl = '' , extraPrams = '' ] = url . split ( '?' ) ; if ( ossClient && rawUrl . startsWith ( 'oss:' ) ) { const filename = rawUrl . slice ( 4 ) ; return `${ ossClient . signatureUrl ( filename , { expires : 300 , process } ) }${ extraPrams ? `&${ extraPrams }` : '' }` ; } if ( /\/\/cdn\.suisuijiang\.com/ . test ( rawUrl ) ) { return `${ rawUrl }?x-oss-process=${ process }${ extraPrams ? `&${ extraPrams }` : '' }` ; } return `${ url }` ; } param blob param fileName export default async function uploadFile ( blob , fileName ) { if ( ! ossClient ) { const [ uploadErr , result ] = await fetch ( 'uploadFile' , { file : blob , fileName , } ) ; if ( uploadErr ) { throw Error ( uploadErr ) ; } return result . url ; } const result = await ossClient . put ( fileName , blob ) ; if ( result . res . status === 200 ) { return endpoint + result . name ; } return Promise . reject ( '上传文件失败' ) ; }