Struct mongodb::GridFsUploadStream
source · pub struct GridFsUploadStream { /* private fields */ }
Expand description
A stream to which bytes can be written to be uploaded to a GridFS bucket.
Uploading to the Stream
The GridFsUploadStream
type implements futures_io::AsyncWrite
. It is recommended that
users call the utility methods in AsyncWriteExt
to interact
with the stream.
Bytes can be written to the stream using the write methods in AsyncWriteExt
. When
close
is invoked on the stream, any remaining bytes
in the buffer are written to the chunks collection and a corresponding
FilesCollectionDocument
is written to the files collection. It is an error to write to,
abort, or close the stream after close
has been called.
use futures_util::io::AsyncWriteExt;
let bytes = vec![0u8; 100];
let mut upload_stream = bucket.open_upload_stream("example_file", None);
upload_stream.write_all(&bytes[..]).await?;
upload_stream.close().await?;
Aborting the Stream
A stream can be aborted by calling the abort
method. This will remove any chunks associated
with the stream from the chunks collection. It is an error to write to, abort, or close the
stream after abort
has been called.
use futures_util::io::AsyncWriteExt;
let bytes = vec![0u8; 100];
let mut upload_stream = bucket.open_upload_stream("example_file", None);
upload_stream.write_all(&bytes[..]).await?;
upload_stream.abort().await?;
In the event of an error during any operation on the GridFsUploadStream
, any chunks associated
with the stream will be removed from the chunks collection. Any subsequent attempts to write to,
abort, or close the stream will return an error.
If a GridFsUploadStream
is dropped prior to abort
or close
being called, its Drop
implementation will spawn a task to remove any chunks associated with the stream from the chunks
collection. Users should prefer calling abort
explicitly to relying on the Drop
implementation in order to await
the task and inspect the result of the delete operation.
Flushing the Stream
Because all chunks besides the final chunk of a file must be exactly chunk_size_bytes
, calling
flush
is not guaranteed to flush all bytes to the
chunks collection. Any remaining buffered bytes will be written to the chunks collection upon a
call to close
.
Using tokio::io::AsyncWrite
Users who prefer to use tokio’s AsyncWrite
trait can use the tokio_util::compat
module.
use tokio_util::compat::FuturesAsyncWriteCompatExt;
let futures_upload_stream = bucket.open_upload_stream("example_file", None);
let tokio_upload_stream = futures_upload_stream.compat_write();
Implementations§
source§impl GridFsUploadStream
impl GridFsUploadStream
sourcepub fn id(&self) -> &Bson
pub fn id(&self) -> &Bson
Gets the stream’s unique Bson
identifier. This value will be the id
field for the
FilesCollectionDocument
uploaded to the files collection when the stream is closed.
Trait Implementations§
source§impl AsyncWrite for GridFsUploadStream
impl AsyncWrite for GridFsUploadStream
source§fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize, Error>>
fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize, Error>>
buf
into the object. Read moresource§fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<()>>
Auto Trait Implementations§
impl !RefUnwindSafe for GridFsUploadStream
impl Send for GridFsUploadStream
impl !Sync for GridFsUploadStream
impl Unpin for GridFsUploadStream
impl !UnwindSafe for GridFsUploadStream
Blanket Implementations§
source§impl<W> AsyncWriteExt for Wwhere
W: AsyncWrite + ?Sized,
impl<W> AsyncWriteExt for Wwhere
W: AsyncWrite + ?Sized,
source§fn flush(&mut self) -> Flush<'_, Self>where
Self: Unpin,
fn flush(&mut self) -> Flush<'_, Self>where
Self: Unpin,
AsyncWrite
. Read moresource§fn close(&mut self) -> Close<'_, Self>where
Self: Unpin,
fn close(&mut self) -> Close<'_, Self>where
Self: Unpin,
AsyncWrite
.source§fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self>where
Self: Unpin,
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self>where
Self: Unpin,
buf
into the object. Read moresource§fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectored<'a, Self>where
Self: Unpin,
fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectored<'a, Self>where
Self: Unpin,
bufs
into the object using vectored
IO operations. Read moresource§impl<T> FuturesAsyncWriteCompatExt for Twhere
T: AsyncWrite,
impl<T> FuturesAsyncWriteCompatExt for Twhere
T: AsyncWrite,
source§fn compat_write(self) -> Compat<Self>where
Self: Sized,
fn compat_write(self) -> Compat<Self>where
Self: Sized,
self
with a compatibility layer that implements
tokio::io::AsyncWrite
.