.\" Man page generated from reStructuredText. . .TH "MONGOC_GRIDFS_BUCKET_T" "3" "Jun 04, 2021" "1.17.6" "libmongoc" .SH NAME mongoc_gridfs_bucket_t \- mongoc_gridfs_bucket_t . .nr rst2man-indent-level 0 . .de1 rstReportMargin \\$1 \\n[an-margin] level \\n[rst2man-indent-level] level margin: \\n[rst2man-indent\\n[rst2man-indent-level]] - \\n[rst2man-indent0] \\n[rst2man-indent1] \\n[rst2man-indent2] .. .de1 INDENT .\" .rstReportMargin pre: . RS \\$1 . nr rst2man-indent\\n[rst2man-indent-level] \\n[an-margin] . nr rst2man-indent-level +1 .\" .rstReportMargin post: .. .de UNINDENT . RE .\" indent \\n[an-margin] .\" old: \\n[rst2man-indent\\n[rst2man-indent-level]] .nr rst2man-indent-level -1 .\" new: \\n[rst2man-indent\\n[rst2man-indent-level]] .in \\n[rst2man-indent\\n[rst2man-indent-level]]u .. .SH SYNOPSIS .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C #include typedef struct _mongoc_gridfs_bucket_t mongoc_gridfs_bucket_t; .ft P .fi .UNINDENT .UNINDENT .SH DESCRIPTION .sp \fBmongoc_gridfs_bucket_t\fP provides a spec\-compliant MongoDB GridFS implementation, superseding \fBmongoc_gridfs_t\fP\&. See the \fI\%GridFS MongoDB documentation\fP\&. .SH THREAD SAFETY .sp \fBmongoc_gridfs_bucket_t\fP is NOT thread\-safe and should only be used in the same thread as the owning \fBmongoc_client_t\fP\&. .SH LIFECYCLE .sp It is an error to free a \fBmongoc_gridfs_bucket_t\fP before freeing all derived instances of \fBmongoc_stream_t\fP\&. The owning \fBmongoc_client_t\fP must outlive the \fBmongoc_gridfs_bucket_t\fP\&. .SH EXAMPLE .sp example\-gridfs\-bucket.c .INDENT 0.0 .INDENT 3.5 .sp .nf .ft C #include #include #include int main (int argc, char *argv[]) { const char *uri_string = "mongodb://localhost:27017/?appname=new\-gridfs\-example"; mongoc_client_t *client; mongoc_database_t *db; mongoc_stream_t *file_stream; mongoc_gridfs_bucket_t *bucket; mongoc_cursor_t *cursor; bson_t filter; bool res; bson_value_t file_id; bson_error_t error; const bson_t *doc; char *str; mongoc_init (); if (argc != 3) { fprintf (stderr, "usage: %s SOURCE_FILE_PATH FILE_COPY_PATH\en", argv[0]); return EXIT_FAILURE; } /* 1. Make a bucket. */ client = mongoc_client_new (uri_string); db = mongoc_client_get_database (client, "test"); bucket = mongoc_gridfs_bucket_new (db, NULL, NULL, &error); if (!bucket) { printf ("Error creating gridfs bucket: %s\en", error.message); return EXIT_FAILURE; } /* 2. Insert a file. */ file_stream = mongoc_stream_file_new_for_path (argv[1], O_RDONLY, 0); res = mongoc_gridfs_bucket_upload_from_stream ( bucket, "my\-file", file_stream, NULL, &file_id, &error); if (!res) { printf ("Error uploading file: %s\en", error.message); return EXIT_FAILURE; } mongoc_stream_close (file_stream); mongoc_stream_destroy (file_stream); /* 3. Download the file in GridFS to a local file. */ file_stream = mongoc_stream_file_new_for_path (argv[2], O_CREAT | O_RDWR, 0); if (!file_stream) { perror ("Error opening file stream"); return EXIT_FAILURE; } res = mongoc_gridfs_bucket_download_to_stream ( bucket, &file_id, file_stream, &error); if (!res) { printf ("Error downloading file to stream: %s\en", error.message); return EXIT_FAILURE; } mongoc_stream_close (file_stream); mongoc_stream_destroy (file_stream); /* 4. List what files are available in GridFS. */ bson_init (&filter); cursor = mongoc_gridfs_bucket_find (bucket, &filter, NULL); while (mongoc_cursor_next (cursor, &doc)) { str = bson_as_canonical_extended_json (doc, NULL); printf ("%s\en", str); bson_free (str); } /* 5. Delete the file that we added. */ res = mongoc_gridfs_bucket_delete_by_id (bucket, &file_id, &error); if (!res) { printf ("Error deleting the file: %s\en", error.message); return EXIT_FAILURE; } /* 6. Cleanup. */ mongoc_stream_close (file_stream); mongoc_stream_destroy (file_stream); mongoc_cursor_destroy (cursor); bson_destroy (&filter); mongoc_gridfs_bucket_destroy (bucket); mongoc_database_destroy (db); mongoc_client_destroy (client); mongoc_cleanup (); return EXIT_SUCCESS; } .ft P .fi .UNINDENT .UNINDENT .SH SEE ALSO .INDENT 0.0 .IP \(bu 2 The \fI\%MongoDB GridFS specification\fP\&. .IP \(bu 2 The non spec\-compliant \fBmongoc_gridfs_t\fP\&. .UNINDENT .SH AUTHOR MongoDB, Inc .SH COPYRIGHT 2017-present, MongoDB, Inc .\" Generated by docutils manpage writer. .