快照
更新时间:2024-07-02
创建快照
如下代码可以创建一个快照:
package main
import (
"fmt"
"github.com/baidubce/bce-sdk-go/services/bcc"
"github.com/baidubce/bce-sdk-go/services/bcc/api"
)
func main() {
// 设置您的ak、sk和要访问的endpoint
ak := "ak"
sk := "sk"
endpoint := "http://bcc.bj.baidubce.com"
// 创建bcc client
bccClient, _ := bcc.NewClient(ak, sk, endpoint)
args := &api.CreateSnapshotArgs{
// 磁盘ID
VolumeId: "v-***",
SnapshotName: "snapshotName",
Description: "create by sdk",
}
result, err := bccClient.CreateSnapshot(args)
if err != nil {
fmt.Println("create snapshot failed:", err)
} else {
fmt.Println("create snapshot success: ", result)
}
}
查询快照列表
如下代码可以查询当前账户下所有快照的列表:
package main
import (
"fmt"
"github.com/baidubce/bce-sdk-go/services/bcc"
"github.com/baidubce/bce-sdk-go/services/bcc/api"
)
func main() {
// 设置您的ak、sk和要访问的endpoint
ak := "ak"
sk := "sk"
endpoint := "http://bcc.bj.baidubce.com"
// 创建bcc client
bccClient, _ := bcc.NewClient(ak, sk, endpoint)
args := &api.ListSnapshotArgs{}
result, err := bccClient.ListSnapshot(args)
if err != nil {
fmt.Println("list all snapshot failed:", err)
} else {
fmt.Println("list all snapshot success: ", result)
}
}
查询快照详情
如下代码可以查询特定快照的详细信息:
package main
import (
"fmt"
"github.com/baidubce/bce-sdk-go/services/bcc"
)
func main() {
// 设置您的ak、sk和要访问的endpoint
ak := "ak"
sk := "sk"
endpoint := "http://bcc.bj.baidubce.com"
// 创建bcc client
bccClient, _ := bcc.NewClient(ak, sk, endpoint)
// 快照id
snapshotId := "s-***"
result, err := bccClient.GetSnapshotDetail(snapshotId)
if err != nil {
fmt.Println("get snapshot detail failed:", err)
} else {
fmt.Println("get snapshot detail success: ", result)
}
}
删除快照
如下代码可以删除一个快照:
package main
import (
"fmt"
"github.com/baidubce/bce-sdk-go/services/bcc"
)
func main() {
// 设置您的ak、sk和要访问的endpoint
ak := "ak"
sk := "sk"
endpoint := "http://bcc.bj.baidubce.com"
// 创建bcc client
bccClient, _ := bcc.NewClient(ak, sk, endpoint)
// 快照id
snapshotId := "s-***"
err := bccClient.DeleteSnapshot(snapshotId)
if err != nil {
fmt.Println("delete snapshot failed:", err)
} else {
fmt.Println("delete snapshot success")
}
}
查询快照链列表
通过以下代码可以查询快照链列表:
package main
import (
"fmt"
"github.com/baidubce/bce-sdk-go/services/bcc"
"github.com/baidubce/bce-sdk-go/services/bcc/api"
)
func main() {
// 设置您的ak、sk和要访问的endpoint
ak := "ak"
sk := "sk"
endpoint := "http://bcc.bj.baidubce.com"
// 创建bcc client
bccClient, _ := bcc.NewClient(ak, sk, endpoint)
args := &api.ListSnapshotChainArgs{
// 设置排序属性:chainId(快照链id,默认值),chainSize(快照链大小),volumeSize(磁盘大小)。可选参数
OrderBy: "your-order-by",
// 设置排序方式:asc(正序,默认值), desc(倒序)。可选参数
Order: "your-order",
// 设置每页容量,默认值为1000,可选参数
PageSize: 100,
// 设置页数,默认为1,可选参数
PageNo: 1,
// 设置磁盘ID,该字段非空则只返回这个磁盘的快照链信息,可选
VolumeId: "v-***",
}
if res, err := bccClient.ListSnapshotChain(args); err != nil {
fmt.Println("get snapshot chain list failed: ", err)
} else {
fmt.Println("get snapshot chain list success, Snapshots: ", res)
}
}
快照链绑定标签
使用以下代码可以给指定快照链绑定标签:
package main
import (
"fmt"
"github.com/baidubce/bce-sdk-go/services/bcc"
"github.com/baidubce/bce-sdk-go/services/bcc/api"
)
func main() {
// 设置您的ak、sk和要访问的endpoint
ak := "ak"
sk := "sk"
endpoint := "http://bcc.bj.baidubce.com"
// 创建bcc client
bccClient, _ := bcc.NewClient(ak, sk, endpoint)
tagArgs := &api.TagVolumeArgs{
ChangeTags: []api.Tag{
{
TagKey: "Key***",
TagValue: "Value***",
},
},
}
err := bccClient.TagSnapshotChain("your-chain-id", tagArgs)
if err != nil {
fmt.Println("tag snapshotChain failed:", err)
} else {
fmt.Println("tag snapshotChain success")
}
}
快照链解绑标签
使用以下代码可以给指定快照链绑定标签:
package main
import (
"fmt"
"github.com/baidubce/bce-sdk-go/services/bcc"
"github.com/baidubce/bce-sdk-go/services/bcc/api"
)
func main() {
// 设置您的ak、sk和要访问的endpoint
ak := "ak"
sk := "sk"
endpoint := "http://bcc.bj.baidubce.com"
// 创建bcc client
bccClient, _ := bcc.NewClient(ak, sk, endpoint)
tagArgs := &api.TagVolumeArgs{
ChangeTags: []api.Tag{
{
TagKey: "Key***",
TagValue: "Value***",
},
},
}
err := bccClient.UntagSnapshotChain("your-chain-id", tagArgs)
if err != nil {
fmt.Println("untag snapshotChain failed:", err)
} else {
fmt.Println("untag snapshotChain success")
}
}
跨地域复制快照
使用以下代码可以将一份磁盘快照从一个地域复制到其他地域:
package main
import (
"fmt"
"github.com/baidubce/bce-sdk-go/services/bcc"
"github.com/baidubce/bce-sdk-go/services/bcc/api"
)
func main() {
// 设置您的ak、sk和要访问的endpoint
ak := "ak"
sk := "sk"
endpoint := "bcc.bj.baidubce.com"
// 创建bcc client
bccClient, _ := bcc.NewClient(ak, sk, endpoint)
// 快照ID
snapshotId := "s-***"
args := &api.RemoteCopySnapshotArgs{
DestRegionInfos: []api.DestRegionInfo{
{
// 快照名称
Name: "your_snap_name_for_hkg",
// 目标区域
DestRegion: "hkg",
},
{
// 快照名称
Name: "your_snap_name_for_fwh",
// 目标区域
DestRegion: "fwh",
},
},
}
// 请求跨地域复制快照
result, err := bccClient.CreateRemoteCopySnapshot(snapshotId, args)
fmt.Println(err)
fmt.Println(result)
}