node 七牛云上传文件

2018-06-24 01:10:39来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

七牛云储存 nodejs qiniu 模块

信息配置

const qiniu = require('qiniu')

// 需要填写你的 Access Key 和 Secret Key
var accessKey = ''
var secretKey = ''
var mac = new qiniu.auth.digest.Mac(accessKey, secretKey)
// 要上传的空间
var bucket = 'image'
var options = {
 scope: bucket,
 callbackBody: '{"key":"$(key)","hash":"$(etag)","fsize":$(fsize),"bucket":"$(bucket)","name":"$(x:name)"}',
 callbackBodyType: 'application/json'
}
var putPolicy = new qiniu.rs.PutPolicy(options)
var uploadToken = putPolicy.uploadToken(mac)

var config = new qiniu.conf.Config()
// 是否使用https域名
// config.useHttpsDomain = true
// 上传是否使用cdn加速
// config.useCdnDomain = true

**
*在这里添加下面的方法
*

文件路径formUploader.putFile方法

var localFile = "/Users/jemy/Documents/qiniu.mp4";
var formUploader = new qiniu.form_up.FormUploader(config);
var putExtra = new qiniu.form_up.PutExtra();
var key='test.mp4';
// 文件上传
formUploader.putFile(uploadToken, key, localFile, putExtra, function(respErr,
  respBody, respInfo) {
  if (respErr) {
    throw respErr;
  }

  if (respInfo.statusCode == 200) {
    console.log(respBody);
  } else {
    console.log(respInfo.statusCode);
    console.log(respBody);
  }
});

文件字节方法formUploader.put

** 该方法七牛不支持 buffer类,可使用Buffer.toString()进行转换

var formUploader = new qiniu.form_up.FormUploader(config);
var putExtra = new qiniu.form_up.PutExtra();
var key='test.txt';
formUploader.put(uploadToken, key, "hello world", putExtra, function(respErr,
  respBody, respInfo) {
  if (respErr) {
    throw respErr;
  }

  if (respInfo.statusCode == 200) {
    console.log(respBody);
  } else {
    console.log(respInfo.statusCode);
    console.log(respBody);
  }
});

可读流上传方式formUploader.putStream

** 最易想到的就是fs.createReadStream 创建可读流对象,http类,以及child进程输出,输入流、更多了解查阅nodejs stream类

var formUploader = new qiniu.form_up.FormUploader(config);
var putExtra = new qiniu.form_up.PutExtra();
var readableStream = xxx; // 可读的流
formUploader.putStream(uploadToken, key, readableStream, putExtra, function(respErr,
  respBody, respInfo) {
  if (respErr) {
    throw respErr;
  }

  if (respInfo.statusCode == 200) {
    console.log(respBody);
  } else {
    console.log(respInfo.statusCode);
    console.log(respBody);
  }
});

断点续传resumeUploader.putFile调用一个新的resumeUploader.putStream对象

var localFile = "/Users/jemy/Documents/qiniu.mp4";
var resumeUploader = new qiniu.resume_up.ResumeUploader(config);
var putExtra = new qiniu.resume_up.PutExtra();
// 扩展参数
putExtra.params = {
  "x:name": "",
  "x:age": 27,
}
putExtra.fname = 'testfile.mp4';

// 如果指定了断点记录文件,那么下次会从指定的该文件尝试读取上次上传的进度,以实现断点续传
putExtra.resumeRecordFile = 'progress.log';
var key = null;
// 文件分片上传
resumeUploader.putFile(uploadToken, key, localFile, putExtra, function(respErr,
  respBody, respInfo) {
  if (respErr) {
    throw respErr;
  }

  if (respInfo.statusCode == 200) {
    console.log(respBody);
  } else {
    console.log(respInfo.statusCode);
    console.log(respBody);
  }
});

resumeUploader.putWithoutKey 调用 key为null的 resumeUploader.put方法
resumeUploader.putFileWithoutKey 调用 key为null的 resumeUploader.putFile方法

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:【1】ArcGIS API for JavaScript 4.5/4.6 本地部署

下一篇:JavaScript 事件