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

upyun-upload-tool

v0.0.8

Published

upyun node-sdk 封装

Downloads

46

Readme

upyun-upload-tool

对 upyun sdk node-sdk 的封装。依赖[email protected],暂不适用于浏览器环境。

提供上传文件、上传文件夹、删除文件、获取文件信息、获取文件夹列表等功能。 提供上传时自动失败重试功能。 提供上传文件夹时文件并发上传控制功能。

安装

npm install upyun-upload-tool

使用

首先你需要一个又拍云存储的账号,包含服务名(以前叫 bucket/空间名等)、用户名(操作员)、密码。 以下提到的远程路径,如无特别说明,指又拍云服务中文件、目录的路径。

初始化:

const UpYunTool = require('upyun-upload-tool');
const client = new UpYunTool({
  bucket: 'your-service-name',
  operator: 'your-user-name',
  password: 'your-password',
  console: true, // 是否展示执行过程中的一些信息
  retry: 3 // 当上传文件、生成远程目录失败时的重试次数,默认3次。
  concurrent: 6 // 上传文件夹时的并发上传文件数量,默认是6。当传递null或0时,将不会控制并发数量
});

接口

上传文件 putFile

putFile(remotePath, localFile, options = {}) 注意 这里的 localFile 是文件内容,支持 String,Stream, Buffer, 而不是文件的路径。 上传文件失败时,默认重试 3 次,如果 3 次都失败,则本次上传失败。

/**
 * @param {String} remotePath - 远程文件保存路径
 * @param {String|Stream|Buffer} localFile - 需要上传的本地文件
 * @param {Object} options - 上传参数 Content-MD5 | Content-Length | Content-Type | Content-Secret | x-gmkerl-thumb | x-upyun-meta-x | x-upyun-meta-ttl 参见http://docs.upyun.com/api/rest_api/#_2
 */
client
  .putFile(remotePath, localFile, (options = {}))
  .then((res) => console.log(res))
  .catch((error) => console.log(error));

上传文件夹 putDir

上传过程会自动在远程创建文件夹,创建目录或上传文件过程如果失败,也会重试 3 次。 注意,为了上传文件夹的方便,这里 localPath 接收的是本地文件夹路径,会自动将里面的文件转换成 stream 的形式上传。 注意, 因为上传文件夹时使用了递归来处理文件夹中的存在文件夹情况,如果选择了并发控制,保险起见,请将 concurrent 至少设置为子文件夹数 + 1 。

/**
 * @param {String} remotePath - 远程存储路径
 * @param {String} localPath - 本地文件夹目录
 */
client
  .putDir(remotePath, localPath)
  .then((res) => console.log(res))
  .catch((error) => console.log(error));

删除文件 deleteFile

删除失败不会重试,业务层面可以自行处理

client
  .deleteFile(remotePath)
  .then((res) => console.log(res))
  .catch((error) => console.log(error));

下载文件 getFile

下载保存在又拍云存储的文件,如果文件不存在返回 false。

const fs = require("fs");
const writeSream = fs.createWriteStream("./local-file-path.png");
/**
 * 下载文件
 * @param {String} remotePath - 文件远程路径
 * @param {Stream} saveStream - 可选值,如果传递则要传递一个流,下载的文件写入该流中。
 */
client
  .getFile(remotePath, writeSream)
  .then((res) => console.log("download ok"))
  .catch((error) => console.log(error));

如果不传递 saveStream,则返回文件的内容

创建目录 makeDir

创建失败也会重试 3 次。

/**
 * @param {String} remotePath - 想创建的远程目录路径
 */
client
  .makeDir(remotePath)
  .then((res) => console.log(res))
  .catch((error) => console.log(error));

获取目录下文件列表

/**
 * @param {String} remotePath - 需要查看的目录
 * @param {Object} options - 配置参数
 * @param {Number} options.limit - 每次请求获取的目录最大列表,最大值 10000,默认 100
 * @param {String} options.order - 列表以文件最后修改时间排序,可选值 asc|desc,默认 asc
 * @param {String} options.iter - 遍历起点,每次响应参数中,将会包含遍历下一页需要的 iter 值
 * @return {Object}
 * {
 *		files: [
 *		  {
 *		    name: 'example.txt', // file or dir name
 *		    type: 'N', // file type, N: file; F: dir
 *		    size: 28392812, // file size
 *		    time: 1486053098 // last modify time
 *		  }
 *		],
 *   next: 'dlam9pd2Vmd2Z3Zg==' // next page iter
 * }
 */
client
  .listDir(remote, (options = { limit: 100, order: "asc" }))
  .then((res) => {
    // 是否要再次获取由业务层面自行处理
  })
  .catch((error) => console.log(error));

获取文件信息 headFile

client
  .headFile(remotePath)
  .then((res) => console.log(res))
  .catch((error) => console.log(error));