upyun js sdk, 支持服务端和客户端使用,集成:
$ npm install upyun --production --save
浏览器端手动安装时,需要手动引入 sdk 的依赖 axios
(考虑到方便 axios 被复用,浏览器版本构建时,没有引入此依赖)
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
再引入编译后的 sdk 文件
<script src="https://unpkg.com/upyun/dist/upyun.min.js"></script>
$ npm run test
所有的接口返回均是 Promise
const upyun = new upyun.Client(bucket[, options][, getHeaderSignCallback])
参数
bucket
: 又拍云服务(空间),Bucket 实例: new upyun.Bucket({bucketName: 'your bucket name', operatorName: 'your operator name', password: 'your operator password'})
options
: 配置项,可以配置以下参数
domain
: 又拍云 rest api 地址,默认 v0.api.upyun.com
其他可配置域名见又拍云文档
protocol
: 使用 http|https
协议,默认 https
协议getHeaderSignCallback
: 获取又拍云 HTTP Header 签名回调函数,服务端使用时不需要设置该参数。客户端使用必须设置该回调函数,它接受三个参数:bucket, method, path
,用于计算当前请求签名示例
const bucket = new upyun.Bucket('your bucket name', 'your operator name', 'your operator password')
const upyun = new upyun.Client(bucket);
/**
*@param bucket: Bucket 实例
*@param method: 当前请求的 API 使用的方法
*@param path: 当前请求的资源路径
*/
function getSignHeader(bucket, method, path) {
// 请求自己的服务器,计算当前 api 请求签名信息
// 可以参考该项目 sample 目录中的示例代码
...
}
const bucket = new upyun.Bucket('your bucket name')
const upyun = new upyun.Client(bucket, getSignHeader);
查看目录大小(单位: byte)
参数
path
: 目录路径示例
upyun.usage('/sub/dir').then(function(size) {
console.log('/sub/dir total used size: ' + size)
})
获取目录下文件列表
参数
remotePath
: 需要查看的目录路径options
:
limit
: 每次请求获取的目录最大列表,最大值 1000,默认 100order
: 列表以文件最后修改时间排序,可选值 asc|desc
,默认 asc
iter
: 遍历起点,每次响应参数中,将会包含遍历下一页需要的 iter
值响应
目录不存在,返回 false
,否则返回一个对象,结构如下:
{
files: [
{
name: 'example.txt', // file or dir name
type: 'N', // file type, N: file; F: dir
size: 28392812, // file size
time: 1486053098 // last modify time
}
],
next: 'dlam9pd2Vmd2Z3Zg==' // next page iter
}
通过 rest api 上传文件
参数
remotePath
: 文件保存路径(不需要 encodeURI,sdk 会统一处理)localFile
: 需要上传的文件。服务端支持 String | Stream | Buffer
, 浏览器端支持 File | String
options
: 其他可选参数 Content-MD5 | Content-Length | Content-Type | Content-Secret | x-gmkerl-thumb
(大小写无关,详见上传参数)响应
如果是非图片类文件,上传成功返回 true
, 否则返回一个对象,包含图片的基本信息:
{
width: 80,
height: 80,
'file-type': 'image/jpg',
frames: 1
}
如果上传失败,返回 false
创建目录
参数
remotePath
: 新建目录的路径响应
创建成功返回 true
,否则 false
HEAD
请求,获取文件基本信息
参数
remotePath
: 文件在又拍云服务(空间)的路径响应
文件不存在返回 false
。否则返回一个对象,结构如下,详见又拍云 HEAD
{
'file-type': 'file', // 文件类型
'file-size': 289239, // 文件大小
'file-date': 1486053098, // 文件创建时间
'Content-Md5': '...' // 文件 md5 值,该值可能不存在
}
删除文件或目录
参数
remotePath
: 文件或目录在又拍云服务的路径响应
删除成功返回 true
, 否则返回 false
下载保存在又拍云服务的文件
参数
remotePath
: 需要下载的文件路径saveStream
: 可选值,一个可以写入的流。若传递该参数,下载的文件将直接写入该流。该参数不支持浏览器端使用响应
如果文件不存在,将会返回 false
。文件存在时,若没有设置 saveStream
,该方法
将直接返回文件内容。若设置了 saveStream
,文件将直接写入该流,并返回流信息
示例
获取文件内容
upyun.getFile('/sample.txt').then(function (content) {
console.log(content) // will out put file content directly
})
写入其他流
const saveTo = fs.createWriteStream('./localSample.txt')
upyun.getFile('/sample.txt', saveTo).then(function (stream) {
// file has been saved to localSample.txt
// you can pipe the stream to anywhere you want
})
使用又拍云表单 api 上传文件。客户端使用该方法时, 必须先设置获取又拍云 HTTP Body 签名的回调函数
参数
remotePath
: 保存路径localFile
: 需要上传的文件,和 putFile
相同params
: 又拍云表单 api 支持的可选参数(save-key
bucket
两个必选参数不需要手动在这里设置)响应
成功返回 true
,失败返回 false
又拍云服务,包含以下属性
bucketName
服务名(空间名)operatorName
操作员名password
操作员密码,读取该属性时,获取的值是 md5 加密后的结果签名模块
upyun npm package 曾为 James Chen 所有。
经过与其的交流协商,James 已经将此 npm 包转由 UPYUN 开发团队管理和维护。
后续的代码和版本更新,将于原有的项目无任何直接关系。
在 npm 上, "upyun": "<=0.0.3"
是由 James Chen 曾开发的 node-upyun 项目.
非常感谢 James Chen 对 upyun 的支持和贡献
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。