1MONGOC_GRIDFS_BUCKET_T(3)          libmongoc         MONGOC_GRIDFS_BUCKET_T(3)
2
3
4

NAME

6       mongoc_gridfs_bucket_t - mongoc_gridfs_bucket_t
7

SYNOPSIS

9          #include <mongoc/mongoc.h>
10
11          typedef struct _mongoc_gridfs_bucket_t mongoc_gridfs_bucket_t;
12

DESCRIPTION

14       mongoc_gridfs_bucket_t  provides a spec-compliant MongoDB GridFS imple‐
15       mentation, superseding mongoc_gridfs_t. See the GridFS MongoDB documen‐
16       tation.
17

THREAD SAFETY

19       mongoc_gridfs_bucket_t  is  NOT  thread-safe and should only be used in
20       the same thread as the owning mongoc_client_t.
21

LIFECYCLE

23       It is an error to free  a  mongoc_gridfs_bucket_t  before  freeing  all
24       derived  instances  of mongoc_stream_t. The owning mongoc_client_t must
25       outlive the mongoc_gridfs_bucket_t.
26

EXAMPLE

28       example-gridfs-bucket.c.INDENT 0.0
29
30          #include <stdio.h>
31          #include <stdlib.h>
32
33          #include <mongoc/mongoc.h>
34
35          int
36          main (int argc, char *argv[])
37          {
38             const char *uri_string =
39                "mongodb://localhost:27017/?appname=new-gridfs-example";
40             mongoc_client_t *client;
41             mongoc_database_t *db;
42             mongoc_stream_t *file_stream;
43             mongoc_gridfs_bucket_t *bucket;
44             mongoc_cursor_t *cursor;
45             bson_t filter;
46             bool res;
47             bson_value_t file_id;
48             bson_error_t error;
49             const bson_t *doc;
50             char *str;
51             mongoc_init ();
52
53             if (argc != 3) {
54                fprintf (stderr, "usage: %s SOURCE_FILE_PATH FILE_COPY_PATH\n", argv[0]);
55                return EXIT_FAILURE;
56             }
57
58             /* 1. Make a bucket. */
59             client = mongoc_client_new (uri_string);
60             db = mongoc_client_get_database (client, "test");
61             bucket = mongoc_gridfs_bucket_new (db, NULL, NULL, &error);
62             if (!bucket) {
63                printf ("Error creating gridfs bucket: %s\n", error.message);
64                return EXIT_FAILURE;
65             }
66
67             /* 2. Insert a file.  */
68             file_stream = mongoc_stream_file_new_for_path (argv[1], O_RDONLY, 0);
69             res = mongoc_gridfs_bucket_upload_from_stream (
70                bucket, "my-file", file_stream, NULL, &file_id, &error);
71             if (!res) {
72                printf ("Error uploading file: %s\n", error.message);
73                return EXIT_FAILURE;
74             }
75
76             mongoc_stream_close (file_stream);
77             mongoc_stream_destroy (file_stream);
78
79             /* 3. Download the file in GridFS to a local file. */
80             file_stream = mongoc_stream_file_new_for_path (argv[2], O_CREAT | O_RDWR, 0);
81             if (!file_stream) {
82                perror ("Error opening file stream");
83                return EXIT_FAILURE;
84             }
85
86             res = mongoc_gridfs_bucket_download_to_stream (
87                bucket, &file_id, file_stream, &error);
88             if (!res) {
89                printf ("Error downloading file to stream: %s\n", error.message);
90                return EXIT_FAILURE;
91             }
92             mongoc_stream_close (file_stream);
93             mongoc_stream_destroy (file_stream);
94
95             /* 4. List what files are available in GridFS. */
96             bson_init (&filter);
97             cursor = mongoc_gridfs_bucket_find (bucket, &filter, NULL);
98
99             while (mongoc_cursor_next (cursor, &doc)) {
100                str = bson_as_canonical_extended_json (doc, NULL);
101                printf ("%s\n", str);
102                bson_free (str);
103             }
104
105             /* 5. Delete the file that we added. */
106             res = mongoc_gridfs_bucket_delete_by_id (bucket, &file_id, &error);
107             if (!res) {
108                printf ("Error deleting the file: %s\n", error.message);
109                return EXIT_FAILURE;
110             }
111
112             /* 6. Cleanup. */
113             mongoc_stream_close (file_stream);
114             mongoc_stream_destroy (file_stream);
115             mongoc_cursor_destroy (cursor);
116             bson_destroy (&filter);
117             mongoc_gridfs_bucket_destroy (bucket);
118             mongoc_database_destroy (db);
119             mongoc_client_destroy (client);
120             mongoc_cleanup ();
121
122             return EXIT_SUCCESS;
123          }
124

SEE ALSO

126       · The MongoDB GridFS specification.
127
128       · The non spec-compliant mongoc_gridfs_t.
129

AUTHOR

131       MongoDB, Inc
132
134       2017-present, MongoDB, Inc
135
136
137
138
1391.16.2                           Feb 25, 2020        MONGOC_GRIDFS_BUCKET_T(3)
Impressum