nodejs 实现oss后端直传签名

发布于 2020-04-20  408 次阅读


直接贴代码就完事了

        const crypto = require('crypto');
        let end = new Date().getTime() + 3000000;
        let expiration = new Date(end).toISOString();
        let policyString = {
            expiration,
            conditions: [
                ["content-length-range", 0, 1048576000],
            ]
        };
        policyString = JSON.stringify(policyString);

        const policyFiled = Buffer.from(policyString).toString('base64');
        const signatureFiled = crypto.createHmac('sha1', accesssecret).update(policyFiled).digest('base64');
        let res = {
            signatureFiled,
            accessidFiled: accesskey,
            policyFiled,
            codeFiled: 0,
            url
        };
        return this.success(res);

变量 accesssecret为阿里分配的secret,accesskey为阿里分配的key,返回给前端用作验证的key进行上传,url为请求到阿里云oss桶到地址


一个逐渐被时间遗忘掉的人