UNPKG

@mw-components/ali-oss

Version:

阿里云 OSS 命令行工具 ossutil 封装 midway.js 组件,提供 TypeScript 类型定义

413 lines 13.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AliOssComponent = void 0; /* eslint-disable @typescript-eslint/no-unnecessary-condition */ /* eslint-disable max-lines-per-function */ const jaeger_1 = require("@mw-components/jaeger"); const shared_core_1 = require("@waiting/shared-core"); const ali_oss_1 = require("@yuntools/ali-oss"); // eslint-disable-next-line import/no-extraneous-dependencies const opentracing_1 = require("opentracing"); const types_1 = require("./types"); /** 阿里云 OSS ossutils 命令行封装组件 */ class AliOssComponent { config; ctx; client; querySpanMap = new WeakMap(); constructor(config) { this.config = config; const opts = { accessKeyId: config.accessKeyId, accessKeySecret: config.accessKeySecret, endpoint: config.endpoint, }; if (config.stsToken) { opts.stsToken = config.stsToken; } const client = new ali_oss_1.OssClient(opts, config.cmd); client.debug = !!config.debug; this.client = client; } /** * 拷贝文件, * - 拷贝本地文件/目录到远程建议使用 `upload()` 或者 `syncRemote()` 方法 * - 拷贝远程文件/目录到本地建议使用 `download()` 或者 `syncLocal()` 方法 * * 若 force 为空或者 false,且目标文件存在时会卡在命令行提示输入阶段(无显示)最后导致超时异常 * @link https://help.aliyun.com/document_detail/120057.html */ async cp( /** 本地文件、目录或者远程 OSS 对象 */ src, /** OSS 对象,不包括 bucket */ target, options) { const opts = { fnKey: types_1.FnKey.cp, options, target, src, }; const ret = await this.runner(opts); return ret; } /** * 上传本地文件到 OSS * 若 force 为空或 false,且目标文件存在时会卡在命令行提示输入阶段(无显示)最后导致超时异常 * @link https://help.aliyun.com/document_detail/120057.html */ async upload( /** 本地目录或文件 */ src, /** OSS 对象,不包括 bucket */ target, options) { const opts = { fnKey: types_1.FnKey.upload, options, target, src, }; const ret = await this.runner(opts); return ret; } /** * 下载远程文件到本地 * 若 force 为空或 false,且目标文件存在时会卡在命令行提示输入阶段(无显示)最后导致超时异常 * @link https://help.aliyun.com/document_detail/120057.html */ async download( /** OSS 对象,不包括 bucket */ src, /** 本地目录或文件 */ target, options) { const opts = { fnKey: types_1.FnKey.download, options, target, src, }; const ret = await this.runner(opts); return ret; } /** * 创建软链接 * @link https://help.aliyun.com/document_detail/120059.html */ async createSymlink( /** OSS 对象,不包括 bucket */ src, /** OSS 软连接对象,不包括 bucket */ target, options) { const opts = { fnKey: types_1.FnKey.link, options, target, src, }; const ret = await this.runner(opts); return ret; } /** * 创建目录 * @link https://help.aliyun.com/document_detail/120062.html */ async mkdir( /** OSS 对象,不包括 bucket */ target, options) { const opts = { fnKey: types_1.FnKey.mkdir, options, target, src: void 0, }; const ret = await this.runner(opts); return ret; } /** * 移动云端的 OSS 对象 * 流程为先 `cp()` 然后 `rm()` */ async mv( /** OSS 源对象,不包括 bucket */ src, /** OSS 目的对象,不包括 bucket */ target, options) { const opts = { fnKey: types_1.FnKey.mv, options, target, src, }; const ret = await this.runner(opts); return ret; } /** * OSS 远程路径是否存在 */ async pathExists( /** OSS 对象,不包括 bucket */ target, options) { const opts = { fnKey: types_1.FnKey.pathExists, options, target, src: void 0, }; const ret = await this.runner(opts); return ret; } /** * 删除云对象,不支持删除 bucket 本身 * 如果在 recusive 为 false 时删除目录,则目录参数值必须以 '/' 结尾,否则不会删除成功 * @link https://help.aliyun.com/document_detail/120053.html */ async rm( /** OSS 对象,不包括 bucket */ target, options) { const opts = { fnKey: types_1.FnKey.rm, options, target, src: void 0, }; const ret = await this.runner(opts); return ret; } /** * 递归删除,相当于 `rm -rf` * @link https://help.aliyun.com/document_detail/120053.html */ async rmrf( /** OSS 对象,不包括 bucket */ target, options) { const opts = { fnKey: types_1.FnKey.rmrf, options, target, src: void 0, }; const ret = await this.runner(opts); return ret; } /** * sign(生成签名URL) * @link https://help.aliyun.com/document_detail/120064.html */ async sign( /** OSS 对象,不包括 bucket */ src, options) { const opts = { fnKey: types_1.FnKey.sign, options, target: void 0, src, }; const ret = await this.runner(opts); return ret; } /** * 查看 Bucket 和 Object 信息 * @link https://help.aliyun.com/document_detail/120054.html */ async stat( /** OSS 对象,不包括 bucket */ target, options) { const opts = { fnKey: types_1.FnKey.stat, options, target, src: void 0, }; const ret = await this.runner(opts); return ret; } /** * 同步 OSS 文件到本地 * - force 参数默认 true * - 若 force 为 false,且目标文件存在时会卡在命令行提示输入阶段(无显示)最后导致超时异常 * @link https://help.aliyun.com/document_detail/256352.html */ async syncLocal( /** OSS 对象,不包括 bucket */ src, /** 本地目录 */ target, options) { const opts = { fnKey: types_1.FnKey.syncLocal, options, target, src, }; const ret = await this.runner(opts); return ret; } /** * 同步本地文件到 OSS * - force 参数默认 true * - 若 force 为 false,且目标文件存在时会卡在命令行提示输入阶段(无显示)最后导致超时异常 * @link https://help.aliyun.com/document_detail/193394.html */ async syncRemote( /** 本地目录 */ src, /** OSS 对象,不包括 bucket */ target, options) { const opts = { fnKey: types_1.FnKey.syncRemote, options, target, src, }; const ret = await this.runner(opts); return ret; } async runner(options) { const { fnKey } = options; const opts = this.genOptions(options); const id = { time: Symbol(Date.now()) }; try { await this.tracer('start', id, opts); // @ts-ignore const ret = await this.client[fnKey](opts); await this.tracer('finish', id, opts); return await ret; } catch (ex) { await this.tracer('error', id, opts, ex); throw ex; } } genOptions(input) { const ret = { ...this.config, src: input.src, target: input.target, ...input.options, }; return ret; } async tracer(type, id, options, err) { if (!options.enableTracing) { return; } if (!this.ctx) { return; } const end = Date.now(); const logger = await this.ctx.requestContext.getAsync(jaeger_1.Logger); const trm = await this.ctx.requestContext.getAsync(jaeger_1.TracerManager); if (!logger || !trm) { return; } const tmp = options; const opts = { acl: tmp.acl ?? '', src: tmp.src, payer: tmp.payer ?? '', recursive: tmp.recursive ?? false, sampleThrottleMs: tmp.sampleThrottleMs ?? 10000, target: tmp.target, }; switch (type) { case 'start': { const currSpan = trm.currentSpan(); if (!currSpan) { logger.warn('Get current SPAN undefined.'); console.warn('Get current SPAN undefined.'); return; } const span = trm.genSpan(types_1.ConfigKey.componentName, currSpan); const spanInfo = { span, timestamp: Date.now(), }; this.querySpanMap.set(id, spanInfo); span.addTags({ [jaeger_1.TracerLog.queryCostThottleInMS]: opts.sampleThrottleMs, qid: id.time.toString(), acl: opts.acl, payer: opts.payer, recursive: opts.recursive, src: opts.src, target: opts.target, }); const input = { event: jaeger_1.TracerLog.queryStart, time: (0, shared_core_1.genISO8601String)(), }; span.log(input); trm.spanLog(input); break; } case 'finish': { const spanInfo = this.querySpanMap.get(id); if (!spanInfo) { console.warn('Retrieve spanInfo undefined.', opts); return; } const { span } = spanInfo; const start = spanInfo.timestamp; const cost = end - start; const tags = { [jaeger_1.TracerLog.queryCost]: cost, }; span.addTags(tags); const input = { event: jaeger_1.TracerLog.queryFinish, time: (0, shared_core_1.genISO8601String)(), [jaeger_1.TracerLog.queryCost]: cost, }; if (typeof opts.sampleThrottleMs === 'number' && opts.sampleThrottleMs > 0 && cost > opts.sampleThrottleMs) { const tags2 = { [opentracing_1.Tags.SAMPLING_PRIORITY]: 50, [jaeger_1.TracerTag.logLevel]: 'warn', }; span.addTags(tags2); input['level'] = 'warn'; logger?.log(input, span); } else { span.log(input); } trm.spanLog(input); span.finish(); break; } case 'error': { const spanInfo = this.querySpanMap.get(id); if (!spanInfo) { console.warn('Retrieve spanInfo undefined.', opts); return; } const { span } = spanInfo; const start = spanInfo.timestamp; const cost = end - start; const tags = { [jaeger_1.TracerLog.queryCost]: cost, }; span.addTags(tags); const input = { event: jaeger_1.TracerLog.queryError, level: 'error', time: (0, shared_core_1.genISO8601String)(), [jaeger_1.TracerLog.queryCost]: cost, [jaeger_1.TracerLog.error]: err, }; logger?.log(input, span); trm.spanLog(input); span.addTags({ [opentracing_1.Tags.ERROR]: true, [opentracing_1.Tags.SAMPLING_PRIORITY]: 100, [jaeger_1.TracerTag.logLevel]: 'error', [jaeger_1.TracerLog.error]: err, }); span.finish(); break; } } return; } } exports.AliOssComponent = AliOssComponent; //# sourceMappingURL=component.js.map