基于盛大的云数据库系统 MongoIC 构建图片系统

时间:2021-07-02 19:46:50

盛大云MongoIC服务(http://mongoic.grandcloud.cn),MongoIC是2011年10月中旬盛大云正式对外开放的一个系统,不仅是中国第一家专业的MongoDB云服务,也是全球第一家支持数据库恢复的MongoDB云服务。盛大云”的MongoIC支持在线一键申请数据库,可以免去搭建MongoDB集群的烦琐工作,给用户以极大的便捷。MongoIC支持各种形式的备份:用户不仅可以定制每日或每周备份,在进行重要操作前,还可以执行即时备份。而且,MongoIC上的数据库提供高可用性服务,每个数据库都会有多台机器做备份,当主机出现故障后,备用机会自动升级成主机,继续提供数据库服务,从而保证用户业务的连续性。

目前使用的是官方发布的2.0.0版本,价格方面也很实惠:

基于盛大的云数据库系统 MongoIC 构建图片系统

在上篇文章 《基于MongoDB GridFS的图片存储》 已经做了说明,这里把相关代码补上,使用的是官方的C#客户端MongoDB.Driver 1.3。

public class MongoFSDirectoryParameters
    {
        public string ConnectionString { get; set; }
        public string RootName { get; set; }

        public MongoFSDirectoryParameters()
        {
            ConnectionString = "mongodb://localhost/images";
            RootName = "imagesFs";
        }
    }

    public class MongoFSDirectory

    { 
        private readonly MongoDatabase mDatabase;
        private readonly MongoGridFS fs;
        private bool _alreadyDisposed;
        public MongoFSDirectory(MongoFSDirectoryParameters parameters)
        {   
            mDatabase = MongoDatabase.Create(parameters.ConnectionString);
            fs = new MongoGridFS(mDatabase, new MongoGridFSSettings()
            {
                Root = parameters.RootName              
            });
        }
        /// <summary>
        /// add a document to mongodb
        /// </summary>
        /// <param name="content">document bytes array</param>
        /// <returns>the unique identity filename in mongodb</returns>
        public string AddFile(string remoteFileName,byte[] content,string contentType)
        {
            MongoGridFSCreateOptions option = new MongoGridFSCreateOptions()
            {
                ContentType = contentType, UploadDate = DateTime.Now
            };
            MongoGridFSFileInfo info = new MongoGridFSFileInfo(fs, remoteFileName, option);
            using (MongoGridFSStream gfs = info.Create())
            {
                gfs.Write(content, 0, content.Length);
            }
            return remoteFileName;

        }

        /// <summary>
        /// add a document to mongodb
        /// </summary>
        /// <param name="content">document bytes array</param>
        /// <returns>the unique identity filename in mongodb</returns>
        public string AddFile(byte[] content, string contentType)
        {
            string remoteFileName = Guid.NewGuid().ToString();
            return AddFile(remoteFileName, content,contentType);
        }
        /// <summary>
        /// delete doc from mongodb
        /// </summary>
        /// <param name="filename">the unique identity filename</param>
        public string DeleteFile(string filename)
        {
            fs.Delete(filename);
            return filename;
        }

        /// <summary>
        /// 是否已经存在文件
        /// </summary>
        /// <param name="filename"></param>
        /// <returns></returns>
        public bool IsExists(string filename)
        {
            return fs.Exists(filename);
        }

        /// <summary>
        /// get document bytes array from mongodb
        /// </summary>
        /// <param name="filename">unique filename</param>
        /// <returns>bytes array</returns>
        public MongoGridFSFileInfo GetFileInfo(string filename)
        {
            var fileInfo = fs.FindOne(filename);
            return fileInfo;
        }

        /// <summary>
        /// get document bytes array from mongodb
        /// </summary>
        /// <param name="filename">unique filename</param>
        /// <returns>bytes array</returns>
        public byte[] GetFile(string filename)
        {
            byte[] bytes = null;
            using (MongoGridFSStream gfs = fs.Open(filename, FileMode.Open))
            {
                bytes = new byte[gfs.Length];
                gfs.Read(bytes, 0, bytes.Length);                
            }
            return bytes;

        }

    }

也有使用着反映存在性能问题 mongoic蛋疼的性能,期待盛大运营好。下图是保存到了盛大云里头的数据

基于盛大的云数据库系统 MongoIC 构建图片系统

Simple Service Bus / Message Queue with MongoDB