@mlog/mossdk

1.1.1 • Public • Published

MOS javascript SDK 简介

Gavial Mos JavaScript SDK for the browser and Node.js

安装

npm install @mlog/mossdk
// yarn add @mlog/mossdk

兼容性

Node

Node.js >= 8.0.0 required.

Browser

  • IE >= 10 & Edge
  • Major versions of Chrome/Firefox/Safari
  • Major versions of Android/iOS/WP

Node Usage

const Mossdk = require("@mlog/mossdk").default;
// 参数为 Accesskey
let mos = new Mossdk({
  baseUrl: "http://mlogcn.com",
  token: "80dfba73e4d7d412e9cd223f0a52b3791"
});

Browser Usage

import Mossdk from "@mlog/mossdk";
// 参数为 Accesskey
const mos = new Mossdk({
  baseUrl: "http://mlogcn.com",
  token: "80dfba73e4d7d412e9cd223f0a52b3791"
});

Basic usage

<script src="./mossdk.min.js"></script>

<script>
  const mos = new MOS.Mossdk({
    baseUrl: "http://mlogcn.com",
    token: "80dfba73e4d7d412e9cd223f0a52b3791"
  });
  mos.getBucketInfo("aa").then(res => {
    console.log(res);
  });
</script>

Mossdk(option)

创建一个 sdk 实例

参数:

  • option { Object } bucket 配置

    • token { String } accesskey
    • baseUrl { String } 服务端地址
    • useCache { Boolean } 非必填 查询 list prefix 时是否使用缓存,默认 fasle。 例子:
const mos = new Mossdk({
  baseUrl: "http://mlogcn.com",
  token: "80dfba73e4d7d412e9cd223f0a52b3791",
  useCache: true
});

Bucket Operations

.useBucket(name)

使用这个 bucket

参数:

  • name { String } bucket 名称

例子:

  • 使用 aa bucket.
mos.useBucket("aa");

返回值:

this mos 实例

.getBucketInfo(name)

参看这个 bucket 的信息

参数:

  • name { String } bucket 名称

例子:

  • 参看 aa 的信息
mos.getBucketInfo("aa").then(
  res => {
    // 查询成功
    // 在不知道 bucke 的情况下,可以通过该接口确认 bucket是否存在。
    mos.useBucket("aa");
  },
  err => {
    // 失败
    console.log(err);
  }
);

返回值:

{
  "createTime": "2018-11-05 08:34:12",
  "id": "7e4932c355e947291ae1316d856e4",
  "name": "aa",
  "ownerId": 67,
  "ownerType": "GROUP",
  "protectType": "public"
}

Object Operations

.getObjectInfo(key)

查询 object 信息

参数:

  • key { String } object key

例子: 获取 aa buckey 下 /q/ 信息

// 以下例子默认使用 `aa` bucket
// mos.useBucket("aa").getObjectInfo("/q/")
mos.useBucket("aa");
mos.getObjectInfo("/q").then(res => {
  console.log(res);
});

.put(option)

文件上传

参数:

  • option { Object } 上传参数
    • key { String } 上传后文件的 key
    • content { File } 要上传的文件
    • mediaType { String } 非必填 例如’image/jpeg’
    • uploadProgress { Function } 非必填 上传进度事件

例子:

上传一个 test.docx

// 上传过程中触发,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);
  });

返回值:

{
  "key": "/q/tt.docx",
  "type": "upload",
  "message": "success"
}

.get(key)

下载文件

参数:

  • key { String } Object key

例子:

mos.get("/q/test.docx").then(res => {
  console.log(res.message);
});

返回值:

{
  "key": "/q/test.docx",
  "type": "download",
  "message": "success"
}

.list(option)

Object 目录遍历, ``useCache == true` 时启用缓存模式。

参数:

  • option { Object } 遍历参数
    • delimiter { String } 目录对象 key
    • prefix { String } 非必填 匹配前缀
    • startKey { String } 非必填 该目录下最小文件名(包含)
    • endKey { String } 非必填 该目录下最大文件名(包含)
    • pageSize { Number } 非必填 单页数据最大条数

例子:

mos.list({ delimiter: "/q/", prefix: "t" }).then(res => {
  console.log(res);
});

返回值:

  • data { Array } 查出的 Object 列表
  • hasMore { Boolean } 列表是否还有下一页
  • pageIndex { Number } 列表当前页,开启'缓存模式'返回
  • nextMarker { String } 下一页起始文件名
{
  "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
}

.listNext()

Object 目录遍历下一页数据。'缓存模式'开启且返回值 hasMore == true 时可调用,否则会报错。

例子:

mos.listNext().then(res => {
  console.log(res);
});

.listPre()

Object 目录遍历上一页数据。'缓存模式'开启且返回值中 pageIndex > 2 时可用。

例子:

mos.listPre().then(res => {
  console.log(res);
});

'缓存模式'未开启时,请根据 nextMarker 调用 .list() 接口。

例子:

请求下一页数据

mos
  .list({ delimiter: "/q/", prefix: "t", startKey: "/q/tt.docx" })
  .then(res => {
    console.log(res);
  });

.prefix(option)

Object 前缀遍历。 ``useCache == true` 时启用缓存模式。 参数:

  • prefix { String } 文件名前缀
  • listId { String } 本次遍历唯一 ID, 默认为空。 第一次请求传空, 再次调用须传返回结果中的 listId

例子: 匹配 /1 前缀

mos.prefix({ prefix: "/1", listId: "" }).then(res => {
  // 成功
  console.log(res);
});

返回值:

  • data { Array } 查出的 Object 列表
  • hasMore { Boolean } 列表是否还有下一页
  • pageIndex { Number } 列表当前页,开启'缓存模式'返回
  • nextMarker { String } 下一页起始文件名
  • listId { String } 本次遍历唯一 ID
{
  "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"
}

.prefixNext()

Object 前缀遍历下一页数据。'缓存模式'开启且返回值 hasMore == true 时可调用,否则会报错。

例子:

mos.prefixNext().then(res => {
  console.log(res);
});

.prefixPre()

Object 前缀遍历上一页数据。'缓存模式'开启且返回值中 pageIndex > 2 时可用。

例子:

mos.prefixPre().then(res => {
  console.log(res);
});

'缓存模式'未开启时,直接调用 .prefix() 接口。

例子:

请求下一页数据

mos
  .list({ delimiter: "/q/", prefix: "t", startKey: "/q/tt.docx" })
  .then(res => {
    console.log(res);
  });

.delete(key)

删除 Object

参数

  • key { Object } Object key

例子: 删除 /q/test/test.docx

mos.delete("/q/test/test.docx").then(res => {
  // 成功
  console.log(res);
});

返回值:

{
  "key": "/q/test.docx",
  "type": "delete",
  "message": "success"
}

错误处理

sdk 返回的都是 Promise ,错误处理安 Promise 来处理就可以 例子: 上传参数错误

mos
  .put({
    key: "test.docx",
    content: file
  })
  .then(
    res => {
      ···
    },
    err => {
      console.log(err)
      ···
    }
  );

mos
  .put({
    key: "test.docx",
    content: file
  })
  .then()
  .catch(err => {
    console.log(err)
    ···
  });

返回值:

{
  "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"
}

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 1.1.1
    0
    • latest

Version History

Package Sidebar

Install

npm i @mlog/mossdk

Weekly Downloads

0

Version

1.1.1

License

ISC

Unpacked Size

56.9 kB

Total Files

7

Last publish

Collaborators

  • galimu