import OSS from 'ali-oss'
import { getSTSToken } from '@/api/base'

// 获取token
export async function getToken() {
  const { data } = await getSTSToken()
  return { ...data, accessKeyId: data.AccessKeyId, accessKeySecret: data.AccessKeySecret, stsToken: data.SecurityToken }
}

// 创建oss
export async function createOSS() {
  const store = new OSS({
    bucket: 'zxts-book-file',
    ...(await getToken()),
    refreshSTSToken: async () => {
      return await getToken()
    },
    refreshSTSTokenInterval: 14 * 60 * 1000
  })
  return store
}
