作者:落陌_漾吇 | 来源:互联网 | 2023-10-12 23:56
上传流程1.申请上传权限安装七牛云的SDKnpminstallqiniu在项目中引入,定义好其中鉴权对象macconstqiniurequire(qiniu)
上传流程
1.申请上传权限
npm install qiniu
在项目中引入,定义好其中鉴权对象mac
const qiniu = require ( 'qiniu' ) var accessKey = 'your access key' ; var secretKey = 'your secret key' ; var mac = new qiniu. auth. digest. Mac ( accessKey, secretKey) ;
accessKey 和 searetKey 可以在 https://portal.qiniu.com/user/key 查看 2.获取上传凭证
var options = { scope: 'mySpace' , expires: 7200 , returnBody: '{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}' } ; var putPolicy = new qiniu. rs. PutPolicy ( options) ; var uploadToken = putPolicy. uploadToken ( mac) ;
3.使用上传凭证上传文件并返回上传结果
var config = new qiniu. conf. Config ( ) ; config. zone = qiniu. zone. Zone_z0;
机房 Zone对象 华东 qiniu.zone.Zone_z0 华北 qiniu.zone.Zone_z1 华南 qiniu.zone.Zone_z2 华美 qiniu.zone.Zone_na0
var formUploader = new qiniu. form_up. FormUploader ( config) ; var putExtra = new qiniu. form_up. PutExtra ( ) ; formUploader. putFile ( uploadToken, null , './image.png' , putExtra, function ( respErr, respBody, respInfo) { if ( respErr) { throw respErr; } if ( respInfo. statusCode == 200 ) { console. log ( respBody) ; } else { console. log ( respInfo. statusCode) ; console. log ( respBody) ; } } ) ;
{ hash: 'Fheolstp8e4qRrWz1e9v8RBvczxO' , key: 'Fheolstp8e4qRrWz1e9v8RBvczxO' } { key: 'Fheolstp8e4qRrWz1e9v8RBvczxO' , hash: 'Fheolstp8e4qRrWz1e9v8RBvczxO' , fsize: 2145 , bucket: 'mySpace' , name: 'null' }
基本七牛云的上传流程就是这样了 官方文档 https://developer.qiniu.com/kodo/1289/nodejs