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

对象存储

复制全文
下载 pdf
自定义域名
PutBucketCustomDomain(Rust SDK)
复制全文
下载 pdf
PutBucketCustomDomain(Rust SDK)
put_bucket_custom_domain 接口用于为存储桶绑定自定义域名,本文介绍如何通过 Rust SDK 的 put_bucket_custom_domain 接口完成绑定。绑定后需将域名 CNAME 指向桶外网域名。​
注意事项​
  • 您需要根据实际情况,填写示例代码中的地域和访问域名,详细介绍,请参见 地域和访问域名。​
  • 为了防止密钥泄露,本文示例代码需要从环境变量中读取 Access Key ID 和 Secret Access Key。因此使用 SDK 前,您需要配置长期访问凭证,详细介绍,请参见 配置长期访问凭证。​
  • 每个存储桶最多可绑定 20 条自定义域名,超出后 put_bucket_custom_domain 将返回错误。​
  • 请求体中 domain 字段必须填写已通过 ICP 备案的域名,否则 put_bucket_custom_domain 不会生效。​
  • 若 cert_id 留空,put_bucket_custom_domain 将使用 HTTP 协议;提供 cert_id 后,系统强制使用 HTTPS 并校验证书有效性。​
权限说明​
火山引擎主账号默认拥有全部权限,但是 IAM 用户默认没有任何权限。如果您需要使用子账号执行 put_bucket_custom_domain 操作,火山引擎主账号需通过 IAM策略和桶策略为该子账号授予以下权限:​
tos:PutBucketCustomDomain​
代码示例​
为存储桶绑定自定义域名​
​
use async_trait::async_trait;​
use futures_core::future::BoxFuture;​
use std::env;​
use std::future::Future;​
use std::sync::Arc;​
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::{CustomDomainRule, PutBucketCustomDomainInput};​
use ve_tos_rust_sdk::enumeration::AuthProtocolType;​
​
#[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 client = tos::builder::<TokioRuntime>()​
.connection_timeout(3000)​
.request_timeout(10000)​
.max_retry_count(3)​
.ak(ak)​
.sk(sk)​
.region(region)​
.endpoint(endpoint)​
.build().unwrap();​
​
// 创建自定义域名规则​
let rule = CustomDomainRule::new("example.com", AuthProtocolType::AuthProtocolTos);​
​
// 创建输入参数​
let input = PutBucketCustomDomainInput::new("my-bucket", rule);​
​
// 调用接口绑定自定义域名​
let output = client.put_bucket_custom_domain(&input).await;​
​
match output {​
Ok(output) => {​
println!("自定义域名绑定成功,请求ID: {}", output.request_id());​
}​
Err(err) => {​
eprintln!("自定义域名绑定失败: {:?}", err);​
}​
}​
}​
​
为存储桶设置多条自定义域名​
​
use async_trait::async_trait;​
use futures_core::future::BoxFuture;​
use std::env;​
use std::future::Future;​
use std::sync::Arc;​
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::{CustomDomainRule, PutBucketCustomDomainInput};​
use ve_tos_rust_sdk::enumeration::AuthProtocolType;​
​
#[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 client = tos::builder::<TokioRuntime>()​
.connection_timeout(3000)​
.request_timeout(10000)​
.max_retry_count(3)​
.ak(ak)​
.sk(sk)​
.region(region)​
.endpoint(endpoint)​
.build().unwrap();​
​
// 定义多个域名配置​
let domains = vec![​
("domain1.com", AuthProtocolType::AuthProtocolTos, ""),​
("domain2.com", AuthProtocolType::AuthProtocolS3, "cert-1234567890"),​
("domain3.com", AuthProtocolType::AuthProtocolTos, ""),​
];​
​
for (domain, protocol, cert_id) in domains {​
// 创建自定义域名规则​
let mut rule = CustomDomainRule::new(domain, protocol);​
​
// 如果有证书ID则设置​
if !cert_id.is_empty() {​
rule.set_cert_id(cert_id);​
}​
​
// 创建输入参数​
let input = PutBucketCustomDomainInput::new("my-bucket", rule);​
​
// 调用接口绑定自定义域名​
let output = client.put_bucket_custom_domain(&input).await;​
​
match output {​
Ok(output) => {​
println!("域名 {} 绑定成功,请求ID: {}", domain, output.request_id());​
}​
Err(err) => {​
eprintln!("域名 {} 绑定失败: {:?}", domain, err);​
}​
}​
}​
}​
​
为自定义域名设置证书 ID​
​
use async_trait::async_trait;​
use futures_core::future::BoxFuture;​
use std::env;​
use std::future::Future;​
use std::sync::Arc;​
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::{CustomDomainRule, PutBucketCustomDomainInput};​
use ve_tos_rust_sdk::enumeration::AuthProtocolType;​
​
#[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 client = tos::builder::<TokioRuntime>()​
.connection_timeout(3000)​
.request_timeout(10000)​
.max_retry_count(3)​
.ak(ak)​
.sk(sk)​
.region(region)​
.endpoint(endpoint)​
.build().unwrap();​
​
// 创建自定义域名规则​
let mut rule = CustomDomainRule::new("secure-domain.com", AuthProtocolType::AuthProtocolTos);​
​
// 设置证书ID​
rule.set_cert_id("cert-***");​
​
// 创建输入参数​
let input = PutBucketCustomDomainInput::new("my-bucket", rule);​
​
// 调用接口绑定自定义域名​
let output = client.put_bucket_custom_domain(&input).await;​
​
match output {​
Ok(output) => {​
println!("带证书的自定义域名绑定成功,请求ID: {}", output.request_id());​
}​
Err(err) => {​
eprintln!("带证书的自定义域名绑定失败: {:?}", err);​
}​
}​
}​
​
使用 S3 协议绑定自定义域名​
​
use async_trait::async_trait;​
use futures_core::future::BoxFuture;​
use std::env;​
use std::future::Future;​
use std::sync::Arc;​
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::{CustomDomainRule, PutBucketCustomDomainInput};​
use ve_tos_rust_sdk::enumeration::AuthProtocolType;​
​
#[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 client = tos::builder::<TokioRuntime>()​
.connection_timeout(3000)​
.request_timeout(10000)​
.max_retry_count(3)​
.ak(ak)​
.sk(sk)​
.region(region)​
.endpoint(endpoint)​
.build().unwrap();​
​
// 使用S3协议创建自定义域名规则​
let rule = CustomDomainRule::new("s3-domain.com", AuthProtocolType::AuthProtocolS3);​
​
// 创建输入参数​
let input = PutBucketCustomDomainInput::new("my-bucket", rule);​
​
// 调用接口绑定自定义域名​
let output = client.put_bucket_custom_domain(&input).await;​
​
match output {​
Ok(output) => {​
println!("S3协议自定义域名绑定成功,请求ID: {}", output.request_id());​
}​
Err(err) => {​
eprintln!("S3协议自定义域名绑定失败: {:?}", err);​
}​
}​
}​
​
使用 TOS 协议绑定自定义域名​
​
use async_trait::async_trait;​
use futures_core::future::BoxFuture;​
use std::env;​
use std::future::Future;​
use std::sync::Arc;​
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::{CustomDomainRule, PutBucketCustomDomainInput};​
use ve_tos_rust_sdk::enumeration::AuthProtocolType;​
​
#[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 client = tos::builder::<TokioRuntime>()​
.connection_timeout(3000)​
.request_timeout(10000)​
.max_retry_count(3)​
.ak(ak)​
.sk(sk)​
.region(region)​
.endpoint(endpoint)​
.build()​
.unwrap();​
​
// 使用TOS协议创建自定义域名规则​
let rule = CustomDomainRule::new("my-domain.com", AuthProtocolType::AuthProtocolTos);​
​
// 创建输入参数​
let input = PutBucketCustomDomainInput::new("my-bucket", rule);​
​
// 调用接口绑定自定义域名​
let output = client.put_bucket_custom_domain(&input).await;​
​
match output {​
Ok(output) => {​
println!(​
"TOS协议自定义域名绑定成功,请求ID: {}",​
output.request_id()​
);​
}​
Err(err) => {​
eprintln!("TOS协议自定义域名绑定失败: {:?}", err);​
}​
}​
}​
​
​
相关文档​
关于 PutBucketCustomDomain 的 API 文档,请参见 PutBucketCustomDomain。​
​
最近更新时间:2025.12.22 16:33:48
这个页面对您有帮助吗?
有用
有用
无用
无用