Struct mongodb_gridfs::bucket::GridFSBucket
source · pub struct GridFSBucket { /* private fields */ }
Expand description
GridFS bucket. A prefix under which a GridFS system’s collections are stored. Spec
Implementations§
source§impl GridFSBucket
impl GridFSBucket
sourcepub async fn delete(&self, id: ObjectId) -> Result<(), GridFSError>
pub async fn delete(&self, id: ObjectId) -> Result<(), GridFSError>
Given a @id, delete this stored file’s files collection document and associated chunks from a GridFS bucket. Spec
use mongodb_gridfs::{GridFSBucket, GridFSError};
let bucket = GridFSBucket::new(db.clone(), Some(GridFSBucketOptions::default()));
bucket.delete(id).await?;
Errors
Raise GridFSError::FileNotFound
when the requested id doesn’t exists.
source§impl GridFSBucket
impl GridFSBucket
sourcepub async fn open_download_stream_with_filename(
&self,
id: ObjectId
) -> Result<(impl Stream<Item = Vec<u8>>, String), GridFSError>
pub async fn open_download_stream_with_filename( &self, id: ObjectId ) -> Result<(impl Stream<Item = Vec<u8>>, String), GridFSError>
Opens a Stream from which the application can read the contents of the stored file specified by @id. Spec
Returns a [Stream
].
Examples
use tokio_stream::StreamExt;
use mongodb_gridfs::{options::GridFSBucketOptions, GridFSBucket, GridFSError};
let bucket = GridFSBucket::new(db.clone(), Some(GridFSBucketOptions::default()));
let (mut cursor, filename) = bucket.open_download_stream_with_filename(id).await?;
assert_eq!(filename, "test.txt");
let buffer = cursor.next().await.unwrap();
Errors
Raise GridFSError::FileNotFound
when the requested id doesn’t exists.
sourcepub async fn open_download_stream(
&self,
id: ObjectId
) -> Result<impl Stream<Item = Vec<u8>>, GridFSError>
pub async fn open_download_stream( &self, id: ObjectId ) -> Result<impl Stream<Item = Vec<u8>>, GridFSError>
Opens a Stream from which the application can read the contents of the stored file specified by @id. Spec
Returns a [Stream
].
Examples
use tokio_stream::StreamExt;
use mongodb_gridfs::{options::GridFSBucketOptions, GridFSBucket, GridFSError};
let bucket = GridFSBucket::new(db.clone(), Some(GridFSBucketOptions::default()));
let mut cursor = bucket.open_download_stream(id).await?;
let buffer = cursor.next().await.unwrap();
Errors
Raise GridFSError::FileNotFound
when the requested id doesn’t exists.
source§impl GridFSBucket
impl GridFSBucket
source§impl GridFSBucket
impl GridFSBucket
sourcepub async fn find(
&self,
filter: Document,
options: GridFSFindOptions
) -> Result<Cursor<Document>>
pub async fn find( &self, filter: Document, options: GridFSFindOptions ) -> Result<Cursor<Document>>
Find and return the files collection documents that match @filter. Spec
Examples
use bson::doc;
use tokio_stream::StreamExt;
use mongodb_gridfs::{bucket::GridFSBucket, options::GridFSFindOptions};
let mut cursor = bucket
.find(doc! {"filename":"test.txt"}, GridFSFindOptions::default())
.await?;
while let Some(_doc) = cursor.next().await {
// ...
}
source§impl GridFSBucket
impl GridFSBucket
source§impl GridFSBucket
impl GridFSBucket
sourcepub async fn upload_from_stream<'a>(
&mut self,
filename: &str,
source: impl AsyncRead + Unpin,
options: Option<GridFSUploadOptions>
) -> Result<ObjectId, Error>
pub async fn upload_from_stream<'a>( &mut self, filename: &str, source: impl AsyncRead + Unpin, options: Option<GridFSUploadOptions> ) -> Result<ObjectId, Error>
Uploads a user file to a GridFS bucket. The driver generates the file id.
Reads the contents of the user file from the @source Stream and uploads it as chunks in the chunks collection. After all the chunks have been uploaded, it creates a files collection document for @filename in the files collection.
Returns the id of the uploaded file.
Note: this method is provided for backward compatibility. In languages that use generic type parameters, this method may be omitted since the TFileId type might not be an ObjectId.
Examples
use mongodb_gridfs::{options::GridFSBucketOptions, GridFSBucket};
let mut bucket = GridFSBucket::new(db.clone(), Some(GridFSBucketOptions::default()));
let id = bucket
.upload_from_stream("test.txt", "stream your data here".as_bytes(), None)
.await?;
source§impl GridFSBucket
impl GridFSBucket
sourcepub fn new(db: Database, options: Option<GridFSBucketOptions>) -> GridFSBucket
pub fn new(db: Database, options: Option<GridFSBucketOptions>) -> GridFSBucket
Create a new GridFSBucket object on @db with the given @options.
Trait Implementations§
source§impl Clone for GridFSBucket
impl Clone for GridFSBucket
source§fn clone(&self) -> GridFSBucket
fn clone(&self) -> GridFSBucket
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more