'use strict'; const Controller = require('egg').Controller; const moment = require("moment"); const crypto = require("crypto"); const HOST = "https://infish-oss.oss-cn-beijing.aliyuncs.com"; class AliOssController extends Controller { async policy() { const { ctx } = this; let dir = ctx.request.query.dir; let time = moment(Date.now() + 30000).format("YYYY-MM-DDTHH:mm:ss.SSS") + "Z"; let policyObj = JSON.stringify({ "expiration": time, "conditions": [ ["content-length-range", 0, 1048576000], ["starts-with", "$key", dir] ] }) let policy = new Buffer(policyObj).toString('base64'); let signature = crypto.createHmac('sha1', 'AHubozYEzXzi4fBL0ttLJ5sN1HMKA6').update(policy).digest().toString('base64'); ctx.resultOK( { accessid: 'LTAI4GHqGdgY3b14jVRTzL2w', host:HOST, dir, policy, expire:time, signature } ); } } module.exports = AliOssController;