1
+ #include < iostream>
2
+
3
+ #include < bsoncxx/builder/basic/document.hpp>
4
+ #include < bsoncxx/json.hpp>
5
+ #include < mongocxx/client.hpp>
6
+ #include < mongocxx/instance.hpp>
7
+ #include < mongocxx/uri.hpp>
8
+ #include < fstream>
9
+ #include < sstream>
10
+
11
+ using bsoncxx::builder::basic::kvp;
12
+ using bsoncxx::builder::basic::make_document;
13
+
14
+ int main () {
15
+ mongocxx::instance instance;
16
+ mongocxx::uri uri (" <connection string>" );
17
+ mongocxx::client client (uri);
18
+
19
+ auto db = client[" test_gridfs" ];
20
+
21
+ // Creates a GridFS bucket or references an existing one
22
+ // start-create-bucket
23
+ auto bucket = db.gridfs_bucket ();
24
+ // end-create-bucket
25
+
26
+ // Creates or references a GridFS bucket with a custom name
27
+ {
28
+ // start-create-custom-bucket
29
+ mongocxx::options::gridfs::bucket opts;
30
+ opts.bucket_name (" myCustomBucket" );
31
+
32
+ auto bucket = db.gridfs_bucket (opts);
33
+ // end-create-custom-bucket
34
+ }
35
+
36
+ // Uploads a file called "my_file" to the GridFS bucket and writes data to it
37
+ {
38
+ // start-open-upload-stream
39
+ mongocxx::options::gridfs::upload opts;
40
+ opts.chunk_size_bytes (1048576 );
41
+ auto uploader = bucket.open_upload_stream (" my_file" , opts);
42
+
43
+ // ASCII for "HelloWorld"
44
+ std::uint8_t bytes[10 ] = {72 , 101 , 108 , 108 , 111 , 87 , 111 , 114 , 108 , 100 };
45
+
46
+ for (auto i = 0 ; i < 5 ; ++i) {
47
+ uploader.write (bytes, 10 );
48
+ }
49
+
50
+ uploader.close ();
51
+ // end-open-upload-stream
52
+ }
53
+
54
+ // Uploads data to a stream, then writes the stream to a GridFS file
55
+ {
56
+ // start-upload-from-stream
57
+ std::ifstream file (" /path/to/input_file" , std::ios::binary);
58
+ bucket.upload_from_stream (" new_file" , &file);
59
+ // end-upload-from-stream
60
+ }
61
+
62
+
63
+ // Prints information about each file in the bucket
64
+ {
65
+ // start-retrieve-file-info
66
+ auto cursor = bucket.find ({});
67
+ for (auto && doc : cursor) {
68
+ std::cout << bsoncxx::to_json (doc) << std::endl;
69
+ }
70
+ // end-retrieve-file-info
71
+ }
72
+
73
+
74
+ // Downloads a file from the GridFS bucket by referencing its ObjectId value
75
+ {
76
+ // start-open-download-stream
77
+ auto doc = db[" fs.files" ].find_one (make_document (kvp (" filename" , " new_file" )));
78
+ auto id = doc->view ()[" _id" ].get_value ();
79
+
80
+ auto downloader = bucket.open_download_stream (id);
81
+
82
+ std::vector<uint8_t > buffer (downloader.file_length ());
83
+ downloader.read (buffer.data (), buffer.size ());
84
+ // end-open-download-stream
85
+ }
86
+
87
+
88
+ // Downloads an entire GridFS file to a download stream
89
+ {
90
+ // start-download-to-stream
91
+ std::ofstream output_file (" /path/to/output_file" , std::ios::binary);
92
+
93
+ auto doc = db[" fs.files" ].find_one (make_document (kvp (" filename" , " new_file" )));
94
+ auto id = doc->view ()[" _id" ].get_value ();
95
+ bucket.download_to_stream (id, &output_file);
96
+ // end-download-to-stream
97
+ }
98
+
99
+ // Deletes a file from the GridFS bucket with the specified ObjectId
100
+ {
101
+ // start-delete-files
102
+ auto doc = db[" fs.files" ].find_one (make_document (kvp (" filename" , " my_file" )));
103
+ auto id = doc->view ()[" _id" ].get_value ();
104
+
105
+ bucket.delete_file (id);
106
+ // end-delete-files
107
+ }
108
+
109
+ return 0 ;
110
+ }
0 commit comments