Store client api

Example

// 脚本库打包依赖规范:
// import * as objectstorage from 'objectstorage' 导入模块不能修改为其他的别名,必须和模块名一致;
// newClient方法里参数不能为变量,否则打包解析不到连接器依赖
import * as objectstorage from 'objectstorage';
import * as buffer from 'buffer';

let minioCli = objectstorage.newClient(objectstorage.StoreType.MINIO, "minio1", "mybucket");
console.log(minioCli.putObject("minio.text", "老的"));
let data = minioCli.getObject("minio.text");
let buf = buffer.fromBytes(data);
console.log(buf.toString());
console.log(minioCli.getEndpoint());

let oldCli = objectstorage.newClient(objectstorage.StoreType.PROXY, "NS__testbucket");
console.log(oldCli.putObject("a.txt", "选择默认的桶"));
data = oldCli.getObject("a.txt");
buf = buffer.fromBytes(data);
console.log(buf.toString());
console.log(oldCli.getEndpoint());

let newCli = objectstorage.newClient(objectstorage.StoreType.PROXY, "NS__testbucket", "cbucket");
console.log(newCli.putObject("a.txt", "不是选择默认的桶"));
data = newCli.getObject("a.txt");
buf = buffer.fromBytes(data);
console.log(buf.toString());
interface Client {
    abortUpload: ((name, uploadID) => void);
    completeUpload: ((name, uploadID, cps, opt?) => void);
    copyObject: ((sourceObject, destObject, opt?) => void);
    createDir: ((dir, withParent) => void);
    createMultipartUpload: ((name, opt) => string);
    deleteDir: ((dir) => void);
    deleteObject: ((name) => void);
    getEndpoint: (() => string);
    getObject: ((name) => any);
    getShareToken: ((name, timeoutInMinutes) => string);
    getUploadToken: ((name) => string);
    hasObject: ((name) => boolean);
    listDir: ((dir, opt?) => ObsObject[]);
    listParts: ((name, uploadId, opt?) => ListPartsOutput);
    lsDir: ((dir, recursive) => ObsObject[]);
    putObject: ((name, data, ...args) => string);
    uploadObject: ((name, data, opt?) => void);
    uploadPart: ((name, uploadID, partNum, data) => CompletedPart);
    viewObject: ((name, opt?) => ViewResult);
}

Properties

abortUpload: ((name, uploadID) => void)

放弃上传

Type declaration

    • (name, uploadID): void
    • Parameters

      • name: string

        对象名

      • uploadID: string

        分段上传id

      Returns void

completeUpload: ((name, uploadID, cps, opt?) => void)

完成分段上传

Type declaration

    • (name, uploadID, cps, opt?): void
    • Parameters

      Returns void

copyObject: ((sourceObject, destObject, opt?) => void)

拷贝对象到新的名字, 注意不能跨桶拷贝

Type declaration

    • (sourceObject, destObject, opt?): void
    • Parameters

      • sourceObject: string

        原始对象全路径,如 a/b/c.txt

      • destObject: string

        目标对象全路径,如 e/f/g.txt

      • Optional opt: CopyOption

        可选参数 CopyOption

      Returns void

createDir: ((dir, withParent) => void)

创建目录

Type declaration

    • (dir, withParent): void
    • Parameters

      • dir: string

        目录名

      • withParent: boolean

        如果为true则创建其所有的父目录, 类似 mkdir -p

      Returns void

createMultipartUpload: ((name, opt) => string)

初始化分段上传

Type declaration

    • (name, opt): string
    • Parameters

      Returns string

deleteDir: ((dir) => void)

删除目录及其下面的所有文件

Type declaration

    • (dir): void
    • Parameters

      • dir: string

        目录名

      Returns void

deleteObject: ((name) => void)

删除对象

Type declaration

    • (name): void
    • Parameters

      • name: string

        对象名

      Returns void

getEndpoint: (() => string)

获取 endpoint

Type declaration

    • (): string
    • Returns string

getObject: ((name) => any)

Type declaration

    • (name): any
    • Parameters

      • name: string

        对象名

      Returns any

Deprecated

优先使用 viewObject

获取对象数据,对象名必须是在桶上的全路径

Returns

object 对象的二进制文件内容

getShareToken: ((name, timeoutInMinutes) => string)

获取分享凭证用于查看对象

Type declaration

    • (name, timeoutInMinutes): string
    • Parameters

      • name: string

        对象名

      • timeoutInMinutes: number

        凭证的有效时长(分钟)

      Returns string

getUploadToken: ((name) => string)

获取上传凭证

Type declaration

    • (name): string
    • Parameters

      • name: string

        对象名

      Returns string

hasObject: ((name) => boolean)

判断对象是否存在,存在则返回 true

Type declaration

    • (name): boolean
    • Parameters

      • name: string

        对象名

      Returns boolean

Example

import * as objectstorage from 'objectstorage';

let cli = objectstorage.newClient(objectstorage.StoreType.OBS, "sound__cnnorth7");
console.log(cli.hasObject("a.txt"));
console.log(cli.hasObject("a-not-exist.txt"));
listDir: ((dir, opt?) => ObsObject[])

列举目录

Type declaration

listParts: ((name, uploadId, opt?) => ListPartsOutput)

列举已上传分段

Type declaration

lsDir: ((dir, recursive) => ObsObject[])

Type declaration

    • (dir, recursive): ObsObject[]
    • Parameters

      • dir: string

        目录

      • recursive: boolean

        是否递归查询所有子目录

      Returns ObsObject[]

Deprecated

优先使用 listDir 列举目录

putObject: ((name, data, ...args) => string)

Type declaration

    • (name, data, ...args): string
    • Parameters

      • name: string

        对象名(全路径)

      • data: any

        对象二进制内容

      • Rest ...args: string[]

        acl

      Returns string

Deprecated

优先使用 uploadObject

上传对象

uploadObject: ((name, data, opt?) => void)

上传对象

Type declaration

    • (name, data, opt?): void
    • Parameters

      • name: string

        对象名(全路径)

      • data: any

        对象二进制内容

      • Optional opt: UploadOption

        上传可选参数 UploadOption

      Returns void

Example

import * as objectstorage from 'objectstorage';

let cli = objectstorage.newClient(objectstorage.StoreType.OBS, "sound__cnnorth7");
// 上传到对象存储系统上完整路径。
let name = "some/test/123.txt"
// 文件完整内容, 可为任何文件二进制
let data = "xxxx"
// 文件内容类型
let contentType = "text/plain"
// 上传
try {
cli.uploadObject(name, data, { acl: "public-read", contentType: contentType });
} catch (e) {
console.log("upload err", e.message);
}
uploadPart: ((name, uploadID, partNum, data) => CompletedPart)

上传分段

Type declaration

    • (name, uploadID, partNum, data): CompletedPart
    • Parameters

      • name: string

        对象名

      • uploadID: string

        上传ID

      • partNum: number

        分段号

      • data: any

        分段数据

      Returns CompletedPart

viewObject: ((name, opt?) => ViewResult)

查看对象内容

Type declaration

Example

import * as objectstorage from 'objectstorage';
import * as buffer from 'buffer';

let cli = objectstorage.newClient(objectstorage.StoreType.OBS, "sound__cnnorth7");
let res = cli.viewObject("a.txt");
console.log(res.contentType);
console.log(res.contentLength);
// 如果是文本类型,可通过 buffer.fromBytes 转换成 string 类型
console.log(buffer.fromBytes(res.data).toString());

// 从下标为1的字节开始读取
console.log(cli.viewObject("a.txt", { range: "bytes=1-" }));