Quantcast
Channel: CNode:Node.js专业中文社区
Viewing all articles
Browse latest Browse all 14821

使用七牛的问题,你们遇到过吗

$
0
0

使用七牛上传,遇到这个问题

/**
 * Created by yujintang on 2017/2/14.
 */
'use strict';

module.exports = function () {

    const cfg_qiniu = global.config.qiniu;
    const qiniu = require('qiniu');
    qiniu.conf.ACCESS_KEY = cfg_qiniu.AK;
    qiniu.conf.SECRET_KEY = cfg_qiniu.SK;
    let bucket = cfg_qiniu.bucket;

    let qn = {};

    /**
     * key: 上传到七牛上的文件名,
     * filePath: 要上传的本地路径
     */
    qn.upload = function (key, filePath, type) {

        //构建上传策略函数,设置回调的url以及需要回调给业务服务器的数据
        function uptoken(bucket, key) {
            var putPolicy = new qiniu.rs.PutPolicy(bucket + ":" + key);

            let fops = void 0;
            switch (type){
                case 'avatar':
                    fops = cfg_qiniu.avatar_style;
                    break;
                default:
                    fops = '';
                    break;
            }
            let saveas_key = qiniu.util.urlsafeBase64Encode(bucket+':'+key);
            fops = fops+'|saveas/'+saveas_key;
            putPolicy.persistentOps = fops
            return putPolicy.token();
        }

        //生成上传 Token
        let token = uptoken(bucket, key);

        //构造上传函数
        function uploadFile(uptoken, key, localFile) {
            var extra = new qiniu.io.PutExtra();
            qiniu.io.putFile(uptoken, key, localFile, extra);
        }

        //调用uploadFile上传
        uploadFile(token, key, filePath);
    };
    return qn;
}();

运行时候会报这个错误:

[2017-02-21 00:01:15.177] [ERROR] system - uncaughtException from process TypeError: onresp is not a function
    at /Users/yujintang/work/quhui/quehui_server/node_modules/qiniu/qiniu/rpc.js:57:5
    at done (/Users/yujintang/work/quhui/quehui_server/node_modules/urllib/lib/urllib.js:193:5)
    at IncomingMessage.<anonymous> (/Users/yujintang/work/quhui/quehui_server/node_modules/urllib/lib/urllib.js:304:7)
    at emitNone (events.js:91:20)
    at IncomingMessage.emit (events.js:186:7)
    at endReadableNT (_stream_readable.js:974:12)
    at _combinedTickCallback (internal/process/next_tick.js:74:11)
    at process._tickCallback (internal/process/next_tick.js:98:9)

Viewing all articles
Browse latest Browse all 14821

Trending Articles