@faceunity/storage
TypeScript icon, indicating that this package has built-in type declarations

5.1.10 • Public • Published

存储层兼容SDK(OSS与minio)

Usage

npm i @faceunity/storage

示例

OSS

import { Storage, StorageType } from 'storage';

const instance = new Storage(StorageType.OSS, {
    region: process.env.OSS_REGION,
	secure: true,
	accessKeyId: process.env.OSS_KEY,
	accessKeySecret: process.env.OSS_SECRET,
	bucket: process.env.OSS_BUCKET,
	internal: false,
});

(async () => {
    const url = await instance.generatePublicObjectUrl("test.json");
    console.log(url);
    console.log(await instance.exist("test.json"));
})()

minio

minio后端的截图功能使用ffmpeg实现,优先使用本地的ffmpeg,其次使用@ffmpeg-installer/ffmpeg包,都不满足时无法使用截图相关接口与配置

import { Storage, StorageType } from 'storage';

const instance = new Storage(StorageType.OSS, {
    endPoint: process.env.MINIO_ENDPOINT,
	port: 9000,
	accessKey: process.env.MINIO_KEY,
	secretKey: process.env.MINIO_SECRET,
	useSSL: false,
	bucket: process.env.MINIO_BUCKET, //为了兼容OSS行为,除minio实例化必须参数外还需要传入bucket参数指定仓库
});

(async () => {
    const url = await instance.generatePublicObjectUrl("test.json");
    console.log(url);
    console.log(await instance.exist("test.json"));
})()

API

自定义实现

/**
 * 某些特定需求可以通过实例的instance属性获得具体实现的SDK实例
 */
const instance = new Storage(StorageType.OSS,oss_options);
instance.instance

不兼容实现

/**
 * 某些实现方式上无法兼容的方法可以通过实例的client属性调用,等同于封装的自定义实现
 */
const instance = new Storage(StorageType.OSS,oss_options);
instance.client

Storage

/**
 * 存储层后端实现支持StorageType.MINIO或StorageType.OSS
 * options传入各自后端需要的参数即可
 * oss参考https://github.com/ali-sdk/ali-oss#ossoptions
 * minio参考https://docs.min.io/docs/javascript-client-api-reference
 * minio实现除必要参数外需要额外传入bucket参数,参照上方minio实例
 */
const instance = new Storage(StorageType.OSS,oss_options);

list

/**
 * 该函数支持按页迭代与正则匹配
 * 该函数OSS实现从性能考虑不进行全量迭代返回总条数
 * 该函数minio实现由于表现一致性问题需要进行全量内存排序,数量级较大时慎用
 * OSS实现需要获取总条数或大量minio对象list时使用自定义接口
 * @param prefix 前缀匹配
 * @param query 正则过滤
 * @param page 页数
 * @param pageNumber 每页数量
 * @returns string[]
 */
instance.list("prefix", new RegExp("t_"), 2, 5);

exist

/**
 * 判断对象是否存在
 * @param path 对象路径
 */
instance.exist("test.json");

delete

/**
 * 删除对象
 * @param path 对象路径
 */
instance.delete("test.json");

put

/**
 * 上传对象
 * @param path 对象路径
 * @param input 读文件流/Buffer
 */
instance.put("test.json",createReadStream("./out.json"));
instance.put("test.json",readFileSync("./out.json"));

get

/**
 * 获取对象Buffer
 * @param path 对象路径
 */
instance.get("test.json");

deleteMulti

/**
 * 一次删除多个对象
 * @param paths 对象路径
 */
instance.deleteMulti(["test.json","test1.json"]);

copy

instance.copy("test.json","test1.json");

getInfo

该接口用于获得对象信息(包括metadata信息),由于ts的编译器目前尚无法支持泛型类实例方法上的泛型推断,默认返回泛型为OSS返回类型,MINIO作为存储后端时可以使用instance.getInfo<StorageType.MINIO>("test.json");来更正返回类型

instance.getInfo("test.json");
instance.getInfo<StorageType.OSS>("test.json");
instance.getInfo<StorageType.MINIO>("test.json");

不兼容实现(OSS):

generatePublicObjectUrl

/**
 * 获取对象公网URL
 * @param path 对象路径
 */
instance.client.generatePublicObjectUrl("test.json");

generatePrivateObjectUrl

/**
 * 获取对象内网URL
 * @param path 对象路径
 */
instance.client.generatePrivateObjectUrl("test.json");

generatePublicSnapShotUrl

/**
 * 获取对象内网截图URL
 * @param path 对象路径
 */
instance.client.generatePublicSnapShotUrl("test.mp4", SnapShotObjectType.VIDEO);
instance.client.generatePublicSnapShotUrl("test.jpg", SnapShotObjectType.IMAGE);
instance.client.generatePublicSnapShotUrl("test.mp4", SnapShotObjectType.VIDEO, { expire: 3600, width: 300, height: -1 });
instance.client.generatePublicSnapShotUrl("test.jpg", SnapShotObjectType.IMAGE, { expire: 3600, width: 300, height: -1 });

generatePrivateSnapShotUrl

/**
 * 获取对象内网截图URL
 * @param path 对象路径
 */
instance.client.generatePrivateSnapShotUrl("test.mp4", SnapShotObjectType.VIDEO);
instance.client.generatePrivateSnapShotUrl("test.jpg", SnapShotObjectType.IMAGE);
instance.client.generatePrivateSnapShotUrl("test.mp4", SnapShotObjectType.VIDEO, { expire: 3600, width: 300, height: -1 });
instance.client.generatePrivateSnapShotUrl("test.jpg", SnapShotObjectType.IMAGE, { expire: 3600, width: 300, height: -1 });

不兼容实现(Minio):

generateObjectUrl

/**
 * 获取对象URL
 * @param path 对象路径
 */
instance.client.generateObjectUrl("test.json");

generateSnapShotImage

该方法使用FFMPEG生成首帧图(视频/图片),并且支持缩放

/**
 * 获取视频首帧图/对图片进行resize
 * @param path 对象路径
 */
let buf = instance.client.generateSnapShotImage("test.mp4", SnapShotObjectType.VIDEO, { width: -1, height: 300 });
buf = instance.client.generateSnapShotImage("test.png", SnapShotObjectType.IMAGE, { width: -1, height: 300 });
buf = instance.client.generateSnapShotImage("test.mp4", SnapShotObjectType.VIDEO, { width: -1, height: 300 });
buf = instance.client.generateSnapShotImage("test.png", SnapShotObjectType.IMAGE, { width: -1, height: 300 });
buf = instance.client.generateSnapShotImage("test.mp4", SnapShotObjectType.VIDEO, { width: -1, height: 300 });
buf = instance.client.generateSnapShotImage("test.png", SnapShotObjectType.IMAGE, { width: -1, height: 300 });

Readme

Keywords

none

Package Sidebar

Install

npm i @faceunity/storage

Weekly Downloads

0

Version

5.1.10

License

ISC

Unpacked Size

84.1 kB

Total Files

20

Last publish

Collaborators

  • faceunity