设为首页 收藏本站
查看: 1665|回复: 1

[经验分享] MongoDB 使用GridFS保存文件

[复制链接]

尚未签到

发表于 2015-7-6 10:41:15 | 显示全部楼层 |阅读模式
  上篇文章说了一下怎么试用MongoDB来做replication,但是我是通过BSON Object来保存文件,这有个局限,就是如果文件超过4m就会抛出超过最大文件的异常。
  根据官网介绍,BSON objects in MongoDB are limited to 4MB in size.   http://www.mongodb.org/display/DOCS/GridFS
  
  因此重新写了那个操作类,使用GridFS来保存文件,代码很简单,但开始接触弄了比较长时间,有一个问题一直解决不了,我希望自己生成一个Guid的 _id 而不是mongodb生成的_id,但是一直解决不了,希望哪个高手看到指点一下,谢谢!
  
  这个是使用Mongodb官网提供的客户端写的:
  



using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using MongoDB.Driver;
using MongoDB.Driver.Builders;
using MongoDB.Bson;
using MongoDB.Driver.GridFS;
namespace FileUtility
{
public class DocUtility
{
private string connStr = "";
public string ConnStr
{
get
{
if (string.IsNullOrEmpty(connStr))
{
throw new ArgumentNullException("Connection string did not specify!");
}
return connStr;
}
}
public DocUtility()
{
connStr = System.Configuration.ConfigurationManager.AppSettings["FileDb"];
}
public DocUtility(string connectionString)
{
this.connStr = connectionString;
}
///
/// add a document to mongodb
///
/// document bytes array
/// the unique identity filename in mongodb
public string AddDoc(byte[] content)
{
MongoServer server = MongoServer.Create(this.ConnStr);
try
{
string filename = Guid.NewGuid().ToString();
server.Connect();
MongoDatabase db = server.GetDatabase("ecDocs");
MongoGridFS fs = new MongoGridFS(db, new MongoGridFSSettings() { Root="ecDocs" });
MongoGridFSFileInfo info = new MongoGridFSFileInfo(fs, filename);
using (MongoGridFSStream gfs = info.Create())
{
gfs.Write(content, 0, content.Length);
}
return filename;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (server != null)
server.Disconnect();
}
}
///
/// delete doc from mongodb
///
/// the unique identity filename
public string DeleteDoc(string filename)
{
MongoServer server = MongoServer.Create(this.ConnStr);
try
{
server.Connect();
MongoDatabase db = server.GetDatabase("ecDocs");
MongoGridFS fs = new MongoGridFS(db, new MongoGridFSSettings() { Root = "ecDocs" });
fs.Delete(filename);
return filename;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (server != null)
server.Disconnect();
}
}
///
/// get document bytes array from mongodb
///
/// unique filename
/// bytes array
public byte[] GetDoc(string filename)
{
MongoServer server = MongoServer.Create(this.ConnStr);
try
{
server.Connect();
MongoDatabase db = server.GetDatabase("ecDocs");
MongoGridFS fs = new MongoGridFS(db,new MongoGridFSSettings() { Root="ecDocs" });
byte[] bytes = null;
using (MongoGridFSStream gfs = fs.Open(filename, FileMode.Open))
{
bytes = new byte[gfs.Length];
gfs.Read(bytes, 0, bytes.Length);
}
return bytes;
}
catch (Exception ex)
{
throw ex;
}
finally
{
if (server != null)
server.Disconnect();
}
}
}
}

  
  下面这个类是使用Samus的客户端:
  



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MongoDB;
using MongoDB.GridFS;
namespace FileUtilitySamus
{
public class DocUtility
{
private string connStr = "";
public string ConnStr
{
get
{
if (string.IsNullOrEmpty(connStr))
{
throw new ArgumentNullException("Connection string did not specify!");
}
return connStr;
}
}
public DocUtility()
{
connStr = System.Configuration.ConfigurationManager.AppSettings["FileDb"];
}
public DocUtility(string connectionString)
{
this.connStr = connectionString;
}
///
/// add a document to mongodb
///
/// document bytes array
/// the unique identity filename in mongodb
public string AddDoc(byte[] content)
{
using (Mongo mongo = new Mongo(this.ConnStr))
{
try
{
string filename = Guid.NewGuid().ToString();
mongo.Connect();
IMongoDatabase db = mongo.GetDatabase("ecDoc");
GridFile fs = new GridFile(db, "ecDoc");
GridFileInfo info = new GridFileInfo(db, "ecDoc", filename);
using (GridFileStream gfs = info.Create())
{
gfs.Write(content, 0, content.Length);
}
return filename;
}
catch (Exception ex)
{
throw ex;
}
}
}
///
/// delete doc from mongodb
///
/// the unique identity filename
public void DeleteDoc(string filename)
{
using (Mongo mongo = new Mongo(this.ConnStr))
{
try
{
mongo.Connect();
IMongoDatabase db = mongo.GetDatabase("ecDoc");
GridFile fs = new GridFile(db, "ecDoc");
fs.Delete(new Document("filename", filename));
}
catch (Exception ex)
{
throw ex;
}
}
}
///
/// get document bytes array from mongodb
///
/// unique filename
/// bytes array
public byte[] GetDoc(string filename)
{
using (Mongo mongo = new Mongo(this.ConnStr))
{
try
{
mongo.Connect();
IMongoDatabase db = mongo.GetDatabase("ecDoc");
GridFile fs = new GridFile(db, "ecDoc");
GridFileStream gfs = fs.OpenRead(filename);
byte[] bytes = new byte[gfs.Length];
gfs.Read(bytes, 0, bytes.Length);
return bytes;
}
catch (Exception ex)
{
if (ex.GetType() == typeof(System.IO.DirectoryNotFoundException))
throw new System.IO.FileNotFoundException("File not found :" + filename);
else
throw ex;
}
}
}
}
}

  不知道两个客户端的性能相比如何?希望测试过的同学分享一下。

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-83753-1-1.html 上篇帖子: 55最佳实践系列:MongoDB最佳实践 下篇帖子: Java和MongoDB之Hello World
累计签到:779 天
连续签到:1 天
发表于 2017-8-24 09:29:59 | 显示全部楼层
现在mongodb的gridfs上限已经修改为16MB.

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表