采纳答案成功!
向帮助你的同学说点啥吧!感谢那些助人为乐的人
exports.video = function *(next) { var body = this.request.body var videoData = body.video var user = this.session.user if (!videoData || !videoData.key) { this.body = { success: false, err: '视频没有上传成功!' } return next } var video = yield Video.findOne({ qiniu_key: videoData.key }) .exec() if (!video) { video = new Video({ author: user._id, qiniu_key: videoData.key, persistentId: videoData.persistentId }) video = yield video.save() } var url = config.qiniu.video + video.qiniu_key robot .uploadToCloudinary(url) .then(function(data) { if (data && data.public_id) { video.public_id = data.public_id video.detail = data video.save().then(function(_video) { asyncMedia(_video._id) }) } }) this.body = { success: true, data: video._id } }
这是我的代码,可以比对下。另外,里面的的 api key secret 什么的,一定换成自己的哈
在 var url = config.qiniu.video + video.qiniu_key 前面加一下 console.log(video) 看看这个 video 是什么,然后看能不能通过地址直接访问到这个 video 地址。
console.log(video) var url = config.qiniu.video + video.qiniu_key robot .uploadToCloudinary(url) .then(function(data){ if(data && data.public_id){ video.public_id = data.public_id video.detail = data video.save().then(function(_video){ asyncMedia(_video._id ) }) } }) .catch((err) => { console.log(err) }) this.body = { success: true, data: video._id } api key secret 我检查了是我自己账号的 打印了video zhoujialindembp:imoocServer shadow$ node app Listening: 1234 <-- POST /api/signature --> POST /api/signature 200 62ms 319b <-- POST /api/creations/video { __v: 0, author: 580383804be040367c11643e, qiniu_key: '243974bb-766a-47c2-a407-df83c4adae64.mp4', persistentId: 'z0.581f39df45a2655db7826c40', _id: 581f39df4226b5dc59e94efb, meta: { updateAt: Sun Nov 06 2016 22:10:39 GMT+0800 (CST), createAt: Sun Nov 06 2016 22:10:39 GMT+0800 (CST) } } --> POST /api/creations/video 200 105ms 50b { error: { [Error: ENOENT: no such file or directory, open 'ofuein66v.bkt.clouddn.com243974bb-766a-47c2-a407-df83c4adae64.mp4'] errno: -2, code: 'ENOENT', syscall: 'open', path: 'ofuein66v.bkt.clouddn.com243974bb-766a-47c2-a407-df83c4adae64.mp4' } }
robot.js文件里
cloudinary.config(config.cloudinary)
exports.uploadToCloudinary = function(url){
return new Promise(function(resolve, reject){
cloudinary.uploader.upload(url, function(result){
if(result && result.public_id){
resolve(result)
}else{
reject(result)
}
}, {
resource_type: 'video',
folder: 'video' //指定上传到哪个文件夹下
})
})
}
登录后可查看更多问答,登录/注册