Crate qiniu_credential

source ·
Expand description

qiniu-credential

七牛认证信息

负责存储调用七牛 API 所必要的认证信息,提供 CredentialProvider 方便扩展获取认证信息的方式。 同时提供阻塞接口和异步接口(异步接口需要启用 async 功能)。 提供 CredentialProvider 的多个实现方式,例如:

代码示例

计算七牛鉴权签名 V1
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"
    );
计算七牛鉴权签名 V2
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(),
    );
计算下载地址签名
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(())
}

Modules

  • 将所有 Trait 全部重新导出,方便统一导入

Structs

Constants

Traits