這篇文章主要介紹NodeJS如何使用云服務(wù)器存儲上傳文件,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
我們提供的服務(wù)有:網(wǎng)站設(shè)計制作、網(wǎng)站設(shè)計、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認證、淮安區(qū)ssl等。為上千多家企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學管理、有技術(shù)的淮安區(qū)網(wǎng)站制作公司
1-準備工作
確認安裝node和npm
安裝 qiniu,formidable,express模塊
npm install --save qiniu formidable
[x] 導(dǎo)入模塊
let qiniu = require('qiniu'), formidable = require('formidable'), express = require('express'), router = express.Router();
2-服務(wù)器端的云存儲操作
2-1文件上傳
router.post('/qiniu', function (req, res, next) { let bucket = 'myblog', key = '', form = formidable.IncomingForm(), token, putPolicy; form.uploadDir = path.join(__dirname, '../../', 'public/img/upload'); form.keepExtensions = true form.parse(req, function (err, fields, files) { if (err) { console.log(err); } key = files.mypic.path.split(path.sep).pop(); putPolicy = new qiniu.rs.PutPolicy(bucket + ':' + key) //設(shè)置回調(diào) // putPolicy.callbackUrl = 'http://localhost:81/test/callback' // putPolicy.callbackBody = 'filename=$(fname)&filesize=$(fsize)' token = putPolicy.token() console.log('token', token) let extra = new qiniu.io.PutExtra() qiniu.io.putFile(token, key, files.mypic.path, extra, function (err, ret) { if (err) { console.log(err) } console.log('ret', ret); res.json({ token: token, ret: ret, files: files, fields: fields }) }) }) })
文件下載
router.get('/download/:key', function (req, res, next) { let url = 'http://xxxx.bkt.clouddn.com/' + req.params.key, policy = new qiniu.rs.GetPolicy(); console.log(url); res.json({url: policy.makeRequest(url)}) })
文件獲取
router.get('/info/:key', function (req, res, next) { let bucket = 'myblog', key = req.params.key, client = new qiniu.rs.Client(); client.stat(bucket, key, (err, ret) => { if (err) { console.log(err); } res.json(ret); }) })
以上是“NodeJS如何使用云服務(wù)器存儲上傳文件”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對大家有幫助,更多相關(guān)知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!
分享題目:NodeJS如何使用云服務(wù)器存儲上傳文件
網(wǎng)頁地址:http://chinadenli.net/article36/gjcjpg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站建設(shè)、品牌網(wǎng)站制作、定制開發(fā)、網(wǎng)站建設(shè)、靜態(tài)網(wǎng)站、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)