Struct Credential

Source
pub struct Credential { /* private fields */ }
Expand description

认证信息

返回认证信息的 AccessKey 和 SecretKey

Implementations§

Source§

impl Credential

Source

pub fn new( access_key: impl Into<AccessKey>, secret_key: impl Into<SecretKey>, ) -> Self

创建认证信息

Source

pub fn access_key(&self) -> &AccessKey

获取认证信息的 AccessKey

Source

pub fn secret_key(&self) -> &SecretKey

获取认证信息的 SecretKey

Source

pub fn split(self) -> (AccessKey, SecretKey)

同时返回认证信息的 AccessKey 和 SecretKey

Source

pub fn sign(&self, data: &[u8]) -> String

使用七牛签名算法对数据进行签名

参考管理凭证的签名算法文档

use qiniu_credential::{Credential, prelude::*};
let credential = Credential::new("abcdefghklmnopq", "1234567890");
assert_eq!(
    credential.get(Default::default())?.sign(b"hello"),
    "abcdefghklmnopq:b84KVc-LroDiz0ebUANfdzSRxa0="
);
Source

pub fn sign_reader(&self, reader: &mut dyn Read) -> IoResult<String>

使用七牛签名算法对输入流数据进行签名

该方法的异步版本为 Credential::sign_async_reader

参考管理凭证的签名算法文档

use qiniu_credential::{Credential, prelude::*};
use std::io::Cursor;
let credential = Credential::new("abcdefghklmnopq", "1234567890");
assert_eq!(
    credential
        .get(Default::default())?
        .sign_reader(&mut Cursor::new(b"world"))?,
    "abcdefghklmnopq:VjgXt0P_nCxHuaTfiFz-UjDJ1AQ="
);
Source

pub fn sign_with_data(&self, data: &[u8]) -> String

使用七牛签名算法对数据进行签名,并同时给出签名和原数据

参考上传凭证的签名算法文档

use qiniu_credential::{Credential, prelude::*};
use std::io::Cursor;
let credential = Credential::new("abcdefghklmnopq", "1234567890");
assert_eq!(
    credential.get(Default::default())?.sign_with_data(b"hello"),
    "abcdefghklmnopq:BZYt5uVRy1RVt5ZTXbaIt2ROVMA=:aGVsbG8="
);
Source

pub fn authorization_v1_for_request( &self, url: &Uri, content_type: Option<&HeaderValue>, body: &[u8], ) -> String

使用七牛签名算法 V1 对 HTTP 请求(请求体为内存数据)进行签名,返回 Authorization 的值

use qiniu_credential::{Credential, HeaderValue, prelude::*};
use mime::APPLICATION_WWW_FORM_URLENCODED;
use std::io::Cursor;
let credential = Credential::new("abcdefghklmnopq", "1234567890");
let authorization = credential
    .get(Default::default())?
    .authorization_v1_for_request(
        &"http://upload.qiniup.com/".parse()?,
        Some(&HeaderValue::from_str(APPLICATION_WWW_FORM_URLENCODED.as_ref())?),
        b"name=test&language=go"
    );
Source

pub fn authorization_v1_for_request_with_body_reader( &self, url: &Uri, content_type: Option<&HeaderValue>, body: &mut dyn Read, ) -> IoResult<String>

使用七牛签名算法 V1 对 HTTP 请求(请求体为输入流)进行签名,返回 Authorization 的值

该方法的异步版本为 Credential::authorization_v1_for_request_with_async_body_reader

use qiniu_credential::{Credential, HeaderValue, prelude::*};
use std::io::Cursor;
use mime::APPLICATION_WWW_FORM_URLENCODED;
let credential = Credential::new("abcdefghklmnopq", "1234567890");
let authorization = credential
    .get(Default::default())?
    .authorization_v1_for_request_with_body_reader(
        &"http://upload.qiniup.com/".parse()?,
        Some(&HeaderValue::from_str(APPLICATION_WWW_FORM_URLENCODED.as_ref())?),
        &mut Cursor::new(b"name=test&language=go")
    )?;
Source

pub fn authorization_v2_for_request( &self, method: &Method, url: &Uri, headers: &HeaderMap, body: &[u8], ) -> String

使用七牛签名算法 V2 对 HTTP 请求(请求体为内存数据)进行签名,返回 Authorization 的值

use qiniu_credential::{Credential, Method, HeaderMap, HeaderValue, prelude::*};
use http::header::CONTENT_TYPE;
use mime::APPLICATION_JSON;
let credential = Credential::new("abcdefghklmnopq", "1234567890");
let mut headers = HeaderMap::new();
headers.insert(CONTENT_TYPE, HeaderValue::from_str(APPLICATION_JSON.as_ref())?);
let authorization = credential
    .get(Default::default())?
    .authorization_v2_for_request(
        &Method::GET,
        &"http://upload.qiniup.com/".parse()?,
        &headers,
        b"{\"name\":\"test\"}".as_slice(),
    );
Source

pub fn authorization_v2_for_request_with_body_reader( &self, method: &Method, url: &Uri, headers: &HeaderMap, body: &mut dyn Read, ) -> IoResult<String>

