FastDFS的配置、部署与API使用解读(8)FastDFS多种文件上传接口详解

1、StorageClient与StorageClient1的区别

相信使用happy_fish的FastDFS的童鞋们,一定都熟悉StorageClient了,或者你熟悉的是StorageClient1,两者有什么区别呢?

我们在前面的几篇博文中已经知道,FastDFS存储文件时,是将FastDFS存储到某个Group的某个Storage的某个路径下。如果你在Storage Server上设置的是两层目录结构的话,则一个文件上传到FastDFS后的File ID一般形如"group1/M01/00/2A/rBAsVk8ORy2Nf9EoAAIRKo2Da7U901.jpg"形式。其中的“group1”称为Group Name,后段的“M01/00/08/rBAsVk8ORy2Nf9EoAAlRKo2Da7U901.jpg"称为File Name。

知道了这些后,我们就可以讨论StorageClient和StorageClient1的区别了。StorageClient中是将File ID用Group Name和File Name两部分来表示的,而StorageClient1中是将Group Name和File Name统一起来处理的。其实通过分析FastDFS Java Client的源码可以发现,StorageClient1是StorageClient的一个子类。

[java]  view plain copy print ?
  1. public class StorageClient1 extends StorageClient {  
  2.       ...  
  3. }  

StorageClient1中的upload_file1(...)方法的实现,都是在调用this.upload_file(...),将返回的Group Name和File Name组合起来返回的。


2、多种文件上传接口

目前FastDFS Java Client API的最新版本是1.19,该版本中有如下这些上传API(这里以Storage Client1为例):

[java]  view plain copy print ?
  1. /*方法1*/upload_file1(byte[] file_buff, String file_ext_name, NameValuePair[] meta_list);  
  2. /*方法2*/upload_file1(String group_name, byte[] file_buff, String file_ext_name, NameValuePair[] meta_list);  
  3. /*方法3*/upload_file1(String master_file_id, String prefix_name, byte[] file_buff, int offset, int length, String file_ext_name, NameValuePair[] meta_list);  
  4. /*方法4*/upload_file1(String master_file_id, String prefix_name, byte[] file_buff, String file_ext_name, NameValuePair[] meta_list);  
  5.   
  6. /*方法5*/upload_file1(String group_name, long file_size, UploadCallback callback, String file_ext_name, NameValuePair[] meta_list);  
  7. /*方法6*/upload_file1(String master_file_id, String prefix_name, long file_size, UploadCallback callback, String file_ext_name, NameValuePair[] meta_list);  
  8.   
  9. /*方法7*/upload_file1(String local_file_name, String file_ext_name, NameValuePair[] meta_list);  
  10. /*方法8*/upload_file1(String group_name, String local_file_name, String file_ext_name, NameValuePair[] meta_list);  
  11. /*方法9*/upload_file1(String master_file_id, String prefix_name, String local_file_name, String file_ext_name, NameValuePair[] meta_list);  


(1)多种上传格式

方法1-4是上传File Buffer(字节数组);方法5-6是通过回调的方式上传文件流(以后的文章中会解释),方法7-9是上传本地文件。


(2)多种上传方式

下面我们把这几种上传的接口的参数总结一下,方便大家使用API。

 Group NameExtensionMeta ListMaster File IDPrefix NameOffsetLengthSizeFormat
Method 1      Buffer
Method 2     Buffer
Method 3  Buffer
Method 4    Buffer
Method 5    Stream
Method 6   Stream
Method 7      Local
Method 8     Local
Method 9    Local

所有方式都可以携带参数extension和meta list。另外通过回调上传流的方式,必须给出文件大小的参数。

StorageClient1的上传方法,返回值是String,StorageClient的上传方法,返回值是String[]。



本文来自诗商 • 柳惊鸿 Poechant http://blog.csdn.net/poechant,转载请注明出处。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
文本格式不能很好显示,请见谅(附件里有比较齐整的excel表格统计) 大小类型 传输类型 api方法 文件大小 花费时间 速率byte/ms 速率mb/s 缓存数组 次数 备注 大文件 下载 download_file(group_name, remote_filename, localFile) 1073741824(约1G) 28343ms 37883 36.12804413 无 1 下载 download_file(group_name, remote_filename , downloadStream) 1073741824(约1G) 29195ms 36778 35.07423401 0 1 fastDFS的DownloadStream,FileOutputStream 下载 download_file(group_name, remote_filename , downloadStream) 1073741824(约1G) 24352ms 44092 42.04940796 2K 1 fastDFS的DowloadStream,BufferedOutputStream 下载 download_file(group_name, remote_filename , DownloadCallback) 1073741824(约1G) 24831ms 43241 41.23783112 2K 1 实现DownloadCallback,BufferedOutputStream 下载 download_file(group_name, remote_filename , DownloadCallback) 1073741824(约1G) 25922ms 41422 39.50309753 8K 1 实现DownloadCallback,BufferedOutputStream 普通文件 下载 download_file(group_name, remote_filename, localFile) 59113472(约56M) 382ms 154747 147.5782394 无 1 下载 download_file(group_name, remote_filename , downloadStream) 59113472(约57M) 369ms 160199 152.7776718 0 1 fastDFS的DownloadStream,FileOutputStream 下载 download_file(group_name, remote_filename , downloadStream) 59113472(约58M) 499ms 118702 113.2030487 2K 1 fastDFS的DowloadStream,BufferedOutputStream 下载 download_file(group_name, remote_filename , DownloadCallback) 59113472(约59M) 592ms 99853 95.22724152 2K 1 实现DownloadCallback,BufferedOutputStream 下载建议:100M内数据使用fastDFS提供的DownloadStream;大于1G的数据,使用BufferedOutputStream和DowloadStream
FastDFS是一个开源的分布式文件系统,用于轻量级的存储和文件传输。要部署FastDFS,需要按照以下步骤进行操作: 1. 下载和安装FastDFS:首先从FastDFS官方网站下载安装文件。解压缩文件后,进入解压缩目录,执行命令“./make.sh”进行编译。编译成功后,执行“./make.sh install”进行安装。 2. 配置FastDFS:在FastDFS的安装目录下,有一个“/etc/fdfs”文件夹,其中包含了FastDFS的相关配置文件。主要的配置文件是“tracker.conf”和“storage.conf”。根据需求,修改这两个文件的配置信息,如IP地址、端口号、存储路径等。 3. 启动Tracker服务器:执行命令“/etc/init.d/fdfs_trackerd start”,启动Tracker服务器。Tracker服务器用于管理所有存储服务器。 4. 启动Storage服务器:执行命令“/etc/init.d/fdfs_storaged start”,启动Storage服务器。Storage服务器用于存储文件和提供文件上传、下载的功能。 5. 测试FastDFS:可以通过FastDFS提供的客户端工具来测试系统是否正常运行,如上传和下载文件。执行命令“/usr/bin/fdfs_upload_file /etc/fdfs/client.conf test.jpg”上传文件,执行命令“/usr/bin/fdfs_download_file /etc/fdfs/client.conf group1/M00/00/00/wKgFB16e6f2AYSj5AAALZJ4f-Xg054.jpg”下载文件。 6. 配置Nginx服务器:FastDFS只提供了基本的文件存储功能,如果需要对外提供HTTP访问,可以结合Nginx使用。在Nginx的配置文件中,添加FastDFS相关的配置,如代理转发、负载均衡等。 通过以上步骤,就可以成功部署FastDFSFastDFS具有高可用性、高性能、可水平扩展等特点,适用于分布式环境下的文件存储和传输需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值