You need to enable JavaScript to run this app.
Torch Object Storage

Torch Object Storage

Copy page
Download PDF
Manage Bucket Configuration
Managing Bucket Lifecycle (Go SDK)
Copy page
Download PDF
Managing Bucket Lifecycle (Go SDK)
You can utilize the lifecycle management feature of object storage through the SDK to delete expired objects, thereby reducing storage costs. When configuring lifecycle rules, you can specify an object prefix or an object tag for which the rule will take effect. If you specify two effective conditions simultaneously, the object must satisfy all set conditions; only then will the lifecycle rule be applied.​
Configure lifecycle rules​
warning​
  • Before configuring lifecycle rules, you must have the tos:PutLifecycleConfiguration permission. For detailed instructions, please refer to the Permission configuration guide.​
  • If lifecycle rules have already been configured for the bucket, performing this operation will overwrite the existing configuration. If you need to add lifecycle rules, first use GetLifecycleConfiguration to retrieve the current rules, then append and write the new rules, and finally call PutLifecycleConfiguration to update the rules.​
Sample code​
The following code is used to configure the lifecycle rules for a bucket.​
​
package main​
​
import (​
"context"​
"fmt"​
​
"github.com/volcengine/ve-tos-golang-sdk/v2/tos"​
"github.com/volcengine/ve-tos-golang-sdk/v2/tos/enum"​
)​
​
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 if clientErr, ok := err.(*tos.TosClientError); ok {​
fmt.Println("Error:", clientErr.Error())​
fmt.Println("Client Cause Err:", clientErr.Cause.Error())​
} else {​
fmt.Println("Error:", err)​
}​
panic(err)​
}​
}​
​
func main() {​
var (​
accessKey = os.Getenv("TOS_ACCESS_KEY")​
secretKey = os.Getenv("TOS_SECRET_KEY")​
​
endpoint = "your endpoint"​
region = "your region"​
​
bucketName = "*** Provide your bucket name ***"​
ctx = context.Background()​
)​
​
client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))​
checkErr(err)​
lifeCycleRule := tos.LifecycleRule{​
​
ID: "rule1",​
​
Prefix: "prefix-",​
​
Status: enum.LifecycleStatusEnabled,​
​
​
​
Expiration: &tos.Expiration{​
Days: 30,​
},​
​
Tag: []tos.Tag{{​
Key: "key",​
Value: "value",​
}},​
}​
​
putOutput, err := client.PutBucketLifecycle(ctx, &tos.PutBucketLifecycleInput{​
Bucket: bucketName,​
Rules: []tos.LifecycleRule{lifeCycleRule},​
})​
checkErr(err)​
fmt.Println("PutBucketLifecycle Request ID:", putOutput.RequestID)​
​
}​
​
Retrieve lifecycle rules​
warning​
  • Before retrieving lifecycle rules, you must have the tos:GetLifecycleConfiguration permission. For specific operations, please refer to the Permission configuration guide.​
Sample Code​
The following code is used to retrieve the lifecycle rules of a bucket.​
​
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 if clientErr, ok := err.(*tos.TosClientError); ok {​
fmt.Println("Error:", clientErr.Error())​
fmt.Println("Client Cause Err:", clientErr.Cause.Error())​
} else {​
fmt.Println("Error:", err)​
}​
panic(err)​
}​
}​
​
func main() {​
var (​
accessKey = os.Getenv("TOS_ACCESS_KEY")​
secretKey = os.Getenv("TOS_SECRET_KEY")​
​
endpoint = "your endpoint"​
region = "your region"​
​
bucketName = "*** Provide your bucket name ***"​
ctx = context.Background()​
)​
​
client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))​
checkErr(err)​
​
getOutput, err := client.GetBucketLifecycle(ctx, &tos.GetBucketLifecycleInput{Bucket: bucketName})​
checkErr(err)​
fmt.Println("GetBucketLifecycle Request ID:", getOutput.RequestID)​
fmt.Println("GetBucketLifecycle Rules Length:", len(getOutput.Rules))​
for _, rule := range getOutput.Rules {​
​
fmt.Println("Rule ID:", rule.ID)​
​
fmt.Println("Rule Prefix:", rule.Prefix)​
​
fmt.Println("Rule Status:", rule.Status)​
​
fmt.Println("Rule Transitions:", rule.Transitions)​
​
fmt.Println("Rule Expiration:", rule.Expiration)​
​
fmt.Println("Rule NonCurrentVersionTransition:", rule.NonCurrentVersionTransition)​
​
fmt.Println("Rule NoCurrentVersionExpiration:", rule.NoCurrentVersionExpiration)​
​
for key, value := range rule.Tag {​
fmt.Println("Rule Tag Key:", key)​
fmt.Println("Rule Tag Value:", value)​
}​
​
fmt.Println("Rule AbortInCompleteMultipartUpload:", rule.AbortInCompleteMultipartUpload)​
}​
}​
​
Delete lifecycle rules​
warning​
Before deleting lifecycle rules, you must have the tos:PutLifecycleConfiguration permission. For specific instructions, please refer to the Permission Configuration Guide.​
Sample code​
The following code is used to delete lifecycle rules for a bucket:​
​
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 if clientErr, ok := err.(*tos.TosClientError); ok {​
fmt.Println("Error:", clientErr.Error())​
fmt.Println("Client Cause Err:", clientErr.Cause.Error())​
} else {​
fmt.Println("Error:", err)​
}​
panic(err)​
}​
}​
​
func main() {​
var (​
accessKey = os.Getenv("TOS_ACCESS_KEY")​
secretKey = os.Getenv("TOS_SECRET_KEY")​
​
endpoint = "your endpoint"​
region = "your region"​
​
bucketName = "*** Provide your bucket name ***"​
ctx = context.Background()​
)​
​
client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey)))​
checkErr(err)​
​
​
deleteOutput, err := client.DeleteBucketLifecycle(ctx, &tos.DeleteBucketLifecycleInput{Bucket: bucketName})​
checkErr(err)​
fmt.Println("DeleteBucketLifecycle Request ID:", deleteOutput.RequestID)​
}​
​
Relevant documentation​
For more information about lifecycle rules, see Configuring lifecycle rules.​
Last updated: 2026.07.31 18:27:28