npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@mt-utils/huawei-obs

v1.0.0

Published

华为云OBS文件上传下载

Downloads

73

Readme

@mt-utils/huawei-obs

华为云OBS文件上传下载工具库,用于处理文件的分片上传和下载。

特点

  • 提供灵活的参数配置,包括分片大小、上传目录等。
  • 支持使用默认参数或部分覆盖以简化配置流程。
  • 支持分片上传,提高大文件上传的稳定性和效率。
  • 提供详细的错误码和回调事件,便于调试和监控上传进度。

安装

pnpm install @mt-utils/huawei-obs

使用方法

引入库

import huaweiBbs from '@mt-utils/huawei-obs'

配置系统参数

配置必要的系统参数,这些参数通常在华为云平台申请。

/**
 * 华为云OBS平台系统配置
 */
const systemConfig = {
  /**
   * OBS为每个区域提供了一个Endpoint,用于处理该区域的访问请求。
   */
  ENDPOINT: 'your_endpoint',
  /**
   * 在华为云平台申请的密钥ID。
   */
  ACCESS_KEY_ID: 'your_access_key_id',
  /**
   * 与ACCESS_KEY_ID对应的密钥信息。
   */
  SECRET_ACCESS_KEY: 'your_secret_access_key',
  /**
   * OBS桶的名称
   */
  BUCKET_NAME: 'your_bucket_name'
}

// 配置系统参数
huaweiBbs.config(systemConfig)

执行分片上传

// 所需上传的文件
const file = new File(['hello world'], 'hello.txt', { type: 'text/plain' })

/**
 * 分片上传选项
 */
const sliceUploadOptions = {
  /**
   * 文件分片大小,单位为字节。
   * 默认为1M
   */
  partSize: 1024 * 1024 * 5, // 5MB
  /**
   * 上传进度回调
   * @param event - 包含上传进度信息的对象
   * @param event.percent - 上传进度百分比
   */
  onProgress: (event) => {
    console.log(`Upload progress: ${event.percent}%`)
  },
  /**
   * 上传成功回调
   * @param event - 包含上传结果信息的对象
   * @param event.sourceFile - 文件对象
   * @param event.fileDir - 文件上传目录
   */
  onSuccess: (event) => {
    console.log('Upload success:', event)
  },
  /**
   * 上传失败回调
   * @param error - 错误对象,可能是标准 Error 对象或自定义 ObsError 对象
   */
  onError: (error) => {
    console.error('Upload error:', error)
  },
  /**
   * 取消上传回调
   */
  onAbort: () => {
    console.log('Upload aborted')
  },
  /**
   * 上传结束回调,无论成功或失败都会触发
   */
  onFinally: () => {
    console.log('Upload finished')
  },
  /**
   * 上传开始回调
   */
  onStart: () => {
    console.log('Upload started')
  }
}

// 执行分片上传
const sliceUpload = huaweiBbs.createUploadContext(file).createSliceUpload(sliceUploadOptions)

// 取消分片上传
sliceUpload.abort()

错误码

以下是华为云OBS工具库定义的错误码及其描述:

| 错误码 | 代码 | 描述 | | :------------------- | :--------------------------- | :------ | | 请求失败,请检查网络 | UPLOAD_NETWORK_ERROR | 10001 | | 切片上传初始化失败 | SLICE_UPLOAD_INIT_ERROR | 10002 | | 文件切片上传失败 | SLICE_UPLOAD_UPLOAD_ERROR | 10003 | | 切片合并失败 | SLICE_UPLOAD_MERGE_ERROR | 10005 | | 取消分段上传任务失败 | SLICE_UPLOAD_ABORT_ERROR | 10006 | | 取消分段上传任务成功 | SLICE_UPLOAD_ABORT_SUCCESS | 10007 |

注意

本工具库为内部工具库,不负责改BUG,请勿随便下载。