使用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的更多相关文章

  1. 实验mongodb使用gridfs存放一个大文件

    1.启动mongoDB 2.使用gridfs存放大文件 3.观察fs.chunks和fs.files的情况 命令 db.fs.chunks.find()查到的是一些二进制文件:

  2. MongoDB 的 GridFS 详细分析

    GridFS简介 GridFS是MongoDB中的一个内置功能,可以用于存放大量小文件. http://www.mongodb.org/display/DOCS/GridFS http://www.m ...

  3. .NET MongoDB Driver GridFS 2.2原理及使用示例

    一.API解读 1 GridFSBucketOptions 1)public string BucketName { get; set; } 获取或设置bucket名称 2)public int Ch ...

  4. MongoDB基于GridFS管理文件

    前言 GridFS是一种将大型文件存储在MongoDB的文件规范: 数据库支持以BSON格式保存二进制对象. 但是MongoDB中BSON对象最大不能超过4MB. GridFS 规范提供了一种透明的机 ...

  5. MongoDB学习-->Gridfs分布式存储&DBRef关联查询

    mongodb自带的一个分布式文件系统 fs.files _id filename md5 size uploaddate contenttype metadata {"user_id&qu ...

  6. MongoDB GridFS 对图片进行增删改

    using MongoDB.Bson; using MongoDB.Driver; using MongoDB.Driver.Builders; using MongoDB.Driver.GridFS ...

  7. ExpressJS File Uploading – GridFS – MongoDB

    n this blog post we will see how to handle multipart data/file uploading with expressjs. Save files ...

  8. c#通过操作mongodb gridfs实现文件的数据库存储

    @(编程) 源码 using MongoDB.Driver; using MongoDB.Driver.GridFS; using System.IO; namespace Wisdombud.Mon ...

  9. MongoDB GridFS 存储文件

    使用MongoDB的GridFS方式. CSDN: https://blog.csdn.net/qq_32657967/article/details/81534259官方文档: https://do ...

随机推荐

  1. golang-uuid

    uuid第三方库可以在github上找,我在这使用的是:github.com/satori/go.uuid PS:不知道哪里原因,使用go mod 管理包,下载的包和github上的不是完全一样,也特 ...

  2. 【Java】NIO中Selector的select方法源码分析

    该篇博客的有些内容和在之前介绍过了,在这里再次涉及到的就不详细说了,如果有不理解请看[Java]NIO中Channel的注册源码分析, [Java]NIO中Selector的创建源码分析 Select ...

  3. IOS --关于粘贴板 ,剪切板 ,UILabel的复制

    在iOS中下面三个控件,自身就有复制-粘贴的功能: 1.UITextView 2.UITextField 3.UIWebView UIKit framework提供了几个类和协议方便我们在自己的应用程 ...

  4. 【C语言天天练(九)】动态内存分配

    引言:数组的元素存储于内存中连续的位置上.当一个数组被声明时.它所须要的内存在编译时就被分配. 可是,我们能够使用动态内存分配在执行时为它分配内存. 一块内存的生命周期能够分为四个阶段:分配.初始化. ...

  5. android学习笔记三--Activity 布局

    1.线性布局 标签 :<LinearLayout></LinearLayout> 方向:android:orientation, 垂直:vertical 水平:Horizont ...

  6. C++学习总结 复习篇2

      延续上一小节内容:下面继续讲解虚函数和多态 虚函数和多态 基类指针可以指向任何派生类的对象,但是不能调用派生类对象的成员. 但是,基类可以调用覆盖了虚函数的函数.(现在调用将来,这有问题,说明现在 ...

  7. PHP生成excel(3)

    这一节主要是设置背景颜色.边框.字体大小.表格宽度 效果图 代码如下 <?php header("Content-Type:text/html;charset=utf-8") ...

  8. HDU 5274 Dylans loves tree(LCA+dfs时间戳+成段更新 OR 树链剖分+单点更新)

    Problem Description Dylans is given a tree with N nodes. All nodes have a value A[i].Nodes on tree i ...

  9. const成员函数总结

    const 成员函数: 类的成员函数后面加 const,表明这个函数不会对这个类对象的数据成员(准确地说是非静态数据成员)作不论什么改变. 在设计类的时候.一个原则就是对于不改变数据成员的成员函数都要 ...

  10. bash_profile打不开怎么办,用nano .bash_profile打开

    I’ve spent years curating a collection of Mac bash aliases and shortcuts to make my life easier. My ...