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

SYNOPSIS

6          #include <mongoc/mongoc.h>
7
8          typedef struct _mongoc_gridfs_bucket_t mongoc_gridfs_bucket_t;
9

DESCRIPTION

11       mongoc_gridfs_bucket_t  provides a spec-compliant MongoDB GridFS imple‐
12       mentation, superseding mongoc_gridfs_t. See the GridFS MongoDB documen‐
13       tation.
14

THREAD SAFETY

16       mongoc_gridfs_bucket_t  is  NOT  thread-safe and should only be used in
17       the same thread as the owning mongoc_client_t.
18

LIFECYCLE

20       It is an error to free a mongoc_gridfs_bucket_t before freeing all  de‐
21       rived  instances  of  mongoc_stream_t.  The owning mongoc_client_t must
22       outlive the mongoc_gridfs_bucket_t.
23

EXAMPLE

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

AUTHOR

130       MongoDB, Inc
131
133       2017-present, MongoDB, Inc
134
135
136
137
1381.25.1                           Nov 08, 2023        MONGOC_GRIDFS_BUCKET_T(3)
Impressum