使用七牛签名算法 V2 对 HTTP 请求(请求体为输入流)进行签名,返回 Authorization 的值

该方法的异步版本为 Credential::authorization_v2_for_request_with_async_body_reader

use qiniu_credential::{Credential, Method, HeaderMap, HeaderValue, prelude::*};
use http::header::CONTENT_TYPE;
use mime::APPLICATION_JSON;
use std::io::Cursor;
let credential = Credential::new("abcdefghklmnopq", "1234567890");
let mut headers = HeaderMap::new();
headers.insert(CONTENT_TYPE, HeaderValue::from_str(APPLICATION_JSON.as_ref())?);
let authorization = credential
    .get(Default::default())?
    .authorization_v2_for_request_with_body_reader(
        &Method::GET,
        &"http://upload.qiniup.com/".parse()?,
        &headers,
        &mut Cursor::new(b"{\"name\":\"test\"}")
    )?;
Source

pub fn sign_download_url(&self, url: Uri, lifetime: Duration) -> Uri

对对象的下载 URL 签名,可以生成私有存储空间的下载地址

use qiniu_credential::{Credential, prelude::*};
use std::time::Duration;
let credential = Credential::new("abcdefghklmnopq", "1234567890");
let url = "http://www.qiniu.com/?go=1".parse()?;
let url = credential
    .get(Default::default())?
    .sign_download_url(url, Duration::from_secs(3600));
println!("{}", url);
Ok(())
}
Source§

impl Credential

Source

pub async fn sign_async_reader( &self, reader: &mut (dyn AsyncRead + Send + Unpin), ) -> IoResult<String>

Available on crate feature async only.

使用七牛签名算法对异步输入流数据进行签名

参考管理凭证的签名算法文档

use qiniu_credential::{Credential, prelude::*};
use futures_lite::io::Cursor;
let credential = Credential::new("abcdefghklmnopq", "1234567890");
assert_eq!(
    credential
        .async_get(Default::default()).await?
        .sign_async_reader(&mut Cursor::new(b"world")).await?,
    "abcdefghklmnopq:VjgXt0P_nCxHuaTfiFz-UjDJ1AQ="
);
Source

pub async fn authorization_v1_for_request_with_async_body_reader( &self, url: &Uri, content_type: Option<&HeaderValue>, body: &mut (dyn AsyncRead + Send + Unpin), ) -> IoResult<String>

Available on crate feature async only.

使用七牛签名算法 V1 对 HTTP 请求(请求体为异步输入流)进行签名,返回 Authorization 的值

use qiniu_credential::{Credential, HeaderValue, prelude::*};
use mime::APPLICATION_WWW_FORM_URLENCODED;
use futures_lite::io::Cursor;
let credential = Credential::new("abcdefghklmnopq", "1234567890");
let authorization = credential
    .async_get(Default::default()).await?
    .authorization_v1_for_request_with_async_body_reader(
        &"http://upload.qiniup.com/".parse()?,
        Some(&HeaderValue::from_str(APPLICATION_WWW_FORM_URLENCODED.as_ref())?),
        &mut Cursor::new(b"name=test&language=go")
    ).await?;
Source

pub async fn authorization_v2_for_request_with_async_body_reader( &self, method: &Method, url: &Uri, headers: &HeaderMap, body: &mut (dyn AsyncRead + Send + Unpin), ) -> IoResult<String>

Available on crate feature async only.

使用七牛签名算法 V2 对 HTTP 请求(请求体为异步输入流)进行签名,返回 Authorization 的值

use qiniu_credential::{Credential, Method, HeaderMap, HeaderValue, prelude::*};
use http::header::CONTENT_TYPE;
use mime::APPLICATION_JSON;
use futures_lite::io::Cursor;
#[async_std::main]
let credential = Credential::new("abcdefghklmnopq", "1234567890");
let mut headers = HeaderMap::new();
headers.insert(CONTENT_TYPE, HeaderValue::from_str(APPLICATION_JSON.as_ref())?);
let authorization = credential
    .async_get(Default::default()).await?
    .authorization_v2_for_request_with_async_body_reader(
        &Method::GET,
        &"http://upload.qiniup.com/".parse()?,
        &headers,
        &mut Cursor::new(b"{\"name\":\"test\"}")
    ).await?;

Trait Implementations§

Source§

impl Clone for Credential

Source§

fn clone(&self) -> Credential

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl CredentialProvider for Credential

Source§

fn get(&self, _opts: GetOptions) -> IoResult<GotCredential>

返回七牛认证信息 Read more
Source§

fn async_get( &self, opts: GetOptions, ) -> Pin<Box<dyn Future<Output = Result<GotCredential>> + Send + '_>>

Available on crate feature async only.
异步返回七牛认证信息
Source§

impl Debug for Credential

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<Credential> for GotCredential

Source§

fn from(credential: Credential) -> Self

Converts to this type from the input type.
Source§

impl From<GotCredential> for Credential

Source§

fn from(result: GotCredential) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Credential

Source§

fn eq(&self, other: &Credential) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for Credential

Source§

impl StructuralPartialEq for Credential

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.