1MONGOC_GRIDFS_T(3)             MongoDB C Driver             MONGOC_GRIDFS_T(3)
2
3
4

NAME

6       mongoc_gridfs_t - mongoc_gridfs_t
7

SYNOPSIS

9          #include <mongoc/mongoc.h>
10
11          typedef struct _mongoc_gridfs_t mongoc_gridfs_t;
12

DESCRIPTION

14       mongoc_gridfs_t provides a MongoDB gridfs implementation. The system as
15       a whole is made up of gridfs objects, which  contain  gridfs_files  and
16       gridfs_file_lists.  Essentially, a basic file system API.
17
18       There are extensive caveats about the kind of use cases gridfs is prac‐
19       tical for. In particular, any writing after initial  file  creation  is
20       likely  to  both  break  any concurrent readers and be quite expensive.
21       That said, this implementation  does  allow  for  arbitrary  writes  to
22       existing gridfs object, just use them with caution.
23
24       mongoc_gridfs also integrates tightly with the mongoc_stream_t abstrac‐
25       tion, which provides some convenient wrapping  for  file  creation  and
26       reading/writing.   It can be used without, but its worth looking to see
27       if your problem can fit that model.
28
29       WARNING:
30          mongoc_gridfs_t does not support read preferences. In a replica set,
31          GridFS queries are always routed to the primary.
32

THREAD SAFETY

34       mongoc_gridfs_t  is NOT thread-safe and should only be used in the same
35       thread as the owning mongoc_client_t.
36

LIFECYCLE

38       It is an error to free a mongoc_gridfs_t  before  freeing  all  related
39       instances of mongoc_gridfs_file_t and mongoc_gridfs_file_list_t.
40

EXAMPLE

42       example-gridfs.c.INDENT 0.0
43
44          #include <assert.h>
45          #include <mongoc/mongoc.h>
46          #include <stdio.h>
47          #include <stdlib.h>
48          #include <fcntl.h>
49
50          int
51          main (int argc, char *argv[])
52          {
53             mongoc_gridfs_t *gridfs;
54             mongoc_gridfs_file_t *file;
55             mongoc_gridfs_file_list_t *list;
56             mongoc_gridfs_file_opt_t opt = {0};
57             mongoc_client_t *client;
58             const char *uri_string = "mongodb://127.0.0.1:27017/?appname=gridfs-example";
59             mongoc_uri_t *uri;
60             mongoc_stream_t *stream;
61             bson_t filter;
62             bson_t opts;
63             bson_t child;
64             bson_error_t error;
65             ssize_t r;
66             char buf[4096];
67             mongoc_iovec_t iov;
68             const char *filename;
69             const char *command;
70             bson_value_t id;
71
72             if (argc < 2) {
73                fprintf (stderr, "usage - %s command ...\n", argv[0]);
74                return EXIT_FAILURE;
75             }
76
77             mongoc_init ();
78
79             iov.iov_base = (void *) buf;
80             iov.iov_len = sizeof buf;
81
82             /* connect to localhost client */
83             uri = mongoc_uri_new_with_error (uri_string, &error);
84             if (!uri) {
85                fprintf (stderr,
86                         "failed to parse URI: %s\n"
87                         "error message:       %s\n",
88                         uri_string,
89                         error.message);
90                return EXIT_FAILURE;
91             }
92
93             client = mongoc_client_new_from_uri (uri);
94             assert (client);
95             mongoc_client_set_error_api (client, 2);
96
97             /* grab a gridfs handle in test prefixed by fs */
98             gridfs = mongoc_client_get_gridfs (client, "test", "fs", &error);
99             assert (gridfs);
100
101             command = argv[1];
102             filename = argv[2];
103
104             if (strcmp (command, "read") == 0) {
105                if (argc != 3) {
106                   fprintf (stderr, "usage - %s read filename\n", argv[0]);
107                   return EXIT_FAILURE;
108                }
109                file = mongoc_gridfs_find_one_by_filename (gridfs, filename, &error);
110                assert (file);
111
112                stream = mongoc_stream_gridfs_new (file);
113                assert (stream);
114
115                for (;;) {
116                   r = mongoc_stream_readv (stream, &iov, 1, -1, 0);
117
118                   assert (r >= 0);
119
120                   if (r == 0) {
121                      break;
122                   }
123
124                   if (fwrite (iov.iov_base, 1, r, stdout) != r) {
125                      MONGOC_ERROR ("Failed to write to stdout. Exiting.\n");
126                      exit (1);
127                   }
128                }
129
130                mongoc_stream_destroy (stream);
131                mongoc_gridfs_file_destroy (file);
132             } else if (strcmp (command, "list") == 0) {
133                bson_init (&filter);
134
135                bson_init (&opts);
136                bson_append_document_begin (&opts, "sort", -1, &child);
137                BSON_APPEND_INT32 (&child, "filename", 1);
138                bson_append_document_end (&opts, &child);
139
140                list = mongoc_gridfs_find_with_opts (gridfs, &filter, &opts);
141
142                bson_destroy (&filter);
143                bson_destroy (&opts);
144
145                while ((file = mongoc_gridfs_file_list_next (list))) {
146                   const char *name = mongoc_gridfs_file_get_filename (file);
147                   printf ("%s\n", name ? name : "?");
148
149                   mongoc_gridfs_file_destroy (file);
150                }
151
152                mongoc_gridfs_file_list_destroy (list);
153             } else if (strcmp (command, "write") == 0) {
154                if (argc != 4) {
155                   fprintf (stderr, "usage - %s write filename input_file\n", argv[0]);
156                   return EXIT_FAILURE;
157                }
158
159                stream = mongoc_stream_file_new_for_path (argv[3], O_RDONLY, 0);
160                assert (stream);
161
162                opt.filename = filename;
163
164                /* the driver generates a file_id for you */
165                file = mongoc_gridfs_create_file_from_stream (gridfs, stream, &opt);
166                assert (file);
167
168                id.value_type = BSON_TYPE_INT32;
169                id.value.v_int32 = 1;
170
171                /* optional: the following method specifies a file_id of any
172                   BSON type */
173                if (!mongoc_gridfs_file_set_id (file, &id, &error)) {
174                   fprintf (stderr, "%s\n", error.message);
175                   return EXIT_FAILURE;
176                }
177
178                if (!mongoc_gridfs_file_save (file)) {
179                   mongoc_gridfs_file_error (file, &error);
180                   fprintf (stderr, "Could not save: %s\n", error.message);
181                   return EXIT_FAILURE;
182                }
183
184                mongoc_gridfs_file_destroy (file);
185             } else {
186                fprintf (stderr, "Unknown command");
187                return EXIT_FAILURE;
188             }
189
190             mongoc_gridfs_destroy (gridfs);
191             mongoc_uri_destroy (uri);
192             mongoc_client_destroy (client);
193
194             mongoc_cleanup ();
195
196             return EXIT_SUCCESS;
197          }
198
199

AUTHOR

201       MongoDB, Inc
202
204       2017-present, MongoDB, Inc
205
206
207
208
2091.13.1                           Jan 24, 2019               MONGOC_GRIDFS_T(3)
Impressum