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

fastdfs-client

v1.0.2

Published

Fastdfs Clinet in NodeJS

Downloads

38

Readme

Nodejs Client for FastDFS

FastDFS 是分布式文件存储系统。此项目是FastDFS在NodeJS平台下的客户端,可以用于生产环境与FastDFS进行交互,包含文件增删改查、服务信息获取等功能。

说明

本项目git:https://github.com/BigMeat233/fastdfs-client(fork自[email protected]:ymyang/fdfs.git)

fixed:

  1. FastDFS 5.11执行listStorages失败的BUG

attention:

  1. 多tracker配置时,将会按配置顺序进行连接,不会进行负载均衡

安装


npm install fastdfs-client

使用


var FdfsClient = require('fastdfs-client');

var fdfs = new FdfsClient({
    // tracker servers
    trackers: [
        {
            host: 'tracker.fastdfs.com',
            port: 22122
        }
    ],
    // 默认超时时间10s
    timeout: 10000,
    // 默认后缀
    // 当获取不到文件后缀时使用
    defaultExt: 'txt',
    // charset默认utf8
    charset: 'utf8'
});

以上是一些基本配置,你还可以自定义你的日志输出工具,默认是使用console 例如你要使用debug作为你的日志输出工具,你可以这么做:


var debug = require('debug')('fastdfs-client');
var fdfs = new FdfsClient({
    // tracker servers
    trackers: [
        {
            host: 'tracker.fastdfs.com',
            port: 22122
        }
    ],
    logger: {
        log: debug
    }
});

上传文件

注:以下fileId为group + '/' + filename,以下的所有操作使用的fileId都是一样

通过本地文件名上传


fdfs.upload('e:/shou.jpg').then(function(fileId) {
    // fileId 为 group + '/' + filename
    console.log(fileId);
}).catch(function(err) {
    console.error(err);
);

上传Buffer


var fs = require('fs');

// 注意此处的buffer获取方式只为演示功能,实际不会这么去构建buffer
var buffer = fs.readFileSync('test.gif');
fdfs.upload(buffer).then(function(fileId) {
    // fileId 为 group + '/' + filename
    console.log(fileId);
}).catch(function(err) {
    console.error(err);
);

ReadableStream


var fs = require('fs');

var rs = fs.createReadStream('test.gif');
fdfs.upload(rs).then(function(fileId) {
    // fileId 为 group + '/' + filename
    console.log(fileId);
}).catch(function(err) {
    console.error(err);
);

其他一些options,作为第2个参数传入


fdfs.upload('test.gif', {
    // 上传方法 [upload, uploadAppender, append, modify], 默认为upload
    method: 'upload',
    // 指定文件存储的group,不指定则由tracker server分配
    group: 'group1',
    // method为append或modify指定追加的源文件
    fileId: 'group1/M00/00/0F/wKgBeFXlZJuAdsBZAAPm5H9JxDA153.jpg',
    // file bytes, file参数为ReadableStream时必须指定
    size: 1024,
    // method为modify指定追加的源文件的起始点
    offset: 10240,
    // 上传文件的后缀,不指定则获取file参数的后缀,不含(.)
    ext: 'jpg'
}).then(function(fileId) {
    // fileId 为 group + '/' + filename
    console.log(fileId);
}).catch(function(err) {
    console.error(err);
);
 

下载文件

下载到本地


fdfs.download(fileId, 'test_download.gif').then(function() {
    // 下载完成
    
}).catch(function(err) {
    console.error(err);
);

下载到WritableStream


var fs = require('fs');
var ws = fs.createWritableStream('test_download.gif');
fdfs.download(fileId, ws).then(function() {
    // 下载完成
    
}).catch(function(err) {
    console.error(err);
);

下载文件片段


fdfs.download(fileId, {
    target: 'test_download.part',
    offset: 5,
    bytes: 5
}).then(function() {
    // 下载完成
    
}).catch(function(err) {
    console.error(err);
);

删除文件


fdfs.del(fileId).then(function() {
    // 删除成功
    
}).catch(function(err) {
    console.error(err);
);

获取文件信息


fdfs.getFileInfo(fileId).then(function(fileInfo) {
    // fileInfo有4个属性
    // {
    //   // 文件大小
    //   size:
    //   // 文件创建的时间戳,单位为秒
    //   timestamp:
    //   // 校验和
    //   crc32:
    //   // 最初上传到的storage server的ip
    //   addr:
    // }
    console.log(fileInfo);
}).catch(function(err) {
    console.error(err);
);

文件的Meta Data

设置Meta Data, 我只贴出来文件签名信息吧,flag字段如果不传则默认是O


/**
 * @param fileId
 * @param metaData  {key1: value1, key2: value2}
 * @param flag 'O' for overwrite all old metadata (default)
                'M' for merge, insert when the meta item not exist, otherwise update it
 */
fdfs.setMetaData(fileId, metaData, flag).then(function() {
    // 设置成功
    
}).catch(function(err) {
    console.error(err);
); 

获取Meta Data


fdfs.getMetaData(fileId).then(function(metaData) {
    console.log(metaData);
}).catch(function(err) {
    console.error(err);
);

group信息


fdfs.listGroups().then(function(groups) {
    console.log(groups);
}).catch(function(err) {
    console.error(err);
); 

storage信息


fdfs.listStorages(‘group1’).then(function(storages) {
    console.log(storages);
}).catch(function(err) {
    console.error(err);
);