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