Skip to content

Commit

Permalink
增加历史版本恢复demo
Browse files Browse the repository at this point in the history
  • Loading branch information
jojoliang committed Apr 22, 2024
1 parent 9265a98 commit 413e910
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 0 deletions.
104 changes: 104 additions & 0 deletions example/object/object_recover.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package main

import (
"context"
"fmt"
"os"

"net/url"

"net/http"

"github.com/tencentyun/cos-go-sdk-v5"
"github.com/tencentyun/cos-go-sdk-v5/debug"
)

func log_status(err error) {
if err == nil {
return
}
if cos.IsNotFoundError(err) {
// WARN
fmt.Println("WARN: Resource is not existed: %v", err)
} else if e, ok := cos.IsCOSError(err); ok {
fmt.Printf("ERROR: Code: %v\n", e.Code)
fmt.Printf("ERROR: Message: %v\n", e.Message)
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
// ERROR
} else {
fmt.Printf("ERROR: %v\n", err)
// ERROR
}
}

var (
srcBucket = "test-1259654469"
dstBucket = "test2-1259654469"
srcBucketRegion = "ap-guangzhou"
dstBucketRegion = "ap-guangzhou"

srcCosClient *cos.Client
dstCosClient *cos.Client
)

func newClient(bucket, region string) *cos.Client {
u, _ := url.Parse(fmt.Sprintf("https://%v.cos.%v.myqcloud.com", bucket, region))
b := &cos.BaseURL{
BucketURL: u,
}
return cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("COS_SECRETID"),
SecretKey: os.Getenv("COS_SECRETKEY"),
Transport: &debug.DebugRequestTransport{
RequestHeader: false,
RequestBody: false,
ResponseHeader: false,
ResponseBody: false,
},
},
})
}

func recoverObj(key, versionId string) {
sourceURL := fmt.Sprintf("%v.cos.%v.myqcloud.com/%v?versionId=%v", srcBucket, srcBucketRegion, key, versionId)
_, _, err := dstCosClient.Object.MultiCopy(context.Background(), key, sourceURL, nil)
if err != nil {
log_status(err)
}
}

func main() {
srcCosClient = newClient(srcBucket, srcBucketRegion)
dstCosClient = newClient(dstBucket, dstBucketRegion)

keyMarker := ""
versionIdMarker := ""
isTruncated := true
opt := &cos.BucketGetObjectVersionsOptions{
EncodingType: "url",
}
var preKey string
for isTruncated {
opt.KeyMarker = keyMarker
opt.VersionIdMarker = versionIdMarker
v, _, err := srcCosClient.Bucket.GetObjectVersions(context.Background(), opt)
if err != nil {
log_status(err)
break
}
for _, vc := range v.Version {
// 最新非deletemarker对象
if preKey != vc.Key {
preKey = vc.Key
key, _ := cos.DecodeURIComponent(vc.Key)
fmt.Printf("key: %v, versionId: %v, lastest: %v\n", key, vc.VersionId, vc.IsLatest)
recoverObj(key, vc.VersionId)
}
}
keyMarker = v.NextKeyMarker
versionIdMarker = v.NextVersionIdMarker
isTruncated = v.IsTruncated
}
}
107 changes: 107 additions & 0 deletions example/object/object_recover2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package main

import (
"context"
"fmt"
"os"

"net/url"

"net/http"

"github.com/tencentyun/cos-go-sdk-v5"
"github.com/tencentyun/cos-go-sdk-v5/debug"
)

func log_status(err error) {
if err == nil {
return
}
if cos.IsNotFoundError(err) {
// WARN
fmt.Println("WARN: Resource is not existed: %v", err)
} else if e, ok := cos.IsCOSError(err); ok {
fmt.Printf("ERROR: Code: %v\n", e.Code)
fmt.Printf("ERROR: Message: %v\n", e.Message)
fmt.Printf("ERROR: Resource: %v\n", e.Resource)
fmt.Printf("ERROR: RequestId: %v\n", e.RequestID)
// ERROR
} else {
fmt.Printf("ERROR: %v\n", err)
// ERROR
}
}

var (
srcBucket = "test-1259654469"
srcBucketRegion = "ap-guangzhou"

srcCosClient *cos.Client

copyObjs = map[string]struct{}{}
)

func newClient(bucket, region string) *cos.Client {
u, _ := url.Parse(fmt.Sprintf("https://%v.cos.%v.myqcloud.com", bucket, region))
b := &cos.BaseURL{
BucketURL: u,
}
return cos.NewClient(b, &http.Client{
Transport: &cos.AuthorizationTransport{
SecretID: os.Getenv("COS_SECRETID"),
SecretKey: os.Getenv("COS_SECRETKEY"),
Transport: &debug.DebugRequestTransport{
RequestHeader: false,
RequestBody: false,
ResponseHeader: false,
ResponseBody: false,
},
},
})
}

func recoverObj(key, versionId string) {
sourceURL := fmt.Sprintf("%v.cos.%v.myqcloud.com/%v?versionId=%v", srcBucket, srcBucketRegion, key, versionId)
_, _, err := srcCosClient.Object.MultiCopy(context.Background(), key, sourceURL, nil)
if err != nil {
log_status(err)
}
}

func main() {
srcCosClient = newClient(srcBucket, srcBucketRegion)

keyMarker := ""
versionIdMarker := ""
isTruncated := true
opt := &cos.BucketGetObjectVersionsOptions{
EncodingType: "url",
}
for isTruncated {
opt.KeyMarker = keyMarker
opt.VersionIdMarker = versionIdMarker
v, _, err := srcCosClient.Bucket.GetObjectVersions(context.Background(), opt)
if err != nil {
log_status(err)
break
}
for _, vc := range v.DeleteMarker {
if vc.IsLatest {
// 对象被删除,需要恢复
copyObjs[vc.Key] = struct{}{}
}
}
for _, vc := range v.Version {
// 按最新恢复
if _, ok := copyObjs[vc.Key]; ok {
delete(copyObjs, vc.Key)
key, _ := cos.DecodeURIComponent(vc.Key)
fmt.Printf("key: %v, versionId: %v\n", key, vc.VersionId)
recoverObj(key, vc.VersionId)
}
}
keyMarker = v.NextKeyMarker
versionIdMarker = v.NextVersionIdMarker
isTruncated = v.IsTruncated
}
}

0 comments on commit 413e910

Please sign in to comment.