You need to enable JavaScript to run this app.
文档中心
对象存储

对象存储

复制全文
下载 pdf
Rust
初始化客户端(Rust SDK)
复制全文
下载 pdf
初始化客户端(Rust SDK)
本文介绍初始化客户端的示例代码及常见配置场景。​
前提条件​
配置长期访问凭证​
初始化 SDK 前,您需要先配置长期访问凭证。本章节介绍不同操作系统下配置长期访问凭证的操作步骤。​
​
macOS
Linux
Windows
​
  1. 打开终端并执行以下命令打开文件。​
​
nano ~/.bash_profile​
​
  1. 在文件末尾添加 AKSK 信息。​
​
export TOS_ACCESS_KEY=AKTPYmI1Z****​
export TOS_SECRET_KEY=T1dJM01UU****​
​
  1. 保存文件并退出。​
  1. 执行以下命令生效配置信息。​
​
source ~/.bash_profile​
​
  1. 执行以下命令验证配置信息。​
​
echo $TOS_ACCESS_KEY​
echo $TOS_SECRET_KEY​
​
如果配置成功,则返回如下示例:​
​
AKTPYmI1Z****​
T1dJM01UU****​
​
​
创建 TosClient​
初始化 TosClient 实例之后,您才可以向 TOS 服务发送 HTTP/HTTPS 请求。本文介绍 Rust SDK 的初始化方式。​
TOS Rust 客户端提供了一系列接口用来与 TOS 服务进行交互,用来管理桶和对象等 TOS 上的资源。初始化客户端时,需要带上 AccessKey ID、AccessKey Secret 和 Region 等信息。在初始化时,您可以设置建立连接超时时间、单次请求超时时间、超时重试次数和最大空闲连接数量等可选参数。​
​
use async_trait::async_trait;​
use futures_core::future::BoxFuture;​
use std::env;​
use std::future::Future;​
use std::time::Duration;​
use tokio::runtime::Handle;​
use ve_tos_rust_sdk::asynchronous::bucket::BucketAPI;​
use ve_tos_rust_sdk::asynchronous::tos;​
use ve_tos_rust_sdk::asynchronous::tos::AsyncRuntime;​
use ve_tos_rust_sdk::bucket::ListBucketsInput;​
use ve_tos_rust_sdk::common::init_tracing_log;​
use ve_tos_rust_sdk::error::TosError;​
​
#[derive(Debug, Default)]​
pub struct TokioRuntime {}​
​
#[async_trait]​
impl AsyncRuntime for TokioRuntime {​
type JoinError = tokio::task::JoinError;​
async fn sleep(&self, duration: Duration) {​
tokio::time::sleep(duration).await;​
}​
​
fn spawn<'a, F>(&self, future: F) -> BoxFuture<'a, Result<F::Output, Self::JoinError>>​
where​
F: Future + Send + 'static,​
F::Output: Send + 'static,​
{​
Box::pin(Handle::current().spawn(future))​
}​
​
fn block_on<F: Future>(&self, future: F) -> F::Output {​
Handle::current().block_on(future)​
}​
}​
​
#[tokio::main]​
async fn main() {​
// init log​
let _guard = init_tracing_log("info", "temp/logs", "app.log");​
​
let ak = env::var("TOS_ACCESS_KEY").unwrap_or("".to_string());​
let sk = env::var("TOS_SECRET_KEY").unwrap_or("".to_string());​
let endpoint = "https://tos-cn-beijing.volces.com";​
let region = "cn-beijing";​
let tos_client = tos::builder::<TokioRuntime>()​
.connection_timeout(3000)​
.request_timeout(10000)​
.max_connections(1024)​
.max_retry_count(3)​
.ak(ak)​
.sk(sk)​
.region(region)​
.endpoint(endpoint)​
.build()​
.unwrap();​
​
let output = tos_client.list_buckets(&ListBucketsInput::new()).await;​
match output {​
Ok(output) => {​
println!("request_id: {}", output.request_id());​
for bucket in output.buckets() {​
println!("{}", bucket.name());​
}​
}​
Err(err) => {​
match err {​
TosError::TosClientError { message, .. } => {​
println!("message: {}", message);​
}​
TosError::TosServerError { request_id, ec, .. } => {​
println!("request_id: {}, ec: {}", request_id, ec);​
}​
}​
}​
}​
}​
​
初始化参数列表​
初始化 TosClient 支持参数如下。​
​
参数​
参数类型​
是否必选​
示例值​
说明​
ak​
String​
可选​
AKTPYmI1ZGQwMDA0NjlhNGFkMzhjNzM0N2Q0OTQ3ZTV****​
Access Key ID,即密钥 ID。​
sk​
String​
可选​
T1dJM01UUXpOak0wTVdWak5EUmtOR0poWldJNU1HWmxaV1V5TkdReVl6****​
SecretAccess Key,即私有访问密钥。​
endpoint​
String​
可选​
https://tos-cn-beijing.volces.com​
访问域名。​
region​
String​
必选​
cn-beijing​
TOS 服务端所在地域。完整格式:https://{host}:{port}。​
security_token​
String​
可选​
STSeyJBY2NvdW50SWQiOjIxMDAwMDUyMjQsIklkZW50aXR5VHlwZSI6NCwiSWRlbnRpdHlJZCI6MTE3MjI5NiwiQ2hhbm5lbCI6IlVzZXIiLCJBY2Nlc3NLZXlJZCI6IkFLVFBZbUkxWkdRd01EQTBOamxoTkdGa016aGpOek0wTjJRME9UUTNaVFZqTXpFIiwiU2l************​
临时访问凭证中的安全令牌。​
control_endpoint​
String​
可选​
https://tos-control-cn-beijing.volces.com​
TOS 服务端控制面域名,完整格式:https://{host}:{port}。​
enable_verify_ssl​
bool​
可选​
true​
配置是否开启 SSL 证书校验,默认值为 true。​
auto_recognize_content_type​
bool​
可选​
true​
自动识别 MIME 类型,默认值为 true。​
request_timeout​
isize​
可选​
120000​
一次 HTTP 请求总超时时间,单位:毫秒,默认 120000ms。​
connection_timeout​
isize​
可选​
10000​
设置建立连接超时时间,单位:毫秒,默认值为 10000ms。​
max_connections​
isize​
可选​
1024​
连接池中允许打开的最大 HTTP 连接数,默认值为 1024。​
idle_connection_time​
isize​
可选​
60000​
连接池中空闲 HTTP 连接时间超过此参数的设定值,则关闭 HTTP 连接,单位:毫秒,默认 60000ms。​
max_retry_count​
isize​
可选​
3​
设置最大重试次数,默认值为 3。​
enable_crc​
bool​
可选​
true​
设置开启 CRC 校验,默认值为 true。​
is_custom_domain​
bool​
可选​
false​
是否将桶名拼接到 Endpoint 前,通过虚拟主机域名的方式访问 TOS,取值说明如下:​
  • true:不拼接。​
  • false:拼接。​
