@mwcp/ali-oss
TypeScript icon, indicating that this package has built-in type declarations

22.0.1 • Public • Published

@mwcp/ali-oss

Ali Oss ossutil client component for midway.js

Version License Conventional Commits

Note

ESM build only, requires @midwayjs >= 3.12 and set "type": "module" in packages.json

Prepare

ossutil 下载页面

sudo wget https://gosspublic.alicdn.com/ossutil/1.7.11/ossutil64 -O /usr/bin/ossutil
sudo chmod a+x /usr/bin/ossutil

Installation

npm i @mwcp/ali-oss

Configuration

Enable Plugin

Edit ${app_root}/src/configuration.ts:

import { join } from 'path'
import { ILifeCycle } from '@midwayjs/core'
import { Configuration } from '@midwayjs/decorator'
import * as aliOss from '@mw-components/ali-oss'

@Configuration({
  imports: [
    aliOss,
  ],
  importConfigs: [join(__dirname, 'config')],
})
export class ContainerConfiguration implements ILifeCycle { }

Add Configurations

Update project src/config/config.prod.ts

import { Config } from '@mw-components/ali-oss'

export enum ClientKey {
  ossmain = 'ossmain',
}

const clientConfig = {
  accessKeyId: process.env.ALI_OSS_AID || '',
  accessKeySecret: process.env.ALI_OSS_ASECRET || '',
  endpoint: process.env.ALI_OSS_ENDPOINT || 'https://oss-cn-hangzhou.aliyuncs.com',
  bucket: process.env.ALI_OSS_BUCKET || '',
  cmd: 'ossutil',
  debug: false,
}

export const aliOssConfig: Readonly<Config<ClientKey>> = {
  ossmain: clientConfig,
}

Usage

Inject and mkdir()

import assert from 'node:assert/strict'
import { AliOssComponent } from '@mw-components/ali-oss'
import { App, Config, Inject } from '@midwayjs/decorator'

export class RootClass {
  @Inject() readonly ossClient: AliOssComponent

  async mkdir(path: string): Promise<void> {
    const mkdirRet = await ossClient.mkdir('ossmain', path)
    assert(! mkdirRet.exitCode, 'mkdir failed')
    assert(mkdirRet.data, 'mkdir failed')
  }
}

Methods

  • cp() 在远程拷贝
  • createSymlink() 创建软连接
  • mkdir() 创建目录
  • mv() 在远程移动对象
  • pathExists() 检测远程文件是否存在
  • rm() 删除远程对象
  • rmrf() 删除远程对象及其下级所有对象
  • stat() 获取远程对象属性
  • syncLocal() (强制)同步远程对象到本地
  • syncRemote() (强制)同步本地目录到远程
  • upload() 上传本地文件到远程

License

MIT

Package Sidebar

Install

npm i @mwcp/ali-oss

Weekly Downloads

259

Version

22.0.1

License

MIT

Unpacked Size

107 kB

Total Files

85

Last publish

Collaborators

  • waiting