通过redis扩展分布式存储fastdfs的数据对应及方案

本文涉及的产品
云数据库 Tair(兼容Redis),内存型 2GB
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介:

前言:

   周天被朋友拖住了,他说最近要实现一个文件分享的网站,一个是图片,一个是特定的文件,比如压缩包、pdf这类的。 他是做idc的,手里的资源还是不少的, 想通过文件分享网站来导入点所谓的流量。哥年轻时跟他搞过一个php的淘宝图片站,结果有个卖衣服的站点,流量很大,成本太高了,再说也没钱再次投入了,也就放弃 ~   没赚多少钱,倒是赔给那几个淘宝卖家100多块钱。  想起往事,还算有趣的 ~

现在的我以前不是当初菜鸟的我了,google了半天,从别地方,拔下来一个html模板,而且还是很高端的样子,后端是用pyton的tornado框架改了改,跑了几个实例都是没有问题的 ~


实现的方法简单的

上传

建立表单,文件上传,然后写如到tmp目录的下,调用自己写的python fastdfs模块上传文件。也可以放到目录后,用inotify 来做文件的监控,触发文件后,就上传到fastdfs分布式存储里面。

下载

可以通过fastdfs本身的web做下载连接,但是为了更高的性能,推荐使用nginx fastdfs模块来实现的下载,给朋友也是这么做的。


(还没有正式的上线,嘿嘿)

subprocess 调用系统的fastdfs客户端

1
上传文件:/usr/local/bin/fdfs_upload_file  <config_file> <local_filename>


