本文介绍删除对象的常用场景代码。
以下代码用于删除指定目录下 exampledir
的对象。
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 { if serverErr.StatusCode == http.StatusNotFound { return } 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") // 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 ***" ctx = context.Background() ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) // 列举指定前缀下的所有对象 truncated := true continuationToken := "" prefix := "exampledir/" for truncated { output, err := client.ListObjectsType2(ctx, &tos.ListObjectsType2Input{ Bucket: bucketName, MaxKeys: 1000, ContinuationToken: continuationToken, Prefix: prefix, }) checkErr(err) for _, obj := range output.Contents { deleteOutput, err := client.DeleteObjectV2(ctx, &tos.DeleteObjectV2Input{ Bucket: bucketName, Key: obj.Key, }) checkErr(err) fmt.Println("DeleteObjectV2 Request ID:", deleteOutput.RequestID) } truncated = output.IsTruncated continuationToken = output.NextContinuationToken } }
以下代码用于清空整个桶,包括删除所有对象、删除所有未合并的对象。
package main import ( "context" "fmt" "net/http" "os" "github.com/volcengine/ve-tos-golang-sdk/v2/tos" ) func checkErr(err error) { if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { if serverErr.StatusCode == http.StatusNotFound { return } 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") // 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 ***" ctx = context.Background() ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) // 删除所有对象 truncated := true continuationToken := "" for truncated { output, err := client.ListObjectsType2(ctx, &tos.ListObjectsType2Input{ Bucket: bucketName, MaxKeys: 1000, ContinuationToken: continuationToken, }) checkErr(err) for _, obj := range output.Contents { deleteOutput, err := client.DeleteObjectV2(ctx, &tos.DeleteObjectV2Input{ Bucket: bucketName, Key: obj.Key, }) checkErr(err) fmt.Println("DeleteObjectV2 Request ID:", deleteOutput.RequestID) } truncated = output.IsTruncated continuationToken = output.NextContinuationToken } // 终止所有未完成的分片 truncated = true marker := "" uploadIDMarker := "" for truncated { output, err := client.ListMultipartUploadsV2(ctx, &tos.ListMultipartUploadsV2Input{ Bucket: bucketName, MaxUploads: 1000, KeyMarker: marker, UploadIDMarker: uploadIDMarker, }) checkErr(err) for _, upload := range output.Uploads { abortUpload, err := client.AbortMultipartUpload(ctx, &tos.AbortMultipartUploadInput{ Bucket: bucketName, Key: upload.Key, UploadID: upload.UploadID, }) checkErr(err) fmt.Println("AbortMultipartUpload Upload ID:", upload.UploadID, " Request ID:", abortUpload.RequestID) } truncated = output.IsTruncated marker = output.NextKeyMarker uploadIDMarker = output.NextUploadIDMarker } }
以下代码用于清空整个开启多版本桶的示例代码,包括删除所有对象的多个版本、删除删除标记的多个版本、删除所有未合并的对象。
package main import ( "context" "fmt" "net/http" "os" "github.com/volcengine/ve-tos-golang-sdk/v2/tos" ) func checkErr(err error) { if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { if serverErr.StatusCode == http.StatusNotFound { return } 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") // 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 ***" ctx = context.Background() ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) // 删除桶中所有对象 truncated := true marker := "" versionIdMarker := "" for truncated { listOutput, err := client.ListObjectVersionsV2(ctx, &tos.ListObjectVersionsV2Input{ Bucket: bucketName, ListObjectVersionsInput: tos.ListObjectVersionsInput{ MaxKeys: 1000, KeyMarker: marker, VersionIDMarker: versionIdMarker, }, }) checkErr(err) for _, obj := range listOutput.Versions { // 删除多版本对象 output, err := client.DeleteObjectV2(ctx, &tos.DeleteObjectV2Input{ Bucket: bucketName, Key: obj.Key, VersionID: obj.VersionID, }) checkErr(err) fmt.Printf("Delete %s, Request ID: %s\n", obj.Key, output.RequestID) } // 删除标记信息 for _, deleteMarker := range listOutput.DeleteMarkers { output, err := client.DeleteObjectV2(ctx, &tos.DeleteObjectV2Input{ Bucket: bucketName, Key: deleteMarker.Key, VersionID: deleteMarker.VersionID, }) checkErr(err) fmt.Printf("Delete deleteMarker %s, Request ID: %s\n", deleteMarker.Key, output.RequestID) } truncated = listOutput.IsTruncated marker = listOutput.NextKeyMarker versionIdMarker = listOutput.NextVersionIDMarker } // 删除未完成的分片 truncated = true marker = "" uploadIDMarker := "" for truncated { output, err := client.ListMultipartUploadsV2(ctx, &tos.ListMultipartUploadsV2Input{ Bucket: bucketName, MaxUploads: 1000, KeyMarker: marker, UploadIDMarker: uploadIDMarker, }) checkErr(err) for _, upload := range output.Uploads { abortUpload, err := client.AbortMultipartUpload(ctx, &tos.AbortMultipartUploadInput{ Bucket: bucketName, Key: upload.Key, UploadID: upload.UploadID, }) checkErr(err) fmt.Println("AbortMultipartUpload Upload ID:", upload.UploadID, " Request ID:", abortUpload.RequestID) } truncated = output.IsTruncated marker = output.NextKeyMarker uploadIDMarker = output.NextUploadIDMarker } }