mlog MOS javascript SDK
npm install @mlog/mossdkGavial Mos JavaScript SDK for the browser and Node.js
``js`
npm install @mlog/mossdk
// yarn add @mlog/mossdk
Node.js >= 8.0.0 required.
- IE >= 10 & Edge
- Major versions of Chrome/Firefox/Safari
- Major versions of Android/iOS/WP
`js`
const Mossdk = require("@mlog/mossdk").default;
// 参数为 Accesskey
let mos = new Mossdk({
baseUrl: "http://mlogcn.com",
token: "80dfba73e4d7d412e9cd223f0a52b3791"
});
`js`
import Mossdk from "@mlog/mossdk";
// 参数为 Accesskey
const mos = new Mossdk({
baseUrl: "http://mlogcn.com",
token: "80dfba73e4d7d412e9cd223f0a52b3791"
});
`html
`
创建一个 sdk 实例
参数:
- option { Object } bucket 配置
- token { String } accesskey
- baseUrl { String } 服务端地址
- useCache { Boolean } 非必填 查询 list prefix 时是否使用缓存,默认 fasle。
例子:
`js`
const mos = new Mossdk({
baseUrl: "http://mlogcn.com",
token: "80dfba73e4d7d412e9cd223f0a52b3791",
useCache: true
});
使用这个 bucket
参数:
- name { String } bucket 名称
例子:
- 使用 aa bucket.
`js`
mos.useBucket("aa");
返回值:
this mos 实例
参看这个 bucket 的信息
参数:
- name { String } bucket 名称
例子:
- 参看 aa 的信息
`js`
mos.getBucketInfo("aa").then(
res => {
// 查询成功
// 在不知道 bucke 的情况下,可以通过该接口确认 bucket是否存在。
mos.useBucket("aa");
},
err => {
// 失败
console.log(err);
}
);
返回值:
`json`
{
"createTime": "2018-11-05 08:34:12",
"id": "7e4932c355e947291ae1316d856e4",
"name": "aa",
"ownerId": 67,
"ownerType": "GROUP",
"protectType": "public"
}
查询 object 信息
参数:
- key { String } object key
例子:
获取 aa buckey 下 /q/ 信息
`jsaa
// 以下例子默认使用 bucket`
// mos.useBucket("aa").getObjectInfo("/q/")
mos.useBucket("aa");
mos.getObjectInfo("/q").then(res => {
console.log(res);
});
文件上传
参数:
- option { Object } 上传参数
- key { String } 上传后文件的 key
- content { File } 要上传的文件
- mediaType { String } 非必填 例如’image/jpeg’
- uploadProgress { Function } 非必填 上传进度事件
例子:
上传一个 test.docx
`js`
// 上传过程中触发,event 为原生获取上传进度的事件
function uploadProgress(event) {
console.log(event);
}
let file = document.querySelector("input").files[0];
mos
.put({
key: "/q/test.docx",
content: file,
uploadProgress: uploadProgress
})
.then(res => {
// 成功
console.log(res.message);
});
返回值:
`json`
{
"key": "/q/tt.docx",
"type": "upload",
"message": "success"
}
下载文件
参数:
- key { String } Object key
例子:
`js`
mos.get("/q/test.docx").then(res => {
console.log(res.message);
});
返回值:
`json`
{
"key": "/q/test.docx",
"type": "download",
"message": "success"
}
Object 目录遍历, useCache == true 时启用缓存模式。
参数:
- option { Object } 遍历参数
- delimiter { String } 目录对象 key
- prefix { String } 非必填 匹配前缀
- startKey { String } 非必填 该目录下最小文件名(包含)
- endKey { String } 非必填 该目录下最大文件名(包含)
- pageSize { Number } 非必填 单页数据最大条数
例子:
``js`
mos.list({ delimiter: "/q/", prefix: "t" }).then(res => {
console.log(res);
});
返回值:
- data { Array } 查出的 Object 列表
- hasMore { Boolean } 列表是否还有下一页
- pageIndex { Number } 列表当前页,开启'缓存模式'返回
- nextMarker { String } 下一页起始文件名
`json`
{
"data": [
{
"id": "7a7_test.docx",
"key": "/q/test.docx",
"name": "test.docx",
"length": 29370,
"mediaType": null,
"lastModifyTime": 1547445894935,
"bucket": "aa",
"attrs": {},
"contentEncoding": null
}
],
"hasMore": true,
"nextMarker": "/q/tt.docx"
// "pageIndex": 1
}
Object 目录遍历下一页数据。'缓存模式'开启且返回值 hasMore == true 时可调用,否则会报错。
例子:
`js`
mos.listNext().then(res => {
console.log(res);
});
Object 目录遍历上一页数据。'缓存模式'开启且返回值中 pageIndex > 2 时可用。
例子:
`js`
mos.listPre().then(res => {
console.log(res);
});
'缓存模式'未开启时,请根据 nextMarker 调用 .list() 接口。
例子:
请求下一页数据
`js`
mos
.list({ delimiter: "/q/", prefix: "t", startKey: "/q/tt.docx" })
.then(res => {
console.log(res);
});
Object 前缀遍历。 useCache == true 时启用缓存模式。
参数:
- prefix { String } 文件名前缀
- listId { String } 本次遍历唯一 ID, 默认为空。 第一次请求传空, 再次调用须传返回结果中的 listId
例子:
匹配 /1 前缀
``js`
mos.prefix({ prefix: "/1", listId: "" }).then(res => {
// 成功
console.log(res);
});
返回值:
- data { Array } 查出的 Object 列表
- hasMore { Boolean } 列表是否还有下一页
- pageIndex { Number } 列表当前页,开启'缓存模式'返回
- nextMarker { String } 下一页起始文件名
- listId { String } 本次遍历唯一 ID
`json`
{
"data": [
{
"id": "1a1_11.docx",
"key": "/11.docx",
"name": "11.docx",
"length": 29370,
"mediaType": null,
"lastModifyTime": 1547088271387,
"bucket": "aa",
"attrs": {},
"contentEncoding": null
}
],
"hasMore": false,
"nextMarker": null,
"listId": "11c375295d5c4c13ae6380b78624c97d"
}
Object 前缀遍历下一页数据。'缓存模式'开启且返回值 hasMore == true 时可调用,否则会报错。
例子:
`js`
mos.prefixNext().then(res => {
console.log(res);
});
Object 前缀遍历上一页数据。'缓存模式'开启且返回值中 pageIndex > 2 时可用。
例子:
`js`
mos.prefixPre().then(res => {
console.log(res);
});
'缓存模式'未开启时,直接调用 .prefix() 接口。
例子:
请求下一页数据
`js`
mos
.list({ delimiter: "/q/", prefix: "t", startKey: "/q/tt.docx" })
.then(res => {
console.log(res);
});
删除 Object
参数
- key { Object } Object key
例子:
删除 /q/test/test.docx
`js`
mos.delete("/q/test/test.docx").then(res => {
// 成功
console.log(res);
});
返回值:
`json`
{
"key": "/q/test.docx",
"type": "delete",
"message": "success"
}
sdk 返回的都是 Promise ,错误处理安 Promise 来处理就可以
例子:
上传参数错误
`js`
mos
.put({
key: "test.docx",
content: file
})
.then(
res => {
···
},
err => {
console.log(err)
···
}
);
或
`js`
mos
.put({
key: "test.docx",
content: file
})
.then()
.catch(err => {
console.log(err)
···
});
返回值:
`json``
{
"timestamp": 1547453683228,
"status": 500,
"error": "Internal Server Error",
"exception": "java.lang.IllegalArgumentException",
"message": "org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Row length is 0",
"path": "/object/ajax"
}