Comparing version 2.1.0 to 2.2.0
@@ -28,4 +28,4 @@ var crypto = require('crypto'); | ||
hmac.update(signature); | ||
var digest = hmac.digest('base64'); | ||
return util.base64ToUrlsafe(digest); | ||
var digest = hmac.digest('base64'); | ||
return util.base64ToUrlsafe(digest); | ||
}; | ||
@@ -32,0 +32,0 @@ |
@@ -13,9 +13,9 @@ var crypto = require('crypto'); | ||
function checksum(opt, body) { | ||
var hmac = crypto.createHmac('sha1', conf.SECRET_KEY); | ||
hmac.update(opt.path + "\n"); | ||
if (body) { | ||
hmac.update(body); | ||
} | ||
var digest = hmac.digest('base64'); | ||
return util.base64ToUrlsafe(digest); | ||
var hmac = crypto.createHmac('sha1', conf.SECRET_KEY); | ||
hmac.update(opt.path + "\n"); | ||
if (body) { | ||
hmac.update(body); | ||
} | ||
var digest = hmac.digest('base64'); | ||
return util.base64ToUrlsafe(digest); | ||
} | ||
@@ -30,3 +30,3 @@ | ||
Client.prototype.auth = function(opt, params) { | ||
opt.headers['Authorization'] = 'QBox ' + conf.ACCESS_KEY + ':' + checksum(opt, params); | ||
opt.headers['Authorization'] = 'QBox ' + conf.ACCESS_KEY + ':' + checksum(opt, params); | ||
}; | ||
@@ -36,61 +36,61 @@ | ||
var u = uri.parse(url); | ||
var opt = { | ||
headers: {'Accept': 'application/json', 'Accept-Encoding': 'gzip, deflate'}, | ||
host: u.hostname, | ||
port: u.port, | ||
path: u.path, | ||
method: 'POST' | ||
}; | ||
var u = uri.parse(url); | ||
var opt = { | ||
headers: {'Accept': 'application/json', 'Accept-Encoding': 'gzip, deflate'}, | ||
host: u.hostname, | ||
port: u.port, | ||
path: u.path, | ||
method: 'POST' | ||
}; | ||
var proto; | ||
if (u.protocol === 'https:') { | ||
proto = https; | ||
} else { | ||
proto = http; | ||
} | ||
var proto; | ||
if (u.protocol === 'https:') { | ||
proto = https; | ||
} else { | ||
proto = http; | ||
} | ||
var body; | ||
var isStream = false; | ||
var contentLength = 0; | ||
var contentType = 'application/x-www-form-urlencoded'; | ||
if (params) { | ||
if (params instanceof util.Binary) { | ||
contentType = 'application/octet-stream'; | ||
contentLength = params.bytes; | ||
isStream = true; | ||
} else if (params instanceof util.Form) { | ||
contentType = params.contentType; | ||
contentLength = null; | ||
isStream = true; | ||
} else { | ||
if (typeof params === 'string') { | ||
body = params; | ||
} else { | ||
body = querystring.stringify(params); | ||
} | ||
contentLength = body.length; | ||
} | ||
} | ||
var body; | ||
var isStream = false; | ||
var contentLength = 0; | ||
var contentType = 'application/x-www-form-urlencoded'; | ||
if (params) { | ||
if (params instanceof util.Binary) { | ||
contentType = 'application/octet-stream'; | ||
contentLength = params.bytes; | ||
isStream = true; | ||
} else if (params instanceof util.Form) { | ||
contentType = params.contentType; | ||
contentLength = null; | ||
isStream = true; | ||
} else { | ||
if (typeof params === 'string') { | ||
body = params; | ||
} else { | ||
body = querystring.stringify(params); | ||
} | ||
contentLength = body.length; | ||
} | ||
} | ||
opt.headers['Content-Type'] = contentType; | ||
if (contentLength !== null) { | ||
opt.headers['Content-Length'] = contentLength; | ||
opt.headers['Content-Length'] = contentLength; | ||
} | ||
this.auth(opt, body); | ||
this.auth(opt, body); | ||
var req = proto.request(opt, onresp); | ||
req.on('error', onerror); | ||
var req = proto.request(opt, onresp); | ||
req.on('error', onerror); | ||
if (params) { | ||
if (isStream) { | ||
params.stream.pipe(req); | ||
} else { | ||
req.end(params); | ||
} | ||
} else { | ||
req.end(); | ||
} | ||
return req; | ||
if (params) { | ||
if (isStream) { | ||
params.stream.pipe(req); | ||
} else { | ||
req.end(params); | ||
} | ||
} else { | ||
req.end(); | ||
} | ||
return req; | ||
}; | ||
@@ -100,37 +100,37 @@ | ||
var onresp = function(res) { | ||
util.readAll(res, function(data) { | ||
var ret; | ||
if (data.length === 0) { | ||
ret = {code: res.statusCode}; | ||
if (res.statusCode !== 200) { | ||
ret.error = 'E' + res.statusCode; | ||
} | ||
onret(ret); | ||
return; | ||
} | ||
try { | ||
ret = JSON.parse(data); | ||
if (res.statusCode === 200) { | ||
ret = {code: 200, data: ret}; | ||
} else { | ||
ret.code = res.statusCode; | ||
} | ||
} catch (e) { | ||
ret = {code: -2, error: e.toString(), detail: e}; | ||
} | ||
onret(ret); | ||
}); | ||
}; | ||
var onresp = function(res) { | ||
util.readAll(res, function(data) { | ||
var ret; | ||
if (data.length === 0) { | ||
ret = {code: res.statusCode}; | ||
if (res.statusCode !== 200) { | ||
ret.error = 'E' + res.statusCode; | ||
} | ||
onret(ret); | ||
return; | ||
} | ||
try { | ||
ret = JSON.parse(data); | ||
if (res.statusCode === 200) { | ||
ret = {code: 200, data: ret}; | ||
} else { | ||
ret.code = res.statusCode; | ||
} | ||
} catch (e) { | ||
ret = {code: -2, error: e.toString(), detail: e}; | ||
} | ||
onret(ret); | ||
}); | ||
}; | ||
var onerror = function(e) { | ||
var ret = { | ||
code: -1, | ||
error: e.message, | ||
detail: e | ||
}; | ||
onret(ret); | ||
}; | ||
var onerror = function(e) { | ||
var ret = { | ||
code: -1, | ||
error: e.message, | ||
detail: e | ||
}; | ||
onret(ret); | ||
}; | ||
return this.execute(url, params, onresp, onerror); | ||
return this.execute(url, params, onresp, onerror); | ||
}; | ||
@@ -137,0 +137,0 @@ |
@@ -34,3 +34,3 @@ /* | ||
exports.mogrify = function(source_img_url, opts){ | ||
return source_img_url + '?' + this.mkMogrifyParams(opts); | ||
return source_img_url + '?' + this.mkMogrifyParams(opts); | ||
}; |
313
lib/rs.js
@@ -12,4 +12,4 @@ var fs = require('fs'); | ||
exports.mkbucket = function(conn, bucketname, onret) { | ||
var url = config.RS_HOST + '/mkbucket/' + bucketname; | ||
conn.callWith(url, null, onret); | ||
var url = config.RS_HOST + '/mkbucket/' + bucketname; | ||
conn.callWith(url, null, onret); | ||
}; | ||
@@ -21,103 +21,103 @@ | ||
function Service(conn, bucket) { | ||
this.conn = conn; | ||
this.bucket = bucket; | ||
this.conn = conn; | ||
this.bucket = bucket; | ||
} | ||
Service.prototype.buckets = function(onret) { | ||
var url = config.RS_HOST + '/buckets'; | ||
this.conn.callWith(url, null, onret); | ||
var url = config.RS_HOST + '/buckets'; | ||
this.conn.callWith(url, null, onret); | ||
}; | ||
Service.prototype.putAuth = function(onret) { | ||
/* | ||
* func PutAuth() => PutAuthRet | ||
* 上传授权(生成一个短期有效的可匿名上传URL) | ||
**/ | ||
var url = config.IO_HOST + '/put-auth/'; | ||
this.conn.callWith(url, null, onret); | ||
/* | ||
* func PutAuth() => PutAuthRet | ||
* 上传授权(生成一个短期有效的可匿名上传URL) | ||
**/ | ||
var url = config.IO_HOST + '/put-auth/'; | ||
this.conn.callWith(url, null, onret); | ||
}; | ||
Service.prototype.putAuthEx = function(expires, callbackUrl, onret) { | ||
/* | ||
* func PutAuthEx(expires, callbackUrl) => PutAuthRet | ||
* 上传授权(生成一个短期有效的可匿名上传URL) | ||
**/ | ||
var url = config.IO_HOST + '/put-auth/' + expires + '/callback/' + util.encode(callbackUrl); | ||
this.conn.callWith(url, null, onret); | ||
/* | ||
* func PutAuthEx(expires, callbackUrl) => PutAuthRet | ||
* 上传授权(生成一个短期有效的可匿名上传URL) | ||
**/ | ||
var url = config.IO_HOST + '/put-auth/' + expires + '/callback/' + util.encode(callbackUrl); | ||
this.conn.callWith(url, null, onret); | ||
}; | ||
Service.prototype.put = function(key, mimeType, fp, bytes, onret) { | ||
/* | ||
* func Put(key string, mimeType string, fp File, bytes int64) => (data PutRet, code int, err Error) | ||
* 上传一个流 | ||
**/ | ||
if (!mimeType) { | ||
mimeType = 'application/octet-stream'; | ||
} | ||
var entryURI = this.bucket + ':' + key; | ||
var url = config.IO_HOST + '/rs-put/' + util.encode(entryURI) + '/mimeType/' + util.encode(mimeType); | ||
var binary = new util.Binary(fp, bytes); | ||
return this.conn.callWith(url, binary, onret); | ||
/* | ||
* func Put(key string, mimeType string, fp File, bytes int64) => (data PutRet, code int, err Error) | ||
* 上传一个流 | ||
**/ | ||
if (!mimeType) { | ||
mimeType = 'application/octet-stream'; | ||
} | ||
var entryURI = this.bucket + ':' + key; | ||
var url = config.IO_HOST + '/rs-put/' + util.encode(entryURI) + '/mimeType/' + util.encode(mimeType); | ||
var binary = new util.Binary(fp, bytes); | ||
return this.conn.callWith(url, binary, onret); | ||
}; | ||
Service.prototype.putFile = function(key, mimeType, localFile, onret) { | ||
/* | ||
* func PutFile(key string, mimeType string, localFile string) => (data PutRet, code int, err Error) | ||
* 上传文件 | ||
**/ | ||
var self = this; | ||
if (!mimeType) { | ||
mimeType = mime.lookup(localFile); | ||
} | ||
fs.stat(localFile, function(err, fi) { | ||
if (err) { | ||
onret({code: -1, error: err.toString(), detail: err}); | ||
return; | ||
} | ||
var fp = fs.createReadStream(localFile); | ||
self.put(key, mimeType, fp, fi.size, onret); | ||
}); | ||
/* | ||
* func PutFile(key string, mimeType string, localFile string) => (data PutRet, code int, err Error) | ||
* 上传文件 | ||
**/ | ||
var self = this; | ||
if (!mimeType) { | ||
mimeType = mime.lookup(localFile); | ||
} | ||
fs.stat(localFile, function(err, fi) { | ||
if (err) { | ||
onret({code: -1, error: err.toString(), detail: err}); | ||
return; | ||
} | ||
var fp = fs.createReadStream(localFile); | ||
self.put(key, mimeType, fp, fi.size, onret); | ||
}); | ||
}; | ||
Service.prototype.upload = function(upToken, key, mimeType, filename, stream, onret) { | ||
/* | ||
* func Upload(upToken string, key string, mimeType string, filename string, stream ReadStream) => (data PutRet, code int, err Error) | ||
* 以multipart/form-data形式上传ReadStream | ||
**/ | ||
var self = this; | ||
if (!mimeType) { | ||
mimeType = mime.lookup(filename); | ||
} | ||
var entryURI = this.bucket + ':' + key; | ||
entryURI = '/rs-put/' + util.encode(entryURI) + '/mimeType/' + util.encode(mimeType); | ||
/* | ||
* func Upload(upToken string, key string, mimeType string, filename string, stream ReadStream) => (data PutRet, code int, err Error) | ||
* 以multipart/form-data形式上传ReadStream | ||
**/ | ||
var self = this; | ||
if (!mimeType) { | ||
mimeType = mime.lookup(filename); | ||
} | ||
var entryURI = this.bucket + ':' + key; | ||
entryURI = '/rs-put/' + util.encode(entryURI) + '/mimeType/' + util.encode(mimeType); | ||
var form = formstream(); | ||
form.field('action', entryURI); | ||
form.stream('file', stream, filename, mimeType); | ||
var form = formstream(); | ||
form.field('action', entryURI); | ||
form.stream('file', stream, filename, mimeType); | ||
form = new util.Form(form, form.headers()['Content-Type']); | ||
return this.conn.callWith(upToken, form, onret); | ||
form = new util.Form(form, form.headers()['Content-Type']); | ||
return this.conn.callWith(upToken, form, onret); | ||
}; | ||
Service.prototype.uploadFile = function(upToken, key, mimeType, localFile, onret) { | ||
/* | ||
* func UploadFile(upToken string, key string, mimeType string, localFile string) => (data PutRet, code int, err Error) | ||
* 以multipart/form-data形式上传文件 | ||
**/ | ||
var self = this; | ||
if (!mimeType) { | ||
mimeType = mime.lookup(localFile); | ||
} | ||
fs.stat(localFile, function(err, fi) { | ||
if (err) { | ||
onret({code: -1, error: err.toString(), detail: err}); | ||
return; | ||
} | ||
var filename = path.basename(localFile); | ||
var stream = fs.createReadStream(localFile); | ||
self.upload(upToken, key, mimeType, filename, stream, onret); | ||
}); | ||
/* | ||
* func UploadFile(upToken string, key string, mimeType string, localFile string) => (data PutRet, code int, err Error) | ||
* 以multipart/form-data形式上传文件 | ||
**/ | ||
var self = this; | ||
if (!mimeType) { | ||
mimeType = mime.lookup(localFile); | ||
} | ||
fs.stat(localFile, function(err, fi) { | ||
if (err) { | ||
onret({code: -1, error: err.toString(), detail: err}); | ||
return; | ||
} | ||
var filename = path.basename(localFile); | ||
var stream = fs.createReadStream(localFile); | ||
self.upload(upToken, key, mimeType, filename, stream, onret); | ||
}); | ||
}; | ||
Service.prototype.uploadWithToken = function(uploadToken, filename, stream, bucket, key, mimeType, customMeta, callbackParams, enableCrc32Check, onret) { | ||
Service.prototype.uploadWithToken = function(uploadToken, filename, stream, key, mimeType, customMeta, callbackParams, enableCrc32Check, onret) { | ||
/* | ||
@@ -127,2 +127,3 @@ * func UploadWithToken(uploadToken, filename, stream, bucket, key, mimeType, customMeta, callbackParams, enableCrc32Check, onret) => (data PutRet, code int, err Error) | ||
**/ | ||
var bucket = this.bucket; | ||
var actionString = util.generateActionString(filename, bucket, key, mimeType, customMeta, enableCrc32Check); | ||
@@ -152,3 +153,3 @@ if (callbackParams === null) { | ||
Service.prototype.uploadFileWithToken = function(uploadToken, localFile, bucket, key, mimeType, customMeta, callbackParams, enableCrc32Check, onret) { | ||
Service.prototype.uploadFileWithToken = function(uploadToken, localFile, key, mimeType, customMeta, callbackParams, enableCrc32Check, onret) { | ||
/* | ||
@@ -160,80 +161,80 @@ * func UploadFileWithToken(uploadToken, localFile, bucket, key, mimeType, customMeta, callbackParams, enableCrc32Check, onret) => (data PutRet, code int, err Error) | ||
if (!mimeType) { | ||
mimeType = mime.lookup(localFile); | ||
} | ||
fs.stat(localFile, function(err, fi) { | ||
if (err) { | ||
onret({code: -1, error: err.toString(), detail: err}); | ||
return; | ||
} | ||
var filename = path.basename(localFile); | ||
var stream = fs.createReadStream(localFile); | ||
self.uploadWithToken(uploadToken, filename, stream, bucket, key, mimeType, customMeta, callbackParams, enableCrc32Check, onret); | ||
}); | ||
mimeType = mime.lookup(localFile); | ||
} | ||
fs.stat(localFile, function(err, fi) { | ||
if (err) { | ||
onret({code: -1, error: err.toString(), detail: err}); | ||
return; | ||
} | ||
var filename = path.basename(localFile); | ||
var stream = fs.createReadStream(localFile); | ||
self.uploadWithToken(uploadToken, filename, stream, key, mimeType, customMeta, callbackParams, enableCrc32Check, onret); | ||
}); | ||
}; | ||
Service.prototype.get = function(key, attName, onret) { | ||
/* | ||
* func Get(key string, attName string) => GetRet | ||
* 下载授权(生成一个短期有效的可匿名下载URL) | ||
**/ | ||
var entryURI = this.bucket + ':' + key; | ||
var url = config.RS_HOST + '/get/' + util.encode(entryURI) + '/attName/' + util.encode(attName); | ||
this.conn.callWith(url, null, onret); | ||
/* | ||
* func Get(key string, attName string) => GetRet | ||
* 下载授权(生成一个短期有效的可匿名下载URL) | ||
**/ | ||
var entryURI = this.bucket + ':' + key; | ||
var url = config.RS_HOST + '/get/' + util.encode(entryURI) + '/attName/' + util.encode(attName); | ||
this.conn.callWith(url, null, onret); | ||
}; | ||
Service.prototype.getIfNotModified = function(key, attName, base, onret) { | ||
/* | ||
* func GetIfNotModified(key string, attName string, base string) => GetRet | ||
* 下载授权(生成一个短期有效的可匿名下载URL),如果服务端文件没被人修改的话(用于断点续传) | ||
**/ | ||
var entryURI = this.bucket + ':' + key; | ||
var url = config.RS_HOST + '/get/' + util.encode(entryURI) + '/attName/' + util.encode(attName) + '/base/' + base; | ||
this.conn.callWith(url, null, onret); | ||
/* | ||
* func GetIfNotModified(key string, attName string, base string) => GetRet | ||
* 下载授权(生成一个短期有效的可匿名下载URL),如果服务端文件没被人修改的话(用于断点续传) | ||
**/ | ||
var entryURI = this.bucket + ':' + key; | ||
var url = config.RS_HOST + '/get/' + util.encode(entryURI) + '/attName/' + util.encode(attName) + '/base/' + base; | ||
this.conn.callWith(url, null, onret); | ||
}; | ||
Service.prototype.stat = function(key, onret) { | ||
/* | ||
* func Stat(key string) => Entry | ||
* 取资源属性 | ||
*/ | ||
var entryURI = this.bucket + ':' + key; | ||
var url = config.RS_HOST + '/stat/' + util.encode(entryURI); | ||
this.conn.callWith(url, null, onret); | ||
/* | ||
* func Stat(key string) => Entry | ||
* 取资源属性 | ||
*/ | ||
var entryURI = this.bucket + ':' + key; | ||
var url = config.RS_HOST + '/stat/' + util.encode(entryURI); | ||
this.conn.callWith(url, null, onret); | ||
}; | ||
Service.prototype.publish = function(domain, onret) { | ||
/* | ||
* func Publish(domain string) => Bool | ||
* 将本 Table 的内容作为静态资源发布。静态资源的url为:http://domain/key | ||
**/ | ||
var url = config.RS_HOST + '/publish/' + util.encode(domain) + '/from/' + this.bucket; | ||
this.conn.callWith(url, null, onret); | ||
/* | ||
* func Publish(domain string) => Bool | ||
* 将本 Table 的内容作为静态资源发布。静态资源的url为:http://domain/key | ||
**/ | ||
var url = config.RS_HOST + '/publish/' + util.encode(domain) + '/from/' + this.bucket; | ||
this.conn.callWith(url, null, onret); | ||
}; | ||
Service.prototype.unpublish = function(domain, onret) { | ||
/* | ||
* func Unpublish(domain string) => Bool | ||
* 取消发布 | ||
*/ | ||
var url = config.RS_HOST + '/unpublish/' + util.encode(domain); | ||
this.conn.callWith(url, null, onret); | ||
/* | ||
* func Unpublish(domain string) => Bool | ||
* 取消发布 | ||
*/ | ||
var url = config.RS_HOST + '/unpublish/' + util.encode(domain); | ||
this.conn.callWith(url, null, onret); | ||
}; | ||
Service.prototype.remove = function(key, onret) { | ||
/* | ||
* func Delete(key string) => Bool | ||
* 删除资源 | ||
**/ | ||
var entryURI = this.bucket + ':' + key; | ||
var url = config.RS_HOST + '/delete/' + util.encode(entryURI); | ||
this.conn.callWith(url, null, onret); | ||
/* | ||
* func Delete(key string) => Bool | ||
* 删除资源 | ||
**/ | ||
var entryURI = this.bucket + ':' + key; | ||
var url = config.RS_HOST + '/delete/' + util.encode(entryURI); | ||
this.conn.callWith(url, null, onret); | ||
}; | ||
Service.prototype.drop = function(onret) { | ||
/* | ||
* func Drop() => Bool | ||
* 删除整个表(慎用!) | ||
**/ | ||
var url = config.RS_HOST + '/drop/' + this.bucket; | ||
this.conn.callWith(url, null, onret); | ||
/* | ||
* func Drop() => Bool | ||
* 删除整个表(慎用!) | ||
**/ | ||
var url = config.RS_HOST + '/drop/' + this.bucket; | ||
this.conn.callWith(url, null, onret); | ||
}; | ||
@@ -245,7 +246,7 @@ | ||
Service.prototype.saveAs = function(key, source_url, opWithParams, onret) { | ||
var destEntryURI = this.bucket + ':' + key; | ||
var saveAsEntryURI = util.encode(destEntryURI); | ||
var saveAsParam = "/save-as/" + saveAsEntryURI; | ||
var newurl = source_url + '?' + opWithParams + saveAsParam; | ||
this.conn.callWith(newurl, null, onret); | ||
var destEntryURI = this.bucket + ':' + key; | ||
var saveAsEntryURI = util.encode(destEntryURI); | ||
var saveAsParam = "/save-as/" + saveAsEntryURI; | ||
var newurl = source_url + '?' + opWithParams + saveAsParam; | ||
this.conn.callWith(newurl, null, onret); | ||
}; | ||
@@ -267,4 +268,4 @@ | ||
Service.prototype.imageMogrifyAs = function(key, source_img_url, opts, onret) { | ||
var mogrifyParams = img.mkMogrifyParams(opts); | ||
this.saveAs(key, source_img_url, mogrifyParams, onret); | ||
var mogrifyParams = img.mkMogrifyParams(opts); | ||
this.saveAs(key, source_img_url, mogrifyParams, onret); | ||
}; | ||
@@ -277,28 +278,28 @@ | ||
* setStyle() - 设置图片预览风格别名 | ||
* unsetStyle() - 取消设置图片预览风格别名 | ||
* unsetStyle() - 取消设置图片预览风格别名 | ||
*/ | ||
Service.prototype.setProtected = function(protectedMode, onret){ | ||
var url = config.PUB_HOST + "/accessMode/" + this.bucket + "/mode/" + protectedMode; | ||
this.conn.callWith(url, null, onret); | ||
var url = config.PUB_HOST + "/accessMode/" + this.bucket + "/mode/" + protectedMode; | ||
this.conn.callWith(url, null, onret); | ||
}; | ||
Service.prototype.setSeparator = function(sep, onret){ | ||
sep = util.encode(sep); | ||
var url = config.PUB_HOST + "/separator/" + this.bucket + "/sep/" + sep; | ||
this.conn.callWith(url, null, onret); | ||
sep = util.encode(sep); | ||
var url = config.PUB_HOST + "/separator/" + this.bucket + "/sep/" + sep; | ||
this.conn.callWith(url, null, onret); | ||
}; | ||
Service.prototype.setStyle = function(name, style, onret){ | ||
name = util.encode(name); | ||
style = util.encode(style); | ||
var url = config.PUB_HOST + "/style/" + this.bucket + "/name/" + name + "/style/" + style; | ||
this.conn.callWith(url, null, onret); | ||
name = util.encode(name); | ||
style = util.encode(style); | ||
var url = config.PUB_HOST + "/style/" + this.bucket + "/name/" + name + "/style/" + style; | ||
this.conn.callWith(url, null, onret); | ||
}; | ||
Service.prototype.unsetStyle = function(name, onret){ | ||
name = util.encode(name); | ||
var url = config.PUB_HOST + "/unstyle/" + this.bucket + "/name/" + name; | ||
this.conn.callWith(url, null, onret); | ||
name = util.encode(name); | ||
var url = config.PUB_HOST + "/unstyle/" + this.bucket + "/name/" + name; | ||
this.conn.callWith(url, null, onret); | ||
}; | ||
// ------------------------------------------------------------------------------------------ |
@@ -9,8 +9,8 @@ var fs = require('fs'); | ||
exports.base64ToUrlsafe = function(v) { | ||
return v.replace(/\//g, '_').replace(/\+/g, '-'); | ||
return v.replace(/\//g, '_').replace(/\+/g, '-'); | ||
}; | ||
exports.encode = function(v) { | ||
var encoded = new Buffer(v || '').toString('base64'); | ||
return exports.base64ToUrlsafe(encoded); | ||
var encoded = new Buffer(v || '').toString('base64'); | ||
return exports.base64ToUrlsafe(encoded); | ||
}; | ||
@@ -55,28 +55,28 @@ | ||
exports.readAll = function(strm, ondata) { | ||
var out = []; | ||
var total = 0; | ||
strm.on('data', function(chunk) { | ||
out.push(chunk); | ||
total += chunk.length; | ||
}); | ||
strm.on('end', function() { | ||
var data; | ||
switch (out.length) { | ||
case 0: | ||
data = new Buffer(0); | ||
break; | ||
case 1: | ||
data = out[0]; | ||
break; | ||
default: | ||
data = new Buffer(total); | ||
var pos = 0; | ||
for (var i = 0; i < out.length; i++) { | ||
var chunk = out[i]; | ||
chunk.copy(data, pos); | ||
pos += chunk.length; | ||
} | ||
} | ||
ondata(data); | ||
}); | ||
var out = []; | ||
var total = 0; | ||
strm.on('data', function(chunk) { | ||
out.push(chunk); | ||
total += chunk.length; | ||
}); | ||
strm.on('end', function() { | ||
var data; | ||
switch (out.length) { | ||
case 0: | ||
data = new Buffer(0); | ||
break; | ||
case 1: | ||
data = out[0]; | ||
break; | ||
default: | ||
data = new Buffer(total); | ||
var pos = 0; | ||
for (var i = 0; i < out.length; i++) { | ||
var chunk = out[i]; | ||
chunk.copy(data, pos); | ||
pos += chunk.length; | ||
} | ||
} | ||
ondata(data); | ||
}); | ||
}; | ||
@@ -88,4 +88,4 @@ | ||
function Binary(stream, bytes) { | ||
this.stream = stream; | ||
this.bytes = bytes; | ||
this.stream = stream; | ||
this.bytes = bytes; | ||
} | ||
@@ -98,4 +98,4 @@ | ||
function Form(stream, contentType) { | ||
this.stream = stream; | ||
this.contentType = contentType; | ||
this.stream = stream; | ||
this.contentType = contentType; | ||
} | ||
@@ -102,0 +102,0 @@ |
{ | ||
"name": "qiniu", | ||
"version": "2.1.0", | ||
"version": "2.2.0", | ||
"description": "Node wrapper for Qiniu Resource (Cloud) Storage API", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
169
README.md
@@ -30,96 +30,95 @@ # Node wrapper for Qiniu Resource (Cloud) Storage API | ||
var qiniu = require('../index.js'); | ||
var mime = require('mime'); | ||
var qiniu = require('qiniu'); | ||
qiniu.conf.ACCESS_KEY = '<Please apply your access key>'; | ||
qiniu.conf.SECRET_KEY = '<Dont send your secret key to anyone>'; | ||
// 配置密钥 | ||
qiniu.conf.ACCESS_KEY = '<Please apply your access key>'; | ||
qiniu.conf.SECRET_KEY = '<Dont send your secret key to anyone>'; | ||
var key = __filename; | ||
var friendName = key; | ||
var bucket = 'qiniu_test_bucket'; | ||
var DEMO_DOMAIN = bucket + '.dn.qbox.me'; | ||
// 实例化带授权的 HTTP Client 对象 | ||
var conn = new qiniu.digestauth.Client(); | ||
var conn = new qiniu.digestauth.Client(); | ||
// 创建空间,也可以在开发者自助网站创建 | ||
var bucket = 'yet_another_bucket'; | ||
qiniu.rs.mkbucket(conn, bucket, function(resp) { | ||
console.log("\n===> Make bucket result: ", resp); | ||
if (resp.code != 200) { | ||
return; | ||
} | ||
}); | ||
// 创建bucket | ||
qiniu.rs.mkbucket(conn, bucket, function(resp) { | ||
console.log("\n===> Make bucket result: ", resp); | ||
if (resp.code != 200) { | ||
return; | ||
} | ||
}); | ||
// 实例化 Bucket 操作对象 | ||
var rs = new qiniu.rs.Service(conn, bucket); | ||
// 使用新创建的bucket创建RS服务 | ||
var rs = new qiniu.rs.Service(conn, bucket); | ||
// 上传文件第1步 | ||
// 生成上传授权凭证(uploadToken) | ||
var opts = { | ||
scope: "yet_another_bucket", | ||
expires: 3600, | ||
callbackUrl: "http://www.example.com/notifications/qiniurs", // 可选 | ||
callbackBodyType: "application/x-www-form-urlencoded", // 可选 | ||
customer: "username@example.com" // 可选 | ||
}; | ||
var token = new qiniu.auth.UploadToken(opts); | ||
var uploadToken = token.generateToken(); | ||
var opts = { | ||
scope: bucket, | ||
expires: 3600, | ||
callbackUrl: null, | ||
callbackBodyType: null, | ||
customer: "sunikbear@gmail.com" | ||
}; | ||
// 组装Upload Token上传参数 | ||
// 产生规格详见文档:http://docs.qiniutek.com/v3/api/io/ | ||
var localFile = key, | ||
customMeta = "", | ||
callbackParams = {}, | ||
enableCrc32Check = false; | ||
// 创建 Upload Token | ||
var token = new qiniu.auth.UploadToken(opts); | ||
var uploadToken = token.generateToken(); | ||
var mimeType = mime.lookup(key); | ||
// 上传文件第2步 | ||
// 组装上传文件所需要的参数 | ||
var key = __filename; | ||
var localFile = key, | ||
customMeta = "", | ||
callbackParams = {"bucket": bucket, "key": key}, | ||
enableCrc32Check = false, | ||
mimeType = mime.lookup(key); | ||
// 使用Upload Token以multipart/form-data形式上传文件 | ||
rs.uploadFileWithToken(uploadToken, localFile, bucket, key, mimeType, customMeta, callbackParams, enableCrc32Check, function(resp){ | ||
console.log("\n===> Upload File with Token result: ", resp); | ||
if (resp.code != 200) { | ||
clear(rs); | ||
return; | ||
} | ||
// 查看文件属性 | ||
rs.stat(key, function(resp) { | ||
console.log("\n===> Stat result: ", resp); | ||
if (resp.code != 200) { | ||
clear(rs); | ||
return; | ||
} | ||
// 获取文件下载链接(含文件属性信息) | ||
rs.get(key, friendName, function(resp) { | ||
console.log("\n===> Get result: ", resp); | ||
if (resp.code != 200) { | ||
clear(rs); | ||
return; | ||
} | ||
// 删除文件 | ||
rs.remove(key, function(resp) { | ||
clear(rs); | ||
console.log("\n===> Delete result: ", resp); | ||
}); | ||
}); | ||
}); | ||
}); | ||
// 将bucket的内容作为静态内容发布 | ||
rs.publish(DEMO_DOMAIN, function(resp){ | ||
console.log("\n===> Publish result: ", resp); | ||
if (resp.code != 200){ | ||
clear(rs); | ||
return; | ||
} | ||
}); | ||
// 删除bucket,慎用! | ||
function clear(rs) { | ||
rs.drop(function(resp){ | ||
console.log("\n===> Drop result: ", resp); | ||
}); | ||
} | ||
// 上传文件第3步 | ||
// 上传文件 | ||
rs.uploadFileWithToken(uploadToken, localFile, key, mimeType, customMeta, callbackParams, enableCrc32Check, function(resp){ | ||
console.log("\n===> Upload File with Token result: ", resp); | ||
if (resp.code != 200) { | ||
// ... | ||
return; | ||
} | ||
// 查看已上传文件属性信息 | ||
rs.stat(key, function(resp) { | ||
console.log("\n===> Stat result: ", resp); | ||
if (resp.code != 200) { | ||
// ... | ||
return; | ||
} | ||
}); | ||
}); | ||
// 获取文件下载链接(含文件属性信息) | ||
var saveAsFriendlyName = key; | ||
rs.get(key, saveAsFriendlyName, function(resp) { | ||
console.log("\n===> Get result: ", resp); | ||
if (resp.code != 200) { | ||
// ... | ||
return; | ||
} | ||
}); | ||
// 删除已上传文件 | ||
rs.remove(key, function(resp) { | ||
console.log("\n===> Delete result: ", resp); | ||
}); | ||
// 将bucket的内容作为静态内容发布 | ||
var DEMO_DOMAIN = bucket + '.dn.qbox.me'; | ||
rs.publish(DEMO_DOMAIN, function(resp){ | ||
console.log("\n===> Publish result: ", resp); | ||
if (resp.code != 200){ | ||
clear(rs); | ||
return; | ||
} | ||
}); | ||
// 删除bucket,慎用! | ||
rs.drop(function(resp){ | ||
console.log("\n===> Drop result: ", resp); | ||
}); | ||
## 贡献代码 | ||
@@ -126,0 +125,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26266
557
138