Package Exports
- upyun
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (upyun) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
UPYUN Node SDK
UPYUN Node SDK, 集成:
安装
$ npm install upyun --save初始化
var upyun = new UpYun(bucket, operator, password [, endpoint], [, options]);参数
bucket: 你要使用的 upyun 空间名字.operator: 拥有bucket授权的操作员password: 拥有bucket授权的操作员的密码endpointAPI 接入点,可以刷是如下值:v0.api.upyun.com: 自动选择合适的线路v1.api.upyun.com: 电信线路v2.api.upyun.com: 联通(网通)线路v3.api.upyun.com: 移动(铁通)线路
optionsoptions.apiVersion如果不指定,则使用旧版 API,新版 API 可以指定为v2:options.secret如果指定,则可以使用 form 上传:
注:旧版 API 已不再更新,请指定 options.apiVersion 为
v2使用新版 API。
示例
var UpYun = require('upyun');
var upyun = new UpYun('testbucket', 'operatername', 'operaterpwd', 'v0.api.upyun.com', {
apiVersion: 'v2',
secret: 'yoursecret'
});
upyun.usage(function(err, result) {
//...
})响应结果
SDK 各 API 方法会按以下的统一格式返回数据:
{
'statusCode': 200, // HTTP 状态码
'headers': {
'server': 'vivi/0.6',
'date': 'Wed, 13 Aug 2014 02:15:27 GMT',
'content-type': 'application/json',
'content-length': '24'
}, // API 响应头部
'data': <响应体>
}注:如果请求出错,则
data里为具体的错误码和错误描述。如:
{
'statusCode': 401, // HTTP 状态码
'headers': { // API 响应头部
'server': 'vivi/0.6',
'date': 'Wed, 13 Aug 2014 02:19:07 GMT',
'content-type': 'application/json',
'content-length': '39'
},
'data': { // 错误信息
'code': 40400001,
'msg': 'file not found'
}
}详细细错误码及说明请参考 API 错误码表。
API
### usage(callback)获取空间使用状况.(单位:byte)
响应
{
statusCode: 200,
headers: { ... },
data: 21754
}### listDir(remotePath, limit, order, iter, callback)
遍历指定目录.
参数
remotePath欲遍历的目录limit限定每次请求的列表最大数目order以last_modified的值正序或者倒序排列asc(正序) 或desc(倒序).(Default:asc)iter遍历的起点(当指定limit小于实际文件数时,在第二次请求时候,指定此参数,即可继续上次的遍历)
响应
{
statusCode: 200,
headers: {
'x-upyun-list-iter': 'g2gCZAAEbmV4dGQAA2VvZg'
},
data: 'foo.jpg\tN\t4237\t1415096225\nbar\tF\t423404\t1415096260'
}### makeDir(remotePath, callback)
创建文件夹
参数
remotePath欲创建的目录路径
### removeDir(remotePath, callback)
删除文件夹
remotePath欲移除的目录路径
### putFile(remotePath, localFile, type, checksum, opts, callback)
上传文件
参数
remotePath文件存放路径localFile欲上传的文件,文件的本地路径 或者文件对应的 buffertype指定文件的Content-Type, 如果传null, 这时服务器会自动判断文件类型checksum为true时 SDK 会计算文件的 md5 值并将其传于 API 校验opts其他请求头部参数(以 JS 对象格式传入,常用于图片处理等需求). 更多请参考 官方 API 文档
响应
{
statusCode: 200,
headers: {...
},
data: ''
}### headFile(remotePath, callback)
HEAD 请求检测文件是否存在
参数
remotePath文件在 upyun 空间的路径
### getFile(remotePath, localPath, callback)
下载文件
参数
remotePath文件在 upyun 空间的路径localPath文件在本地存放路径, 如果localPath为null,文件的内容将会直接在响应的主体中返回
### deleteFile(remotePath, callback)
删除文件
参数
remotePath文件在 upyun 空间的路径
切换 API 接入点
参数
endpoint接入点v0.api.upyun.com: 自动选择合适的线路v1.api.upyun.com: 电信线路v2.api.upyun.com: 联通(网通)线路v3.api.upyun.com: 移动(铁通)线路
表单上传文件
参数
localFile欲上传的文件,文件的本地路径 或者 文件对应的 bufferopts其他请求头部参数(以 JS 对象格式传入,常用于图片处理等需求). 更多请参考 官方 API 文档signer外部签名函数,该参数据接收policy参数,需要返回签名后的字符串。
响应
{
statusCode: 200,
headers: {...
},
data: {
...
}
}示例
var opts = {
'save-key': '/test' + tempstr,
'Content-Type': 'image/jpg',
}
upyun.formPutFile('/path/to/local/file.jpg', opts,
function(policy){
return utils.md5sum(policy + '&' + <your secret>);
},
function(err, result) {
consule.log(result);
}
)
# tools
- tools.md5sum(string)
- tools.md5sumFile(file_path, callback)
- tools.makeSign(method, uri, date, length, password, operator)
- tools.policy(opts)
- tools.signature(policy, secret)
示例
var tools = require('upyun/tools');
tools.md5sum('123456');
tools.md5sumFile('./a.txt', function(err, md5_value) {
console.log(md5_value)
})
tools.makeSign(method, uri, date, length, password, operator)
tools.signature(policy, secret)备注
upyun npm package 曾为 James Chen 所有。
经过与其的交流协商,James 已经将此 npm 包转由 UPYUN 开发团队管理和维护。
后续的代码和版本更新,将于原有的项目无任何直接关系。
在 npm 上, "upyun": "<=0.0.3" 是由 James Chen 曾开发的 node-upyun 项目.
非常感谢 James Chen 对 upyun 的支持和贡献