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

对象存储

复制全文
下载 pdf
Rust
快速入门(Rust SDK)
复制全文
下载 pdf
快速入门(Rust SDK)
本文介绍如何通过 TOS Rust SDK 来完成常见的操作,如创建桶,上传、下载和删除对象等。​
创建桶​
存储桶(Bucket)是 TOS 的全局唯一的命名空间,相当于数据的容器,用来储存对象数据。如下代码展示如何创建一个新存储桶。​
​
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::CreateBucketInput;​
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() {​
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 bucket = "bucketname";​
let input = CreateBucketInput::new(bucket);​
let output = tos_client.create_bucket(&input).await;​
match output {​
Ok(output) => {​
println!("request_id: {}", output.request_id());​
}​
Err(err) => {​
match err {​
TosError::TosClientError { message, .. } => {​
println!("message: {}", message);​
}​
TosError::TosServerError { request_id, ec, .. } => {​
println!("request_id: {}, ec: {}", request_id, ec);​
}​
}​
}​
}​
}​
​
上传对象​
新建存储桶成功后,可以上传对象对象到存储桶中。以下代码将字符串上传到 TOS 存储桶。​
​
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::object::ObjectAPI;​
use ve_tos_rust_sdk::asynchronous::tos;​
use ve_tos_rust_sdk::asynchronous::tos::AsyncRuntime;​
use ve_tos_rust_sdk::error::TosError;​
use ve_tos_rust_sdk::object::PutObjectFromBufferInput;​
​
#[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() {​
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 bucket = "bucketname";​
let key = "objectkey";​
let mut input = PutObjectFromBufferInput::new(bucket, key);​
// 设置对象内容​
input.set_content("hello world");​
let output = tos_client.put_object_from_buffer(&input).await;​
match output {​
Ok(output) => {​
println!("request_id: {}", output.request_id());​
}​
Err(err) => {​
match err {​
TosError::TosClientError { message, .. } => {​
println!("message: {}", message);​
}​
TosError::TosServerError { request_id, ec, .. } => {​
println!("request_id: {}, ec: {}", request_id, ec);​
}​
}​
}​
}​
}​
​
下载对象​
以下展示从存储桶中下载一个对象到内存中。​
​
use async_trait::async_trait;​
use futures_core::future::BoxFuture;​
use futures_util::StreamExt;​
use std::env;​
use std::future::Future;​
use std::time::Duration;​
use tokio::runtime::Handle;​
use ve_tos_rust_sdk::asynchronous::object::ObjectAPI;​
use ve_tos_rust_sdk::asynchronous::tos;​
use ve_tos_rust_sdk::asynchronous::tos::AsyncRuntime;​
use ve_tos_rust_sdk::error::TosError;​
use ve_tos_rust_sdk::object::GetObjectInput;​
​
#[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() {​
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 bucket = "bucketname";​
let key = "objectkey";​
let input = GetObjectInput::new(bucket, key);​
let output = tos_client.get_object(&input).await;​
match output {​
Ok(mut output) => {​
println!("request_id: {}", output.request_id());​
// 读取对象内容到内存​
let mut buf = Vec::new();​
loop {​
match output.next().await {​
None => break,​
Some(result) => {​
let x = result.unwrap();​
buf.extend_from_slice(x.slice(0..x.len()).as_ref());​
}​
}​
}​
println!("{}", String::from_utf8(buf).unwrap());​
}​
Err(err) => {​
match err {​
TosError::TosClientError { message, .. } => {​
println!("message: {}", message);​
}​
TosError::TosServerError { request_id, ec, .. } => {​
println!("request_id: {}, ec: {}", request_id, ec);​
}​
}​
}​
}​
}​
​
列举对象​
以下展示从存储桶中列举已经上传的对象。​
​
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::object::ObjectAPI;​
use ve_tos_rust_sdk::asynchronous::tos;​
use ve_tos_rust_sdk::asynchronous::tos::AsyncRuntime;​
use ve_tos_rust_sdk::error::TosError;​
use ve_tos_rust_sdk::object::ListObjectsType2Input;​
​
#[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() {​
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 bucket = "bucketname";​
let input = ListObjectsType2Input::new(bucket);​
let output = tos_client.list_objects_type2(&input).await;​
match output {​
Ok(output) => {​
println!("request_id: {}", output.request_id());​
for content in output.contents() {​
println!("content: {}, size: {}", content.key(), content.size());​
}​
}​
Err(err) => {​
match err {​
TosError::TosClientError { message, .. } => {​
println!("message: {}", message);​
}​
TosError::TosServerError { request_id, ec, .. } => {​
println!("request_id: {}, ec: {}", request_id, ec);​
}​
}​
}​
}​
}​
​
删除对象​
以下代码展示从存储桶中删除一个对象。​
​
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::object::ObjectAPI;​
use ve_tos_rust_sdk::asynchronous::tos;​
use ve_tos_rust_sdk::asynchronous::tos::AsyncRuntime;​
use ve_tos_rust_sdk::error::TosError;​
use ve_tos_rust_sdk::object::DeleteObjectInput;​
​
#[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() {​
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 bucket = "bucketname";​
let key = "objectkey";​
let input = DeleteObjectInput::new(bucket, key);​
let output = tos_client.delete_object(&input).await;​
match output {​
Ok(output) => {​
println!("request_id: {}", output.request_id());​
}​
Err(err) => {​
match err {​
TosError::TosClientError { message, .. } => {​
println!("message: {}", message);​
}​
TosError::TosServerError { request_id, ec, .. } => {​
println!("request_id: {}, ec: {}", request_id, ec);​
}​
}​
}​
}​
}​
​
​
最近更新时间:2025.12.22 16:33:47
这个页面对您有帮助吗?
有用
有用
无用
无用