当您将文件上传至 TOS 后,您可以使用 TOS 自动生成的文件 URL 访问或下载文件。如果您希望使用您自己的域名访问存储在 TOS 的文件,您可以为存储桶绑定您自己的域名。绑定后,即可使用相应域名访问文件。
注意
以下代码用于设置桶的自定义域名规则。
package main import ( "context" "fmt" "github.com/volcengine/ve-tos-golang-sdk/v2/tos" ) func checkErr(err error) { if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { fmt.Println("Error:", serverErr.Error()) fmt.Println("Request ID:", serverErr.RequestID) fmt.Println("Response Status Code:", serverErr.StatusCode) fmt.Println("Response Header:", serverErr.Header) fmt.Println("Response Err Code:", serverErr.Code) fmt.Println("Response Err Msg:", serverErr.Message) } else { fmt.Println("Error:", err) } panic(err) } } func main() { var ( accessKey = os.Getenv("TOS_ACCESS_KEY") secretKey = os.Getenv("TOS_SECRET_KEY") // Bucket 对于的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com endpoint = "https://tos-cn-beijing.volces.com" region = "cn-beijing" // 填写 BucketName bucketName = "*** Provide your bucket name ***" ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) // 设置自定义域名 input := &tos.PutBucketCustomDomainInput{ Bucket: bucketName, Rule: tos.CustomDomainRule{ Domain: "www.example.com", }, } output, err := client.PutBucketCustomDomain(context.Background(), input) checkErr(err) fmt.Println("PutBucketCustomDomain Request ID:", output.RequestID) }
注意
要列举桶的自定义域名规则,默认您必须为桶所有者。
以下代码用于列举桶的自定义域名规则。
package main import ( "context" "fmt" "github.com/volcengine/ve-tos-golang-sdk/v2/tos" ) func checkErr(err error) { if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { fmt.Println("Error:", serverErr.Error()) fmt.Println("Request ID:", serverErr.RequestID) fmt.Println("Response Status Code:", serverErr.StatusCode) fmt.Println("Response Header:", serverErr.Header) fmt.Println("Response Err Code:", serverErr.Code) fmt.Println("Response Err Msg:", serverErr.Message) } else { fmt.Println("Error:", err) } panic(err) } } func main() { var ( accessKey = os.Getenv("TOS_ACCESS_KEY") secretKey = os.Getenv("TOS_SECRET_KEY") // Bucket 对于的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com endpoint = "https://tos-cn-beijing.volces.com" region = "cn-beijing" // 填写 BucketName bucketName = "*** Provide your bucket name ***" ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) output, err := client.ListBucketCustomDomain(context.Background(), &tos.ListBucketCustomDomainInput{Bucket: bucketName}) checkErr(err) fmt.Println("ListBucketCustomDomain Request ID:", output.RequestID) // 查看自定义域名 for _, rule := range output.Rules { fmt.Println("ListBucketCustomDomain Rule Domain:", rule.Domain) } }
注意
要删除桶的自定义域名规则,默认您必须为桶所有者。
以下代码用于删除桶的自定义域名规则。
package main import ( "context" "fmt" "github.com/volcengine/ve-tos-golang-sdk/v2/tos" ) func checkErr(err error) { if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { fmt.Println("Error:", serverErr.Error()) fmt.Println("Request ID:", serverErr.RequestID) fmt.Println("Response Status Code:", serverErr.StatusCode) fmt.Println("Response Header:", serverErr.Header) fmt.Println("Response Err Code:", serverErr.Code) fmt.Println("Response Err Msg:", serverErr.Message) } else { fmt.Println("Error:", err) } panic(err) } } func main() { var ( accessKey = os.Getenv("TOS_ACCESS_KEY") secretKey = os.Getenv("TOS_SECRET_KEY") // Bucket 对于的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com endpoint = "https://tos-cn-beijing.volces.com" region = "cn-beijing" // 填写 BucketName bucketName = "*** Provide your bucket name ***" ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) output, err := client.DeleteBucketCustomDomain(context.Background(), &tos.DeleteBucketCustomDomainInput{Bucket: bucketName, Domain: "Provide your custom domain"}) checkErr(err) fmt.Println("DeleteBucketCustomDomain Request ID:", output.RequestID) }
关于自定义域名配置的更多信息,请参见自定义域名。