MongoDB C++ gridfs worked example

來源:互聯網
上載者:User

標籤:file_path   put   class   host   hand   private   使用   efi   set   

使用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 ();}intmain (int argc, char *argv[]){    MongoGridFS gridfs("test2gridfs");    gridfs.saveFile("test.py", "test.py");    return 0;}

 

MongoDB C++ gridfs worked example

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.