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

对象存储

复制全文
下载 pdf
预签名机制
PreSignedURL(Rust SDK)
复制全文
下载 pdf
PreSignedURL(Rust SDK)
pre_signed_url 接口用于生成带签名的临时上传或下载 URL,本文介绍如何通过 Rust SDK 的 pre_signed_url 接口获取该 URL 并直接访问对象。​
注意事项​
  • 您需要根据实际情况,填写示例代码中的地域和访问域名,详细介绍,请参见 地域和访问域名。​
  • 为了防止密钥泄露,本文示例代码需要从环境变量中读取 Access Key ID 和 Secret Access Key。因此使用 SDK 前,您需要配置长期访问凭证,详细介绍,请参见 配置长期访问凭证。​
  • pre_signed_url 有效期由 expires 参数控制,单位秒,最大不超过 7 天。​
  • 使用 pre_signed_url 上传时,http_method 须与生成时指定的 method 保持一致,否则返回 403。​
​
代码示例​
使用预签名的 URL 上传对象​
​
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::common::init_tracing_log;​
use ve_tos_rust_sdk::enumeration::{HttpMethodType};​
use ve_tos_rust_sdk::asynchronous::tos;​
use ve_tos_rust_sdk::asynchronous::tos::AsyncRuntime;​
use ve_tos_rust_sdk::auth::{PreSignedURLInput, SignerAPI};​
use reqwest::Client as HttpClient;​
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 _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 client = tos::builder::<TokioRuntime>()​
.connection_timeout(3000)​
.request_timeout(10000)​
.max_retry_count(3)​
.ak(ak)​
.sk(sk)​
.region(region)​
.endpoint(endpoint)​
.build().unwrap();​
​
// 1. 使用预签名的URL上传对象​
let bucket = "example-bucket";​
let key = "test-upload-object.txt";​
let mut upload_input = PreSignedURLInput::new_with_key(bucket, key);​
upload_input.set_http_method(HttpMethodType::HttpMethodPut);​
upload_input.set_expires(3600); // 有效期1小时​
​
let upload_output = client.pre_signed_url(&upload_input);​
match upload_output {​
Ok(output) => {​
println!("Pre-signed URL for upload: {}", output.signed_url());​
// 使用reqwest客户端上传对象​
let http_client = HttpClient::new();​
let upload_response = http_client.put(output.signed_url())​
.body("Hello from pre-signed URL upload")​
.send()​
.await;​
match upload_response {​
Ok(resp) if resp.status().is_success() => {​
println!("Upload successful with status: {}", resp.status());​
}​
Ok(resp) => {​
println!("Upload failed with status: {}", resp.status());​
}​
Err(e) => {​
println!("Upload request error: {}", e);​
}​
}​
}​
Err(err) => {​
match err {​
TosError::TosClientError { message, .. } => {​
println!("Client error: {}", message);​
}​
TosError::TosServerError { request_id, ec, code, .. } => {​
println!("Server error: request_id: {}, ec: {}, code: {}", request_id, ec, code);​
}​
}​
}​
}​
}​
​
使用预签名的 URL 下载对象​
​
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::common::init_tracing_log;​
use ve_tos_rust_sdk::enumeration::{HttpMethodType};​
use ve_tos_rust_sdk::asynchronous::tos;​
use ve_tos_rust_sdk::asynchronous::tos::AsyncRuntime;​
use ve_tos_rust_sdk::auth::{PreSignedURLInput, SignerAPI};​
use reqwest::Client as HttpClient;​
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 _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 client = tos::builder::<TokioRuntime>()​
.connection_timeout(3000)​
.request_timeout(10000)​
.max_retry_count(3)​
.ak(ak)​
.sk(sk)​
.region(region)​
.endpoint(endpoint)​
.build().unwrap();​
​
// 2. 使用预签名的URL下载对象​
let bucket = "example-bucket";​
let key = "test-download-object.txt";​
let mut download_input = PreSignedURLInput::new_with_key(bucket, key);​
download_input.set_http_method(HttpMethodType::HttpMethodGet);​
download_input.set_expires(3600);​
​
let download_output = client.pre_signed_url(&download_input);​
match download_output {​
Ok(output) => {​
println!("Pre-signed URL for download: {}", output.signed_url());​
// 使用reqwest客户端下载对象​
let http_client = HttpClient::new();​
let download_response = http_client.get(output.signed_url())​
.send()​
.await;​
match download_response {​
Ok(resp) if resp.status().is_success() => {​
let content = resp.text().await.unwrap();​
println!("Download successful, content: {}", content);​
}​
Ok(resp) => {​
println!("Download failed with status: {}", resp.status());​
}​
Err(e) => {​
println!("Download request error: {}", e);​
}​
}​
}​
Err(err) => {​
match err {​
TosError::TosClientError { message, .. } => {​
println!("Client error: {}", message);​
}​
TosError::TosServerError { request_id, ec, code, .. } => {​
println!("Server error: request_id: {}, ec: {}, code: {}", request_id, ec, code);​
}​
}​
}​
}​
}​
​
使用预签名的 URL 删除对象​
​
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::common::init_tracing_log;​
use ve_tos_rust_sdk::enumeration::{HttpMethodType};​
use ve_tos_rust_sdk::asynchronous::tos;​
use ve_tos_rust_sdk::asynchronous::tos::AsyncRuntime;​
use ve_tos_rust_sdk::auth::{PreSignedURLInput, SignerAPI};​
use reqwest::Client as HttpClient;​
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 _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 client = tos::builder::<TokioRuntime>()​
.connection_timeout(3000)​
.request_timeout(10000)​
.max_retry_count(3)​
.ak(ak)​
.sk(sk)​
.region(region)​
.endpoint(endpoint)​
.build().unwrap();​
​
// 3. 使用预签名的URL删除对象​
let bucket = "example-bucket";​
let key = "test-delete-object.txt";​
let mut delete_input = PreSignedURLInput::new_with_key(bucket, key);​
delete_input.set_http_method(HttpMethodType::HttpMethodDelete);​
delete_input.set_expires(3600);​
​
let delete_output = client.pre_signed_url(&delete_input);​
match delete_output {​
Ok(output) => {​
println!("Pre-signed URL for delete: {}", output.signed_url());​
// 使用reqwest客户端删除对象​
let http_client = HttpClient::new();​
let delete_response = http_client.delete(output.signed_url())​
.send()​
.await;​
match delete_response {​
Ok(resp) if resp.status().is_success() => {​
println!("Delete successful with status: {}", resp.status());​
}​
Ok(resp) => {​
println!("Delete failed with status: {}", resp.status());​
}​
Err(e) => {​
println!("Delete request error: {}", e);​
}​
}​
}​
Err(err) => {​
match err {​
TosError::TosClientError { message, .. } => {​
println!("Client error: {}", message);​
}​
TosError::TosServerError { request_id, ec, code, .. } => {​
println!("Server error: request_id: {}, ec: {}, code: {}", request_id, ec, code);​
}​
}​
}​
}​
}​
​
相关文档​
关于 PreSignedURL 的 API 文档,请参见 URL 中包含签名。​
​
最近更新时间:2025.12.22 16:33:48
这个页面对您有帮助吗?
有用
有用
无用
无用