MongoDB C++ gridfs worked example
使用libmongoc,参考:http://mongoc.org/libmongoc/current/mongoc_gridfs_t.html
#include <mongoc.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h> class MongoGridFS {
public:
MongoGridFS(const char* db);
~MongoGridFS(); void saveFile(const char* input_file_path, const char* filename);
private:
mongoc_gridfs_t *gridfs;
mongoc_client_t *client;
}; MongoGridFS::MongoGridFS(const char* db) {
assert(db != NULL);
mongoc_init ();
/* connect to localhost client */
client = mongoc_client_new ("mongodb://127.0.0.1:27017?appname=gridfs-example");
assert (client);
mongoc_client_set_error_api (client, ); /* grab a gridfs handle in test prefixed by fs */
bson_error_t error;
gridfs = mongoc_client_get_gridfs (client, db, "fs", &error);
assert (gridfs);
} void MongoGridFS::saveFile(const char* input_file_path, const char* filename) {
assert(input_file_path != NULL && filename != NULL);
mongoc_stream_t *stream = mongoc_stream_file_new_for_path (input_file_path, O_RDONLY, );
assert (stream); mongoc_gridfs_file_opt_t opt = {};
opt.filename = filename; /* the driver generates a file_id for you */
mongoc_gridfs_file_t *file = mongoc_gridfs_create_file_from_stream (gridfs, stream, &opt);
assert (file); mongoc_gridfs_file_save (file);
mongoc_gridfs_file_destroy (file);
} MongoGridFS::~MongoGridFS() {
mongoc_gridfs_destroy (gridfs);
mongoc_client_destroy (client);
mongoc_cleanup ();
} int
main (int argc, char *argv[])
{
MongoGridFS gridfs("test2gridfs");
gridfs.saveFile("test.py", "test.py");
return ;
}
MongoDB C++ gridfs worked example的更多相关文章
- 实验mongodb使用gridfs存放一个大文件
1.启动mongoDB 2.使用gridfs存放大文件 3.观察fs.chunks和fs.files的情况 命令 db.fs.chunks.find()查到的是一些二进制文件:
- MongoDB 的 GridFS 详细分析
GridFS简介 GridFS是MongoDB中的一个内置功能,可以用于存放大量小文件. http://www.mongodb.org/display/DOCS/GridFS http://www.m ...
- .NET MongoDB Driver GridFS 2.2原理及使用示例
一.API解读 1 GridFSBucketOptions 1)public string BucketName { get; set; } 获取或设置bucket名称 2)public int Ch ...
- MongoDB基于GridFS管理文件
前言 GridFS是一种将大型文件存储在MongoDB的文件规范: 数据库支持以BSON格式保存二进制对象. 但是MongoDB中BSON对象最大不能超过4MB. GridFS 规范提供了一种透明的机 ...
- MongoDB学习-->Gridfs分布式存储&DBRef关联查询
mongodb自带的一个分布式文件系统 fs.files _id filename md5 size uploaddate contenttype metadata {"user_id&qu ...
- MongoDB GridFS 对图片进行增删改
using MongoDB.Bson; using MongoDB.Driver; using MongoDB.Driver.Builders; using MongoDB.Driver.GridFS ...
- ExpressJS File Uploading – GridFS – MongoDB
n this blog post we will see how to handle multipart data/file uploading with expressjs. Save files ...
- c#通过操作mongodb gridfs实现文件的数据库存储
@(编程) 源码 using MongoDB.Driver; using MongoDB.Driver.GridFS; using System.IO; namespace Wisdombud.Mon ...
- MongoDB GridFS 存储文件
使用MongoDB的GridFS方式. CSDN: https://blog.csdn.net/qq_32657967/article/details/81534259官方文档: https://do ...
随机推荐
- ctrl+c,ctrl+d,ctrl+z在linux程序中意义和区别
原文: http://blog.csdn.net/sxhlovehmm/article/details/41318111 [侵删] ctrl+c和ctrl+z都是中断命令,但是他们的作用却不一样. ...
- MySQL什么时候会使用内部临时表?
1.union执行过程 首先我们创建一个表t1 create table t1(id int primary key, a int, b int, index(a)); delimiter ;; cr ...
- git(一):了解、学习、安装git
自述 一直到今天才真正的去了解学习使用git,看<git权威指南>这本书的第一篇,忽然有很多共鸣,比如在大学开始编程的时候,总是把写的所有demo和项目保存在U盘里,内存不够用就改为移动硬 ...
- JS--截取字符串常用方法详细
使用 substring()或者slice() 函数:split() 功能:使用一个指定的分隔符把一个字符串分割存储到数组 例子: str=”jpg|bmp|gif|ico|png”; arr=the ...
- Matlab多项式拟合測试
x=0:0.2:4; %生成等差数列 rnd=rand(1,size(x,2))*5; %生成一组随机数 y=x.*x.*x+x.*x+6+rnd; %生成y=x^3+x^2+6函数在垂直方向5个尺度 ...
- 用C++实现一个Log系统
提要 近期在写一些C++的图形代码,在调试和測试过程中都会须要在终端打印一些信息出来. 之前的做法是直接用 std::cout<<"Some Word"<< ...
- 生活娱乐 ATM机键盘余温泄露密码
安全系统存漏洞 ATM机键盘余温或泄露密码 ATM机会泄露你的银行卡密码? 据美国<大众科学>网站8月30日报道,你的手指在ATM机上留下的余温能让尾随你而来的黑客准确获知你的密码. 加利 ...
- 系统重装 WIN7如何创建和使用VHD文件
1 在磁盘管理中,点击操作-创建VHD,然后可以创建一个空的VHD文件 2 右击这个磁盘,点击初始化磁盘,然后可以新建简单卷 3 右击这个磁盘,设置为脱机或者联机就可以在计算机中显示和隐藏这个 ...
- linux系统之shell编程-正則表達式
shell编程正則表達式: 1:元字符 [ ] . * ? + ( ) | { } ^ $ 2 : [a-z0-9] 表示匹配随意数字和字母的一个 3 : [^a-z] ...
- VBscript 做的设置网卡名称
Set WSHShell=WScript.CreateObject("WScript.Shell") Dim NetcardDescriptionDim NetcardName i ...