ym-qiniu
v0.3.11
Published
## 引入 ---- ````javascript var QINIU = require('ym-modules').qiniu; var qiniu = new QINIU({ ACCESS_KEY: ACCESS_KEY, SECRET_KEY: SECRET_KEY, BUCKET: BUCKET, SERVER_URL: SERVER_URL, TOKEN_EXPIRE: TOKEN_EXPIRE TOKEN_CACHE_EXPIRE: TOKE
Downloads
17
Readme
七牛云存储模块
引入
var QINIU = require('@ym/qiniu');
var qiniu = new QINIU({
ACCESS_KEY: ACCESS_KEY,
SECRET_KEY: SECRET_KEY,
BUCKET: BUCKET,
SERVER_URL: SERVER_URL,
TOKEN_EXPIRE: TOKEN_EXPIRE,
TOKEN_CACHE_EXPIRE: TOKEN_CACHE_EXPIRE,
REDIS_STORE: REDIS_STORE
});
// ACCESS_KEY not null, 访问七牛云存储的公钥
// SECRET_KEY not null, 访问七牛云存储的密钥
// BUCKET not null, 要上传的空间
// SERVER_URL not null, 七牛云存储地址前缀
// TOKEN_EXPIRE 七牛私有链接默认失效时间,单位秒,default 600
// TOKEN_CACHE_EXPIRE 七牛私有链接token缓存时间,单位秒,default 590
// REDIS_STORE redis缓存库,与其他redis缓存区分,类型为NUMBER,default 3
上传文件
qiniu.upload(savekey, filePath, folder, cb).then(function(json){
console.log(json);
});
// 客户端上传文件采用form表单提交文件,在req.file.keyname中,path为七牛上传用的filePath,originFileName为七牛上传用的原始文件名
// 七牛上传后,为防止重名,七牛云存储存储的文件名都为编码过的唯一别名,原始文件名须在本地服务器另外开辟空间存储,存储过程可在回调函数cb中进行
// 重要!!:上传时请保持文件名后缀不要改变,在数据库中存储时也保证文件名的文件类型后缀有单独字段保存或文件名包含类型后缀,下载会用到
// savekey String,not null, 上传的文件的原始文件名
// filePath String,not null, 上传的文件的客户端路径
// folder Array,can null, 如果对上传的文件有需要分类的需求,可以添加此参数,此参数为一个数组,按照给定的文件夹顺序排列,如/images/jpg/savekey.jpg,则folder为['images','jpg']
// json upload函数运行后返回的是一个Promise,resolved的参数是一个json
// json={server_url:server_url,key:key}
// server_url是上传的服务地址,key是上传后的文件名, server_url+key是文件的服务器完整路径
// cb 回调函数,第一个参数为Promise的resoved,所以如果不用链式结构,可以写回调函数cb(json)
生成下载链接
qiniu.makeLink(key,deadTime,delay,fileName).then(function(link){
link......
});
// key not null,文件在云服务存储的别名
// deadTime 链接失效时间,单位为秒,如:20,3600,
// delay 生成链接的token缓存时间,单位秒,如10,默认比deadTime少10s
// fileName 指定下载的资源名,自己指定,需要包含资源类型后缀,如.png,.jpg,.zip等,如不指定,默认为七牛存储的key
// 当deadTime的参数为字符串是,deadTime为默认10分钟,而fileName为此时的deadTime.
//
// makeLink返回的是一个Promise
获取Buket中的文件列表
qiniu.getFileList(options,cb).then((data)=>{
data....
});
// options 一个json参数包
// options.bucket 指定要获取列表的bucket,默认为初始化时的bucket
// options.marker String,列表起点,默认为空字符串
// options.limit number获取列表的长度,default 1000
// options.prefix string,过滤出要获取的列表中文件包含的前缀,默认为空字符串
批量生成下载链接
qiniu.batchLinks(keysArray,deadTime).then(function(linksArray){
linksArray......
});
// keysArray not null, 文件名数组
// deadTime 链接失效时间
// batchLinks返回的是一个Promise
批量删除
qiniu.batchDelete(sourceArray, cb)
// sourceArray not null, 云存储资源的key数组,格式为[key1,key2,key3...];
// cb 回调函数,第一个参数为响应的结果,第二个参数为返回的状态码,正确响应时为200
// 接口运行时产生一个Promise对象,因此可以使用then
qiniu.batchDelete(sourceArray).then(function(body){
....
},function(err){
....
})