默认值为 false。​
说明​
通过自定义域名访问 TOS 时,需要设置 isCustomDomain 为 true。​
proxy_host​
String​
可选​
http://your-proxy.com​
代理服务器的主机地址,当前只支持 http 协议。​
proxy_port​
isize​
可选​
8080​
代理服务器的端口号。​
proxy_username​
String​
可选​
username​
连接代理服务器时使用的用户名。​
proxy_password​
String​
可选​
password​
连接代理服务器时使用的用户密码。​
disable_encoding_meta​
bool​
可选​
true​
是否进行对用户自定义元数据(x-tos-meta-*)、Content-Disposition 的编码,为 true 时不进行编码,默认为 false。​
expect_100_continue_threshold​
isize​
可选​
65536​
大于 0 时,代表上传对象相关接口对于待上传的数据长度大于该阈值的请求(无法预测数据长度的情况统一判定为大于该阈值)开启 100-Continue 机制,单位:字节,默认为 65536。​
high_latency_log_threshold​
isize​
可选​
100​
大于 0 时,代表开启高延迟日志,单位:KB,默认为 100,当单次请求传输总速率低于该值且总请求耗时大于 500 毫秒时打印 WARN 级别日志。​
user_agent_product_name​
String​
可选​
your-product-name​
User-Agent 扩展规范中的业务方/产品名。​
user_agent_soft_name​
String​
可选​
your-soft-name​
User-Agent 扩展规范中的软件名。​
user_agent_soft_version​
String​
可选​
v1.0.0​
User-Agent 扩展规范中的软件版本号。​
user_agent_customized_key_values​
HashMap<String, String>​
可选​
-​
User-Agent 扩展规范中的自定义扩展 KV 键值对。​
follow_redirect_times​
isize​
可选​
3​
Get/Head 请求跟随 3xx 重定向的最大次数,默认为 0,代表不跟随重定向。​
client_crt​
String​
可选​
-​
客户端 SSL 证书文件路径。​
client_key​
String​
可选​
-​
客户端 SSL 密钥文件路径。​
ca_crt​
String​
可选​
-​
服务端自定义根证书文件路径。​
​
​
​
最近更新时间:2026.01.23 10:53:39
这个页面对您有帮助吗?
有用
有用
无用
无用