UNPKG

@yuntools/ali-oss

Version:

阿里云 OSS 命令行工具 ossutil 封装,支持 ESM,CJS 导入,提供 TypeScript 类型定义

613 lines (594 loc) 22 kB
import { OutputRow } from 'rxrunscript'; type ConfigPath = string; declare enum PlaceholderKey { src = "__src__", dest = "__dest__", target = "__target__", bucket = "bucket", bucketName = "bucketname", /** * 对于远程目录进行编码,并且添加 `oss://` 前缀 * 不适用于本地目录 */ encodeSource = "encodeSource", /** * 对于远程目录进行编码,并且添加 `oss://` 前缀 */ encodeTarget = "encodeTarget" } declare enum ACLKey { /** 继承Bucket的读写权限 */ default = "default", /** 有该Bucket的拥有者可以对该Bucket内的文件进行读写操作,其他人无法访问该Bucket内的文件 */ private = "private", /** * 只有Bucket拥有者可以对该Bucket内的文件进行写操作,其他用户(包括匿名访问者)都可以对该Bucket中的文件进行读操作。 * 这有可能造成您数据的外泄以及费用激增,如果被人恶意写入违法信息还可能会侵害您的合法权益。 * 除特殊场景外,不建议您配置此权限 */ publicRead = "public-read", /** * 任何人(包括匿名访问者)都可以对该Bucket内文件进行读写操作。 * 这有可能造成您数据的外泄以及费用激增, * \*\*请谨慎操作\*\* */ publicReadWrite = "public-read-write" } interface ProcessResp { /** * 0: success, others: error */ readonly exitCode: number; readonly exitSignal: string; readonly stdout: string; readonly stderr: string; } declare enum DataKey { elapsed = "elapsed", averageSpeed = "averageSpeed", acl = "ACL", acceptRanges = "Accept-Ranges", contentLength = "Content-Length", contentMd5 = "Content-Md5", contentType = "Content-Type", etag = "Etag", lastModified = "Last-Modified", owner = "Owner", xOssHashCrc64ecma = "X-Oss-Hash-Crc64ecma", xOssObjectType = "X-Oss-Object-Type", xOssStorageClass = "X-Oss-Storage-Class", link = "link", httpUrl = "httpUrl", httpShareUrl = "httpShareUrl", succeedTotalNumber = "succeedTotalNumber", succeedTotalSize = "succeedTotalSize", uploadDirs = "uploadDirs", uploadFiles = "uploadFiles", /** sync between cloud */ copyObjects = "copyObjects", downloadObjects = "downloadObjects" } type PickFunc = (input: string, rule: RegExp, debug: boolean) => string | number | undefined; declare enum FnKey { cp = "cp", download = "download", link = "createSymlink", mkdir = "mkdir", mv = "mv", pathExists = "pathExists", probeUpload = "probeUpload", rm = "rm", rmrf = "rmrf", sign = "sign", stat = "stat", syncCloud = "syncCloud", syncLocal = "syncLocal", syncRemote = "syncRemote", upload = "upload" } declare enum CmdKey { cp = "cp", download = "cp", link = "create-symlink", createSymlink = "create-symlink", mkdir = "mkdir", mv = "mv", probeUpload = "probe", rm = "rm", rmrf = "rm", sign = "sign", stat = "stat", syncCloud = "sync", syncLocal = "sync", syncRemote = "sync", upload = "cp" } /** 扁担参数名映射 */ declare enum MKey { accessKeyId = "access-key-id", accessKeySecret = "access-key-secret", stsToken = "sts-token", /** 设置分片大小,单位为字节 */ partSize = "part-size", /** 文件名称的编码方式。取值为url。如果不指定该选项,则表示文件名称未经过编码 */ encodingType = "encoding-type", /** 上传链接子目录,默认不上传 */ enableSymlinkDir = "enable-symlink-dir", /** 批量操作时不忽略错误 */ disableIgnoreError = "disable-ignore-error", /** 仅上传当前目录下的文件,忽略子目录及子目录下的文件 */ onlyCurrentDir = "only-current-dir", /** 设置断点续传文件的大小阈值,单位为字节 */ bigfileThreshold = "bigfile-threshold", /** 指定断点续传记录信息所在的目录 */ checkpointDir = "checkpoint-dir", /** 指定保存上传文件时的快照信息所在的目录。在下一次上传文件时,ossutil会读取指定目录下的快照信息进行增量上传 */ snapshotPath = "snapshot-path", /** 表示上传文件时不为目录生成Object */ disableCrc64 = "disable-crc64", /** Object 的指定版本。仅适用于已开启或暂停版本控制状态 Bucket下的 Object */ versionId = "version-id", /** * Object 的所有版本。 * 仅适用于已开启或暂停版本控制状态 Bucket 下的 Object, * 且同一个删除示例中仅允许选择--version-id或--all-versions其中一个选项 */ allVersions = "all-versions", /** 客户端读超时的时间,单位为秒,默认值为1200 */ readTimeoutSec = "read-timeout", /** 客户端连接超时的时间,单位为秒,默认值为120 */ connectTimeoutSec = "connect-timeout", /** 超时秒 */ timeoutSec = "timeout", /** 参数名typo */ trafficLimit = "trafic-limit", disableEncodeSlash = "disable-encode-slash" } interface Config { accessKeyId?: string | undefined; accessKeySecret?: string | undefined; stsToken?: string | undefined; endpoint?: string | undefined; } declare enum Msg { accessDenied = "AccessDenied", cloudFileAlreadyExists = "Cloud file already exists", cloudConfigFileNotExists = "Cloud config file not exists", noSuchBucket = "NoSuchBucket" } type DownLinks = { [key in NodeJS.Platform]?: string | undefined; }; /** * ossutil 的通用选项,可以在大部分命令中使用 * @link https://help.aliyun.com/document_detail/50455.htm */ interface BaseOptions extends Config { /** * 是否对 `oss://bucket_name` 后面的key(目录名称)进行编码。 * - 若输入参数已经被编码(比如从操作结果中获取),则需要显示设置为 false * - 若为本地目录,则不能设置为 true * @default false */ [PlaceholderKey.encodeSource]?: boolean | undefined; /** * 是否对 `oss://bucket_name` 后面的key(目录名称)进行编码。 * 若输入参数已经被编码(比如从操作结果中获取),则需要显示设置为 false * @default true */ [PlaceholderKey.encodeTarget]?: boolean | undefined; /** `oss://bucket_name` */ [PlaceholderKey.bucket]?: string | undefined; /** * 客户端连接超时的时间,单位为秒, * @default 120 */ connectTimeoutSec?: number | undefined; /** * 客户端读超时的时间,单位为秒, * @default 1200 */ readTimeoutSec?: number | undefined; /** 在当前工作目录下输出ossutil日志文件ossutil.log。该选项默认为空,表示不输出日志文件 */ loglevel?: 'info' | 'debug' | undefined; } interface DataBase { /** * @example 0.303190 */ [DataKey.elapsed]: string | undefined; } interface ProcessRet<T extends DataBase = DataBase> { readonly data: T | undefined; readonly stdout: string; readonly stderr: string; /** * 0: success, others: error */ readonly exitCode: number; readonly exitSignal: string; } type ParamMap = Map<string, string | number | boolean>; type ProcessInputFn = (input: any | undefined, globalConfig: Config | undefined) => Promise<ParamMap>; interface MvOptions extends BaseOptions { /** cloudurl 源路径,不包括 bucket */ src: string; /** cloudurl 目的路径,不包括 bucket */ target: string; /** 强制操作,不进行询问提示 */ force?: boolean | undefined; /** 递归操作。 * 当指定该选项时,ossutil 会对 Bucket下所有符合条件的 Object 进行操作, * 否则只对指定的单个 Object 进行操作 */ recursive?: boolean | undefined; } /** * @link https://help.aliyun.com/document_detail/120057.html */ interface CpOptions extends MvOptions { /** 目的 cloudurl 路径,不包括 bucket */ target: string; /** 源路径,可以是本地文件或 cloudurl */ src: string; /** * 设置断点续传文件的大小阈值,单位为字节 * @default 100 * 1024 * 1024 (100MB) */ bigfileThreshold?: number | undefined; /** 指定断点续传记录信息所在的目录 */ checkpointDir?: string | undefined; /** * 上传链接子目录,默认不上传 * @default false * */ enableSymlinkDir?: boolean | undefined; /** * 表示上传文件时不为目录生成Object * @default false */ disableCrc64?: boolean | undefined; /** 批量操作时不忽略错误 */ disableIgnoreError?: boolean | undefined; /** 仅上传当前目录下的文件,忽略子目录及子目录下的文件 */ onlyCurrentDir?: boolean | undefined; /** 设置分片大小,单位为字节 */ partSize?: number | undefined; /** 指定保存上传文件时的快照信息所在的目录。在下一次上传文件时,ossutil会读取指定目录下的快照信息进行增量上传 */ snapshotPath?: string | undefined; /** * @default ACLKey.default */ acl?: ACLKey | undefined; /** 不包含任何符合指定条件的文件 */ exclude?: string | undefined; /** 包含符合指定条件的所有文件 */ include?: string | undefined; /** * 多文件操作时的并发任务数 * @default 3 */ jobs?: number | undefined; /** * 最大上传速度,单位为 KB/s * @default 0 不限制上传速度 */ maxupspeed?: number | undefined; /** 文件的元信息。包括部分HTTP标准属性(HTTP Header)以及以x-oss-meta-开头的用户自定义元数据 */ meta?: string | undefined; /** 请求的支付方式。如果希望访问指定路径下的资源产生的流量、请求次数等费用由请求者支付,请将此选项的值设置为requester */ payer?: string | undefined; /** 单文件操作时的并发任务数,取值范围为 1~10000 */ parallel?: number | undefined; /** * 上传文件时设置标签信息,格式为 `TagkeyA=TagvalueA&TagkeyB=TagvalueB....` */ tagging?: string | undefined; /** 只有当目标文件不存在,或源文件的最后修改时间晚于目标文件时,才会执行上传操作 */ update?: boolean | undefined; } interface DataCp extends DataBase { /** byte/s */ [DataKey.averageSpeed]: number | undefined; [DataKey.succeedTotalNumber]: number | undefined; [DataKey.succeedTotalSize]: string | undefined; [DataKey.uploadDirs]: number; [DataKey.uploadFiles]: number; /** sync between cloud */ [DataKey.copyObjects]: number; } /** * @link https://help.aliyun.com/document_detail/120057.html */ interface DownloadOptions extends CpOptions { /** 目的文件,必须是本地文件 */ target: string; /** cloudurl 路径,不包括 bucket */ src: string; } interface DataDownload extends DataBase { /** byte/s */ [DataKey.averageSpeed]: number | undefined; [DataKey.succeedTotalNumber]: number | undefined; [DataKey.succeedTotalSize]: string | undefined; [DataKey.downloadObjects]: number; } declare const initBaseOptions: BaseOptions; declare const initOptions: BaseOptions & { target: string; }; /** * src will be delete */ declare function processInputWoSrc<T extends BaseOptions & { target: string; }>(input: T, initOptionsInput: T, globalConfig: Config | undefined): Promise<ParamMap>; /** * @link https://help.aliyun.com/document_detail/120057.html */ interface LinkOptions extends BaseOptions { /** cloudurl 源路径,不包括 bucket */ src: string; /** cloudurl 目的路径,不包括 bucket */ target: string; } /** * @link https://help.aliyun.com/document_detail/120057.html */ interface MkdirOptions extends BaseOptions { /** cloudurl 路径,不包括 bucket */ target: string; } type ProbUpOptions = BaseOptions; /** * @link https://help.aliyun.com/document_detail/120053.html */ interface RmOptions extends Omit<MvOptions, 'force' | 'src'> { /** cloudurl 目的路径,不包括 bucket */ target: string; /** * Object 的所有版本。 * 仅适用于已开启或暂停版本控制状态 Bucket 下的 Object, * 且同一个删除示例中仅允许选择--version-id或--all-versions其中一个选项 */ allVersions?: boolean | undefined; /** Object 的指定版本。仅适用于已开启或暂停版本控制状态 Bucket下的 Object */ versionId?: string | undefined; /** 仅在删除 Bucket 时使用此选项 */ /** 不包含任何符合指定条件的 Object */ exclude?: string | undefined; /** 包含符合指定条件的所有 Object */ include?: string | undefined; /** 指定操作的对象为 Bucket 中未完成的 Multipart 事件 */ multipart?: boolean | undefined; } /** * @link https://help.aliyun.com/document_detail/120053.html */ interface RmrfOptions extends Omit<RmOptions, 'recursive'> { /** cloudurl 目的路径,不包括 bucket */ target: string; } interface SignOptions extends BaseOptions { /** cloudurl 路径,不包括 bucket */ src: string; /** * 不对cloud_url中携带的正斜线(/)进行编码 * @default false */ disableEncodeSlash?: boolean | undefined; /** * 签名 URL 过期时间,单位秒 * @default 60 */ timeoutSec?: number | undefined; /** * 限定HTTP的访问速度,单位为bit/s。缺省值为0,表示不受限制。 * 取值范围为 245760~838860800 */ trafficLimit?: number | undefined; /** Object 的指定版本ID。仅适用于已开启或暂停版本控制状态 Bucket下的 Object */ versionId?: string | undefined; } interface DataSign extends DataBase { /** 不带有 token 认证信息的文件路径 */ [DataKey.link]: string | undefined; /** 不带有 token 认证信息的链接 */ [DataKey.httpUrl]: string | undefined; /** 带有 token 认证信息的链接 */ [DataKey.httpShareUrl]: string | undefined; } interface StatOptions extends BaseOptions { /** cloudurl 路径,不包括 bucket */ target: string; versionId?: string | undefined; payer?: string | undefined; } interface DataStat extends DataBase { [DataKey.acl]: 'default' | 'public' | 'private'; [DataKey.acceptRanges]: string | undefined; [DataKey.contentLength]: number | undefined; [DataKey.contentMd5]: string | undefined; [DataKey.contentType]: string | undefined; [DataKey.etag]: string | undefined; [DataKey.lastModified]: string | undefined; [DataKey.owner]: string | undefined; [DataKey.xOssHashCrc64ecma]: string | undefined; [DataKey.xOssObjectType]: string | undefined; [DataKey.xOssStorageClass]: string | undefined; } /** * @link https://help.aliyun.com/document_detail/120057.html */ interface UploadOptions extends CpOptions { /** 目的 cloudurl 路径,不包括 bucket */ target: string; /** 源路径,必须是本地文件 */ src: string; } /** * @link https://help.aliyun.com/document_detail/193394.html */ interface SyncOptions extends Omit<UploadOptions, 'recursive'> { /** 目的 cloudurl 路径,不包括 bucket */ target: string; /** 源路径,可以是本地文件或 cloudurl */ src: string; /** * 删除目的端指定路径下的其他文件,仅保留本次同步的文件, * **警告: 建议启用本选项前开启版本控制,防止数据误删除** * * @default false */ delete?: boolean; } /** * @link https://help.aliyun.com/document_detail/256354.html */ interface SyncCloudOptions extends SyncOptions { /** 目的 cloudurl 路径,不包括 bucket */ target: string; /** 源 cloudurl 路径,不包括 bucket */ src: string; } /** * @link https://help.aliyun.com/document_detail/256352.html */ interface SyncLocalOptions extends SyncOptions { /** 目的本地路径 */ target: string; /** 源 cloudurl 路径,不包括 bucket */ src: string; } /** * @link https://help.aliyun.com/document_detail/193394.html */ interface SyncRemoteOptions extends SyncOptions { /** 源路径,本地目录 */ src: string; } /** * 阿里云 OSS 服务接口, * 基于命令行工具 ossutil 封装 */ declare class OssClient { /** * 配置参数或者配置文件路径 * @default ~/.ossutilconfig */ protected readonly configInput?: string | Config | undefined; cmd: string; debug: boolean; configPath: string; private readonly config; constructor( /** * 配置参数或者配置文件路径 * @default ~/.ossutilconfig */ configInput?: string | Config | undefined, cmd?: string); /** * 删除 OSS 配置文件 */ destroy(): Promise<void>; /** * 在远程之间拷贝文件。 * 若 force 为空或者 false,且目标文件存在时会卡在命令行提示输入阶段(无显示)最后导致超时异常 * @note 下载文件使用 `download()` * @link https://help.aliyun.com/document_detail/120057.html */ cp(options: CpOptions): Promise<ProcessRet<DataCp>>; /** * 创建软链接 * @link https://help.aliyun.com/document_detail/120059.html */ createSymlink(options: LinkOptions): Promise<ProcessRet>; /** * 下载远程文件到本地 * 若 force 为空或者 false,且目标文件存在时会卡在命令行提示输入阶段(无显示)最后导致超时异常 * @link https://help.aliyun.com/document_detail/120057.html */ download(options: DownloadOptions): Promise<ProcessRet<DataDownload>>; /** * 创建目录 * @link https://help.aliyun.com/document_detail/120062.html */ mkdir(options: MkdirOptions): Promise<ProcessRet>; /** * 移动云端的 OSS 对象 * 流程为先 `cp()` 然后 `rm()` */ mv(options: MvOptions): Promise<ProcessRet<DataStat | DataBase>>; /** * OSS 远程路径是否存在 */ pathExists(options: StatOptions): Promise<boolean>; /** * 探测上传状态 * @link https://help.aliyun.com/document_detail/120061.html */ probeUpload(options: ProbUpOptions): Promise<ProcessRet>; /** * 删除云对象,不支持删除 bucket 本身 * 如果在 recusive 为 false 时删除目录,则目录参数值必须以 '/' 结尾,否则不会删除成功 * @link https://help.aliyun.com/document_detail/120053.html */ rm(options: RmOptions): Promise<ProcessRet>; /** * 递归删除,相当于 `rm -rf` * @link https://help.aliyun.com/document_detail/120053.html */ rmrf(options: RmrfOptions): Promise<ProcessRet>; /** * sign(生成签名URL) * @link https://help.aliyun.com/document_detail/120064.html */ sign(options: SignOptions): Promise<ProcessRet<DataSign>>; /** * 查看 Bucket 和 Object 信息 * @link https://help.aliyun.com/document_detail/120054.html */ stat(options: StatOptions): Promise<ProcessRet<DataStat>>; /** * 在 OSS 之间同步文件 * - force 参数默认 true * - 若 force 为 false,且目标文件存在时会卡在命令行提示输入阶段(无显示)最后导致超时异常 * @link https://help.aliyun.com/document_detail/256354.html */ syncCloud(options: SyncCloudOptions): Promise<ProcessRet<DataCp>>; /** * 同步 OSS 文件到本地 * - force 参数默认 true * - 若 force 为 false,且目标文件存在时会卡在命令行提示输入阶段(无显示)最后导致超时异常 * @link https://help.aliyun.com/document_detail/256352.html */ syncLocal(options: SyncLocalOptions): Promise<ProcessRet<DataCp>>; /** * 同步本地文件到 OSS * - force 参数默认 true * - 若 force 为 false,且目标文件存在时会卡在命令行提示输入阶段(无显示)最后导致超时异常 * @link https://help.aliyun.com/document_detail/193394.html */ syncRemote(options: SyncRemoteOptions): Promise<ProcessRet<DataCp>>; /** * 上传本地文件到 OSS * 若 force 为空或者 false,且目标文件存在时会卡在命令行提示输入阶段(无显示)最后导致超时异常 * @link https://help.aliyun.com/document_detail/120057.html */ upload(options: UploadOptions): Promise<ProcessRet<DataCp>>; private runner; private genCliParams; } /** * @link https://help.aliyun.com/document_detail/120075.html */ declare const configDownLinks: DownLinks; declare function writeConfigFile(config: Config, filePath?: string): Promise<{ path: ConfigPath; hash: string; }>; declare function validateConfigPath(config: ConfigPath): Promise<void>; declare function downloadOssutil(srcLink: string, targetPath?: string): Promise<string>; declare function setBinExecutable(file: string): Promise<OutputRow>; declare function encodeInputPath(input: string, encode?: boolean): string; export { ACLKey, BaseOptions, CmdKey, Config, ConfigPath, CpOptions, DataBase, DataCp, DataDownload, DataKey, DataSign, DataStat, DownLinks, DownloadOptions, FnKey, LinkOptions, MKey, MkdirOptions, Msg, MvOptions, OssClient, ParamMap, StatOptions as PathExistsOptions, PickFunc, PlaceholderKey, ProbUpOptions, ProcessInputFn, ProcessResp, ProcessRet, RmOptions, RmrfOptions, SignOptions, StatOptions, SyncLocalOptions, SyncOptions, SyncRemoteOptions, UploadOptions, configDownLinks, downloadOssutil, encodeInputPath, initBaseOptions, initOptions, processInputWoSrc, setBinExecutable, validateConfigPath, writeConfigFile };