oss.js 925 B

1234567891011121314151617181920212223242526272829303132
  1. import crypto from 'crypto-js';
  2. import { encode, decode } from './base64.js';
  3. import moment from 'moment'
  4. // 计算签名。
  5. function computeSignature(accessKeySecret, canonicalString) {
  6. return crypto.enc.Base64.stringify(crypto.HmacSHA1(canonicalString, accessKeySecret));
  7. }
  8. function getPolicy(){
  9. const date = new Date();
  10. date.setHours(date.getHours() + 1);
  11. const policyText = {
  12. expiration: date.toISOString(), // 设置policy过期时间。
  13. conditions: [
  14. // 限制上传大小。
  15. ["content-length-range", 0, 1024 * 1024 * 1024],
  16. ],
  17. };
  18. return policyText;
  19. }
  20. function getKey(i){
  21. let now=new Date();
  22. let date=moment(now).format('yyyyMMDD');
  23. let time=moment(now).format('HHmmss');
  24. let longTime=moment(now).format('yyyyMMDDHHmmss');
  25. let userId=JSON.parse(uni.getStorageSync('user')).id;
  26. return `${date}/${userId}/${time}/${longTime}${i}`
  27. }
  28. module.exports={
  29. computeSignature,
  30. getPolicy,
  31. getKey
  32. }