1MONGOC_GRIDFS_T(3) libmongoc MONGOC_GRIDFS_T(3)
2
3
4
6 This GridFS implementation does not conform to the MongoDB GridFS
7 specification. For a spec compliant implementation, use
8 mongoc_gridfs_bucket_t.
9
11 #include <mongoc/mongoc.h>
12
13 typedef struct _mongoc_gridfs_t mongoc_gridfs_t;
14
16 mongoc_gridfs_t provides a MongoDB gridfs implementation. The system as
17 a whole is made up of gridfs objects, which contain gridfs_files and
18 gridfs_file_lists. Essentially, a basic file system API.
19
20 There are extensive caveats about the kind of use cases gridfs is prac‐
21 tical for. In particular, any writing after initial file creation is
22 likely to both break any concurrent readers and be quite expensive.
23 That said, this implementation does allow for arbitrary writes to ex‐
24 isting gridfs object, just use them with caution.
25
26 mongoc_gridfs also integrates tightly with the mongoc_stream_t abstrac‐
27 tion, which provides some convenient wrapping for file creation and
28 reading/writing. It can be used without, but its worth looking to see
29 if your problem can fit that model.
30
31 WARNING:
32 mongoc_gridfs_t does not support read preferences. In a replica set,
33 GridFS queries are always routed to the primary.
34
36 mongoc_gridfs_t is NOT thread-safe and should only be used in the same
37 thread as the owning mongoc_client_t.
38
40 It is an error to free a mongoc_gridfs_t before freeing all related in‐
41 stances of mongoc_gridfs_file_t and mongoc_gridfs_file_list_t.
42
44 example-gridfs.c
45
46 #include <assert.h>
47 #include <mongoc/mongoc.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <fcntl.h>
51
52 int
53 main (int argc, char *argv[])
54 {
55 mongoc_gridfs_t *gridfs;
56 mongoc_gridfs_file_t *file;
57 mongoc_gridfs_file_list_t *list;
58 mongoc_gridfs_file_opt_t opt = {0};
59 mongoc_client_t *client;
60 const char *uri_string = "mongodb://127.0.0.1:27017/?appname=gridfs-example";
61 mongoc_uri_t *uri;
62 mongoc_stream_t *stream;
63 bson_t filter;
64 bson_t opts;
65 bson_t child;
66 bson_error_t error;
67 ssize_t r;
68 char buf[4096];
69 mongoc_iovec_t iov;
70 const char *filename;
71 const char *command;
72 bson_value_t id;
73
74 if (argc < 2) {
75 fprintf (stderr, "usage - %s command ...\n", argv[0]);
76 return EXIT_FAILURE;
77 }
78
79 mongoc_init ();
80
81 iov.iov_base = (void *) buf;
82 iov.iov_len = sizeof buf;
83
84 /* connect to localhost client */
85 uri = mongoc_uri_new_with_error (uri_string, &error);
86 if (!uri) {
87 fprintf (stderr,
88 "failed to parse URI: %s\n"
89 "error message: %s\n",
90 uri_string,
91 error.message);
92 return EXIT_FAILURE;
93 }
94
95 client = mongoc_client_new_from_uri (uri);
96 assert (client);
97 mongoc_client_set_error_api (client, 2);
98
99 /* grab a gridfs handle in test prefixed by fs */
100 gridfs = mongoc_client_get_gridfs (client, "test", "fs", &error);
101 assert (gridfs);
102
103 command = argv[1];
104 filename = argv[2];
105
106 if (strcmp (command, "read") == 0) {
107 if (argc != 3) {
108 fprintf (stderr, "usage - %s read filename\n", argv[0]);
109 return EXIT_FAILURE;
110 }
111 file = mongoc_gridfs_find_one_by_filename (gridfs, filename, &error);
112 assert (file);
113
114 stream = mongoc_stream_gridfs_new (file);
115 assert (stream);
116
117 for (;;) {
118 r = mongoc_stream_readv (stream, &iov, 1, -1, 0);
119
120 assert (r >= 0);
121
122 if (r == 0) {
123 break;
124 }
125
126 if (fwrite (iov.iov_base, 1, r, stdout) != r) {
127 MONGOC_ERROR ("Failed to write to stdout. Exiting.\n");
128 exit (1);
129 }
130 }
131
132 mongoc_stream_destroy (stream);
133 mongoc_gridfs_file_destroy (file);
134 } else if (strcmp (command, "list") == 0) {
135 bson_init (&filter);
136
137 bson_init (&opts);
138 bson_append_document_begin (&opts, "sort", -1, &child);
139 BSON_APPEND_INT32 (&child, "filename", 1);
140 bson_append_document_end (&opts, &child);
141
142 list = mongoc_gridfs_find_with_opts (gridfs, &filter, &opts);
143
144 bson_destroy (&filter);
145 bson_destroy (&opts);
146
147 while ((file = mongoc_gridfs_file_list_next (list))) {
148 const char *name = mongoc_gridfs_file_get_filename (file);
149 printf ("%s\n", name ? name : "?");
150
151 mongoc_gridfs_file_destroy (file);
152 }
153
154 mongoc_gridfs_file_list_destroy (list);
155 } else if (strcmp (command, "write") == 0) {
156 if (argc != 4) {
157 fprintf (stderr, "usage - %s write filename input_file\n", argv[0]);
158 return EXIT_FAILURE;
159 }
160
161 stream = mongoc_stream_file_new_for_path (argv[3], O_RDONLY, 0);
162 assert (stream);
163
164 opt.filename = filename;
165
166 /* the driver generates a file_id for you */
167 file = mongoc_gridfs_create_file_from_stream (gridfs, stream, &opt);
168 assert (file);
169
170 id.value_type = BSON_TYPE_INT32;
171 id.value.v_int32 = 1;
172
173 /* optional: the following method specifies a file_id of any
174 BSON type */
175 if (!mongoc_gridfs_file_set_id (file, &id, &error)) {
176 fprintf (stderr, "%s\n", error.message);
177 return EXIT_FAILURE;
178 }
179
180 if (!mongoc_gridfs_file_save (file)) {
181 mongoc_gridfs_file_error (file, &error);
182 fprintf (stderr, "Could not save: %s\n", error.message);
183 return EXIT_FAILURE;
184 }
185
186 mongoc_gridfs_file_destroy (file);
187 } else {
188 fprintf (stderr, "Unknown command");
189 return EXIT_FAILURE;
190 }
191
192 mongoc_gridfs_destroy (gridfs);
193 mongoc_uri_destroy (uri);
194 mongoc_client_destroy (client);
195
196 mongoc_cleanup ();
197
198 return EXIT_SUCCESS;
199 }
200
201
202 SEE ALSO:
203 The MongoDB GridFS specification.
204
205 The spec-compliant mongoc_gridfs_bucket_t.
206
207
209 MongoDB, Inc
210
212 2017-present, MongoDB, Inc
213
214
215
216
2171.25.1 Nov 08, 2023 MONGOC_GRIDFS_T(3)