tornado的文件上传api

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def post(self):
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     upload_path=os.path.join(os.path.dirname(__file__), 'files' )  #文件的暂存路径
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     file_metas=self.request.files[ 'file' ]    #提取表单中‘name’为‘file’的文件元数据
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     for  meta  in  file_metas:
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
         filename=meta[ 'filename' ]
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
         filepath=os.path.join(upload_path,filename)
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
         with  open(filepath, 'wb' as  up:      #有些文件需要已二进制的形式存储,实际中可以更改
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
             up.write(meta[ 'body' ])
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
         self.write( 'finished!' )
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
app=tornado.web.Application([
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
     (r '/file' ,UploadFileHandler),
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
])


wKiom1LJmfKi7TBIAAE4agppxMA452.jpg


一般来说,上传后得到的文件名字,是一串hash,和以前的名字比起来不太一样。


     举个例子,我明明上传的文件时 xiaorui.jpg,结果fastdfs接口给我返回的是 一堆的hash ,这是在让人有点小郁闷。

    我这里还只是图片的应用,后期文件功能出来后,让人看到的都是hash,你都不能通过hash想象出他的文件。

wKioL1LJogihA8GBAAA1TDtMvdE176.jpg


查看了uc那边的技术博客,扩展了一套简单的路子。


这边用到的是   nginx tornado  jquery bootstrap


1.   应用系统在上传文件到FastDFS成功时将原始文件名和“文件索引(FID)”保存下来,我这里会记录在redis里面。

2.  在浏览器下载的时用Nginx的域名和FID拼出url,在url后面增加一个参数,指定原始文件名。例如: xxx.xx.xx.xx/group2/M00/00/89/eQ6h3FKJf_PRl8p4AUz4wO8tqaA688.apk?attname=xiaorui.cc


3. 在Nginx上进行如下配置,这样Nginx就会截获url中的参数attname,在Http响应头里面加上字段 Content-Disposition “attachment;filename=$arg_attname”。



1
2
3
4
5
6
7
8
9
10
11
12
13
14
location /group1/M00 {
root /data/store/data1;
if  ($arg_attname ~  "^(.*).cc" ) {
     add_header Content-Disposition  "attachment;filename=$arg_attname" ;
}
ngx_fastdfs_module;
}
location /group2/M00 {
root /data/store/data2;
if  ($arg_attname ~  "^(.*).cc" ) {
     add_header Content-Disposition  "attachment;filename=$arg_attname" ;
}
ngx_fastdfs_module;
}


4.   浏览器发现响应头里面有Content-Disposition “attachment;filename=$arg_attname”时,就会把文件名显示成filename指定的名称。


大家可以测试下,原来就是给用户的url多出一个get参数,用nginx识别到之后,插入到header头信息Content-Disposition 协议。


到半夜为止,和哥们一块已经实现了该项目的大部门网络框架,当然前端的nginx


是用kvm虚拟机做的,毕竟7层对io需求不是很大。针对nginx做的轮训是在代码


层面实现的。


wKiom1LJq5KzMMsmAAC8ES5PABE643.jpg



好了,就这样吧。 这fastdfs真是个好东西 ! 特别适合以文件为载体的在线服


务,如图片站,文件分享等等 !






 本文转自 rfyiamcool 51CTO博客,原文链接:http://blog.51cto.com/rfyiamcool/1348839,如需转载请自行联系原作者




相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
相关文章
|
1天前
|
缓存 NoSQL Java
大数据-50 Redis 分布式锁 乐观锁 Watch SETNX Lua Redisson分布式锁 Java实现分布式锁
大数据-50 Redis 分布式锁 乐观锁 Watch SETNX Lua Redisson分布式锁 Java实现分布式锁
13 3
大数据-50 Redis 分布式锁 乐观锁 Watch SETNX Lua Redisson分布式锁 Java实现分布式锁
|
1天前
|
存储 缓存 NoSQL
大数据-38 Redis 高并发下的分布式缓存 Redis简介 缓存场景 读写模式 旁路模式 穿透模式 缓存模式 基本概念等
大数据-38 Redis 高并发下的分布式缓存 Redis简介 缓存场景 读写模式 旁路模式 穿透模式 缓存模式 基本概念等
14 4
|
1天前
|
存储 NoSQL 大数据
大数据-51 Redis 高可用方案CAP-AP 主从复制 一主一从 全量和增量同步 哨兵模式 docker-compose测试
大数据-51 Redis 高可用方案CAP-AP 主从复制 一主一从 全量和增量同步 哨兵模式 docker-compose测试
14 3
|
1天前
|
缓存 NoSQL Ubuntu
大数据-39 Redis 高并发分布式缓存 Ubuntu源码编译安装 云服务器 启动并测试 redis-server redis-cli
大数据-39 Redis 高并发分布式缓存 Ubuntu源码编译安装 云服务器 启动并测试 redis-server redis-cli
14 3
|
1天前
|
消息中间件 缓存 NoSQL
大数据-49 Redis 缓存问题中 穿透、雪崩、击穿、数据不一致、HotKey、BigKey
大数据-49 Redis 缓存问题中 穿透、雪崩、击穿、数据不一致、HotKey、BigKey
11 2
|
1天前
|
缓存 分布式计算 NoSQL
大数据-43 Redis 功能扩展 Lua 脚本 对Redis扩展 eval redis.call redis.pcall
大数据-43 Redis 功能扩展 Lua 脚本 对Redis扩展 eval redis.call redis.pcall
12 2
|
1天前
|
SQL 分布式计算 NoSQL
大数据-42 Redis 功能扩展 发布/订阅模式 事务相关的内容 Redis弱事务
大数据-42 Redis 功能扩展 发布/订阅模式 事务相关的内容 Redis弱事务
11 2
|
3天前
|
存储 缓存 NoSQL
【redis】数据量庞大时的应对策略
【redis】数据量庞大时的应对策略
15 1
|
3天前
|
存储 NoSQL 关系型数据库
【redis】认识redis和分布式系统
【redis】认识redis和分布式系统
14 1
|
3天前
|
存储 NoSQL Redis
redis保存数据的结构-redisobject结构体
`redisObject`结构体是Redis内部数据组织的核心,它通过集成类型标识、引用计数和编码方式等关键信息,实现了数据的高效管理和访问。这种设计允许Redis根据数据的实际需求动态调整存储结构,既保证了内存使用的高效性,也确保了数据操作的灵活性和速度。通过对 `redisObject`的深入了解,可以更好地掌握Redis如何在内存中高效存储和操作数据,进而优化数据库的性能和资源利用。
11 0

热门文章

最新文章