pub struct DownloadingObject { /* private fields */ }
Expand description
准备下载的对象
可以在下载前设置范围参数或回调函数,以及写入数据的目标。
需要注意的是,生成该对象并不表示数据处于下载状态了,
下载将在调用 Self::to_path
,Self::to_writer
,DownloadingObjectReader::read
以后才正式开始。
Implementations§
Source§impl DownloadingObject
impl DownloadingObject
Sourcepub fn range_from(self, range_from: NonZeroU64) -> Self
pub fn range_from(self, range_from: NonZeroU64) -> Self
设置下载范围起始位置
单位为字节,如果不调用,默认从第一个字节开始下载
Sourcepub fn range_to(self, range_to: NonZeroU64) -> Self
pub fn range_to(self, range_to: NonZeroU64) -> Self
设置下载范围结束位置,包含该位置
例如如果要下载前 500 个字节,则调用该方法时应该传入 499
单位为字节,如果不调用,默认下载到最后一个字节
Sourcepub fn retrier(self, retrier: impl DownloadRetrier + 'static) -> Self
pub fn retrier(self, retrier: impl DownloadRetrier + 'static) -> Self
设置下载重试器
默认使用 ErrorRetrier
Sourcepub fn set_header(
self,
header_name: impl IntoHeaderName,
header_value: impl Into<HeaderValue>,
) -> Self
pub fn set_header( self, header_name: impl IntoHeaderName, header_value: impl Into<HeaderValue>, ) -> Self
添加 HTTP 请求头
Sourcepub fn on_before_request<F>(self, callback: F) -> Self
pub fn on_before_request<F>(self, callback: F) -> Self
设置请求前的回调函数
Sourcepub fn on_download_progress<F: Fn(DownloadingProgressInfo) -> AnyResult<()> + Send + Sync + 'static>(
self,
callback: F,
) -> Self
pub fn on_download_progress<F: Fn(DownloadingProgressInfo) -> AnyResult<()> + Send + Sync + 'static>( self, callback: F, ) -> Self
设置下载进度回调函数
Sourcepub fn on_response_ok<F: Fn(&mut HttpResponseParts) -> AnyResult<()> + Send + Sync + 'static>(
self,
callback: F,
) -> Self
pub fn on_response_ok<F: Fn(&mut HttpResponseParts) -> AnyResult<()> + Send + Sync + 'static>( self, callback: F, ) -> Self
设置响应成功的回调函数
Sourcepub fn on_response_error<F: Fn(&mut ResponseError) -> AnyResult<()> + Send + Sync + 'static>(
self,
callback: F,
) -> Self
pub fn on_response_error<F: Fn(&mut ResponseError) -> AnyResult<()> + Send + Sync + 'static>( self, callback: F, ) -> Self
设置响应错误的回调函数
Sourcepub fn to_path(self, path: impl AsRef<Path>) -> DownloadResult<()>
pub fn to_path(self, path: impl AsRef<Path>) -> DownloadResult<()>
将下载的对象内容写入指定的文件系统路径
需要注意,如果文件已经存在,则会覆盖该文件,如果文件不存在,则会创建该文件。
该方法的异步版本为 Self::async_to_path
。
§代码示例
download_manager
.download(object_name)?
.to_path("/home/qiniu/test.png")?;
Sourcepub async fn async_to_path(self, path: impl AsRef<Path>) -> DownloadResult<()>
Available on crate feature async
only.
pub async fn async_to_path(self, path: impl AsRef<Path>) -> DownloadResult<()>
async
only.将下载的对象内容异步写入指定的文件系统路径
需要注意,如果文件已经存在,则会覆盖该文件,如果文件不存在,则会创建该文件。
§代码示例
download_manager
.async_download(object_name)
.await?
.async_to_path("/home/qiniu/test.png")
.await?;
Sourcepub fn to_writer(self, writer: &mut dyn Write) -> DownloadResult<()>
pub fn to_writer(self, writer: &mut dyn Write) -> DownloadResult<()>
将下载的对象内容写入指定的输出流
该方法的异步版本为 Self::to_async_writer
。
§代码示例
let mut buf = Vec::new();
download_manager
.download(object_name)?
.to_writer(&mut buf)?;
Sourcepub async fn to_async_writer(
self,
writer: &mut (dyn AsyncWrite + Send + Sync + Unpin),
) -> DownloadResult<()>
Available on crate feature async
only.
pub async fn to_async_writer( self, writer: &mut (dyn AsyncWrite + Send + Sync + Unpin), ) -> DownloadResult<()>
async
only.将下载的对象内容写入指定的异步输出流
§代码示例
let mut buf = Vec::new();
download_manager
.async_download(object_name)
.await?
.to_async_writer(&mut buf)
.await?;
Sourcepub fn into_read(self) -> DownloadingObjectReader ⓘ
pub fn into_read(self) -> DownloadingObjectReader ⓘ
将下载的对象内容包装成 Read
对象
但注意,由于 Read
接口的限制,错误信息会被 IoError
封装
该方法的异步版本为 Self::into_async_read
。
§代码示例
let mut buf = Vec::new();
let mut reader = download_manager
.download(object_name)?
.into_read();
std::io::copy(&mut reader, &mut buf)?;
Sourcepub fn into_async_read(self) -> AsyncDownloadingObjectReader
Available on crate feature async
only.
pub fn into_async_read(self) -> AsyncDownloadingObjectReader
async
only.将下载的对象内容包装成 futures::AsyncRead
对象
但注意,由于 futures::AsyncRead
接口的限制,错误信息会被 IoError
封装
§代码示例
let mut buf = Vec::new();
let mut reader = download_manager
.async_download(object_name)
.await?
.into_async_read();
futures::io::copy(&mut reader, &mut buf).await?;
Trait Implementations§
Auto Trait Implementations§
impl Freeze for DownloadingObject
impl !RefUnwindSafe for DownloadingObject
impl Send for DownloadingObject
impl Sync for DownloadingObject
impl Unpin for DownloadingObject
impl !UnwindSafe for DownloadingObject
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Pipes by value. This is generally the method you want to use. Read more
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
Borrows
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
Mutably borrows
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
Borrows
self
, then passes self.as_ref()
into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
Mutably borrows
self
, then passes self.as_mut()
into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
Borrows
self
, then passes self.deref()
into the pipe function.Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Immutable access to the
Borrow<B>
of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
Mutable access to the
BorrowMut<B>
of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
Immutable access to the
AsRef<R>
view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
Mutable access to the
AsMut<R>
view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Immutable access to the
Deref::Target
of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Mutable access to the
Deref::Target
of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
Calls
.tap()
only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
Calls
.tap_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
Calls
.tap_borrow()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
Calls
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
Calls
.tap_ref()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
Calls
.tap_ref_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
Calls
.tap_deref()
only in debug builds, and is erased in release
builds.