oss.js 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. 'use strict';
  2. const Controller = require('egg').Controller;
  3. const moment = require("moment");
  4. const crypto = require("crypto");
  5. const HOST = "https://infish-oss.oss-cn-beijing.aliyuncs.com";
  6. class AliOssController extends Controller {
  7. async policy() {
  8. const { ctx } = this;
  9. let dir = ctx.request.query.dir;
  10. let time = moment(Date.now() + 30000).format("YYYY-MM-DDTHH:mm:ss.SSS") + "Z";
  11. let policyObj = JSON.stringify({
  12. "expiration": time,
  13. "conditions": [
  14. ["content-length-range", 0, 1048576000],
  15. ["starts-with", "$key", dir]
  16. ]
  17. })
  18. let policy = new Buffer(policyObj).toString('base64');
  19. let signature = crypto.createHmac('sha1', 'AHubozYEzXzi4fBL0ttLJ5sN1HMKA6').update(policy).digest().toString('base64');
  20. ctx.resultOK(
  21. {
  22. accessid: 'LTAI4GHqGdgY3b14jVRTzL2w',
  23. host:HOST,
  24. dir,
  25. policy,
  26. expire:time,
  27. signature
  28. }
  29. );
  30. }
  31. }
  32. module.exports = AliOssController;