开发者社区> 桃子红了呐> 正文
阿里云
为了无法计算的价值
打开APP
阿里云APP内打开

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, 2);

    /* 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, 0);
    assert (stream);

    mongoc_gridfs_file_opt_t opt = {0};
    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 0;
}
复制代码

 






本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/bonelee/p/6568211.html,如需转载请自行联系原作者


版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
springMvc37-Unknow tag(c:forEach)错误解决办法,jstl.jar包以及standard.jar包下载与导入
springMvc37-Unknow tag(c:forEach)错误解决办法,jstl.jar包以及standard.jar包下载与导入
16 0
EXEL文件转成简书MD表格
源码和下载: 0.1.3 mac: https://github.com/fanfeilong/exceltk/blob/master/pub/exceltk.
840 0
eBay的Turmeric和VJet的源程序移到了GitHub上
eBay的开源SOA平台Turmeric和Vjet项目的源码已被迁移到GitHub上,列在ebay opensource组织名下。据David Carver介绍,“此举是为了更好地与各个开源社区互动,并提供一个简单的方法给潜在的代码提供者提交代码。
695 0
文章
问答
文章排行榜
最热
最新
相关电子书
更多
ApsaraDB for MongoDB
立即下载
MongoShake -- Multi Active-Active and Cross-Region Disaster Recoverable MongoDB Service
立即下载
Hadoop & Cloud Storage: Object Store Integration in Production
立即下载