引言
在华为云上,对CDN缓存内容进行改写是一个常见的需求,以保证最新的内容能尽快被用户访问到。经过运用Go言语,咱们能够开发一个主动化的工具来完成这一需求,并将其集成到Jenkins中以完成持续布置。下面咱们将分过程讲解怎么完成。
1. 完成CDN的改写
要用Go完成华为云CDN的改写作业,咱们需要首要安装go-sdk
,这是华为云为Go开发者供给的SDK,包含了操作华为云服务的API接口。
过程1.1 安装华为云官方Go SDK
咱们能够运用go get
指令来安装SDK:
go get -u github.com/huaweicloud/huaweicloud-sdk-go-v3
过程1.2 创立CDN改写使命
运用华为云apiexplorer查看一下cdn的实例代码: 创立改写缓存使命实例,有V1 V2版别区别,可是看了一眼,目测代码没有什么区别,这儿就持续运用v1版别了:
package main
import (
"fmt"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/global"
cdn "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cdn/v1"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cdn/v1/model"
region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cdn/v1/region"
)
func main() {
// The AK and SK used for authentication are hard-coded or stored in plaintext, which has great security risks. It is recommended that the AK and SK be stored in ciphertext in configuration files or environment variables and decrypted during use to ensure security.
// In this example, AK and SK are stored in environment variables for authentication. Before running this example, set environment variables CLOUD_SDK_AK and CLOUD_SDK_SK in the local environment
ak := os.Getenv("CLOUD_SDK_AK")
sk := os.Getenv("CLOUD_SDK_SK")
auth := global.NewCredentialsBuilder().
WithAk(ak).
WithSk(sk).
Build()
client := cdn.NewCdnClient(
cdn.CdnClientBuilder().
WithRegion(region.ValueOf("cn-north-1")).
WithCredential(auth).
Build())
request := &model.CreateRefreshTasksRequest{}
request.Body = &model.RefreshTaskRequest{
}
response, err := client.CreateRefreshTasks(request)
if err == nil {
fmt.Printf("% vn", response)
} else {
fmt.Println(err)
}
}
依据上面的代码做一个简单的实例,演示怎么运用华为云Go SDK创立CDN改写使命: 域名运用 传入的方式,这儿就直接运用了os.Args,传递参数:
package main
import (
"fmt"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/global"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/config"
cdn "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cdn/v1"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cdn/v1/model"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cdn/v1/region"
"os"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run main.go <your-cdn-url>")
return
}
cdnUrl := os.Args[1]
ak := "YOUR_ACCESS_KEY"
sk := "YOUR_SECRET_KEY"
auth := global.NewCredentialsBuilder().
WithAk(ak).
WithSk(sk).
Build()
hcClient := cdn.NewCdnClient(
cdn.CdnClientBuilder().
WithRegion(region.ValueOf("cn-north-1")).
WithCredential(auth).
WithHttpConfig(config.DefaultHttpConfig()).
Build())
// Create CDN refresh task
createRefreshTask(hcClient, cdnUrl)
}
func createRefreshTask(hcClient *cdn.CdnClient, cdnUrl string) {
refreshTaskRequest := &model.CreateRefreshTasksRequest{}
typeRefreshTask := model.GetRefreshTaskRequestBodyTypeEnum().DIRECTORY
modeRefreshTask := model.GetRefreshTaskRequestBodyModeEnum().DETECT_MODIFY_REFRESH
zhUrlEncodeRefreshTask := false
refreshTaskbody := &model.RefreshTaskRequestBody{
Type: &typeRefreshTask,
Mode: &modeRefreshTask,
ZhUrlEncode: &zhUrlEncodeRefreshTask,
Urls: []string{cdnUrl},
}
refreshTaskRequest.Body = &model.RefreshTaskRequest{
RefreshTask: refreshTaskbody,
}
// Create the refresh task
response, err := hcClient.CreateRefreshTasks(refreshTaskRequest)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating CDN refresh task: %sn", err)
os.Exit(2)
}
fmt.Printf("CDN refresh task created successfully: %sn", response)
}
在上述代码中,替换**YOUR_ACCESS_KEY**
、**YOUR_SECRET_KEY**
为你的华为云账号的密钥信息。
测验运转脚本:
go run main.go https://xxx.xxx.com/
末尾以**/**单斜线结束! 注:以上代码以改写目录为例,且只改写改变资源!详细参数或许其他需求能够参考:console.huaweicloud.com/apiexplorer…,文档中参数!
2. 查询CDN file URL余量
在创立改写使命后,咱们可能还需要查询当前账户下的URL改写余量,以保证后续操作不会遭到次数约束的影响。
过程2.1 查询CDN file URL余量
能够在上面的Go程序中持续增加以下查询余量的代码片段: 参照:console.huaweicloud.com/apiexplorer… 测验调试,查看回来值数据结构,编写代码如下:
// 查询URL 目录余量
func queryCdnQuota(hcClient *cdn.CdnClient) {
request := &model.ShowQuotaRequest{}
response, err := hcClient.ShowQuota(request)
if err != nil {
fmt.Fprintf(os.Stderr, "Error querying CDN quota: %sn", err)
os.Exit(2)
}
// 请保证 response.Quotas 不是nil,否则可能会导致空指针异常
if response.Quotas == nil {
fmt.Fprintln(os.Stderr, "Error: received nil Quotas in response")
os.Exit(2)
}
// 自定义类型名称的映射
typeNameMap := map[string]string{
"file_refresh": "缓存改写剩下Url条数",
"dir_refresh": "缓存改写剩下目录数",
}
// Print out the customized quota information
fmt.Println("CDN quota information:")
for _, quota := range *response.Quotas {
// 查看quota.Type是否为咱们关怀的类型之一
if customName, ok := typeNameMap[*quota.Type]; ok {
remaining := *quota.QuotaLimit - *quota.Used
fmt.Printf("- %s: %dn", customName, remaining)
}
}
}
在main函数中增加一下代码:
queryCdnQuota(hcClient)
此片段會在创立CDN改写使命之后调用ShowQuota
接口,查询并打印出当前账户的URL以及目录改写余量信息。typeNameMap部分是我想自定义一下输出打印的名称,增加可读性!
完成代码如下:
package main
import (
"fmt"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/global"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/config"
cdn "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cdn/v1"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cdn/v1/model"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cdn/v1/region"
"os"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run main.go <your-cdn-url>")
return
}
cdnUrl := os.Args[1]
ak := "YOUR_ACCESS_KEY"
sk := "YOUR_SECRET_KEY"
auth := global.NewCredentialsBuilder().
WithAk(ak).
WithSk(sk).
Build()
hcClient := cdn.NewCdnClient(
cdn.CdnClientBuilder().
WithRegion(region.ValueOf("cn-north-1")).
WithCredential(auth).
WithHttpConfig(config.DefaultHttpConfig()).
Build())
// Create CDN refresh task
createRefreshTask(hcClient, cdnUrl)
}
func createRefreshTask(hcClient *cdn.CdnClient, cdnUrl string) {
refreshTaskRequest := &model.CreateRefreshTasksRequest{}
typeRefreshTask := model.GetRefreshTaskRequestBodyTypeEnum().DIRECTORY
modeRefreshTask := model.GetRefreshTaskRequestBodyModeEnum().DETECT_MODIFY_REFRESH
zhUrlEncodeRefreshTask := false
refreshTaskbody := &model.RefreshTaskRequestBody{
Type: &typeRefreshTask,
Mode: &modeRefreshTask,
ZhUrlEncode: &zhUrlEncodeRefreshTask,
Urls: []string{cdnUrl},
}
refreshTaskRequest.Body = &model.RefreshTaskRequest{
RefreshTask: refreshTaskbody,
}
// Create the refresh task
response, err := hcClient.CreateRefreshTasks(refreshTaskRequest)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating CDN refresh task: %sn", err)
os.Exit(2)
}
fmt.Printf("CDN refresh task created successfully: %sn", response)
}
运转代码回来数据格局如下:
go run main.go https://xxx.xxx.com/
3. 在Jenkins节点上运转程序
一旦咱们的Go程序能够正确履行CDN改写和查询余量的操作,接下来的过程就是在Jenkins中装备该程序的运转环境。
过程3.1 创立一个新的Jenkins使命
在Jenkins中创立一个Freestyle项目或许Pipeline项目,此部分取决于你的作业流。为了简便阐明,咱们这儿以Freestyle项目为例。
过程3.2 装备构建过程
首要增加一个参数化构建过程,传入参数: 约束一下可运转的节点:
在你的Jenkins使命装备页中,增加一个构建过程,挑选“Execute shell”(关于Linux体系)或“Execute Windows batch command”(关于Windows体系),并填入以下内容:
# 假定你的Go程序名为`main`,且已经编译到Jenkins的作业空间中
cd /home/flush-hw&&./main $dir
保证构建环境中已经安装了Go运转时,并且环境变量已经装备,这样main
程序才能够在Jenkins节点上运转没有问题。
注:main要有可履行权限,复制过来要记住chmod x main .
4. 运用Jenkins Credentials办理AK/SK密钥
为了防止在代码中硬编码灵敏信息,如Access Key和Secret Key,引荐运用Jenkins的Credentials插件来办理这些密钥。
过程4.1 增加Credentials
在Jenkins中进入Credentials办理页面,增加一个新的Credentials,挑选“Secret text”,其间**Secret**
字段填入AK:SK的格局。记住这儿的**ID,**接下来绑定的时分会用到
过程4.2 修正Go程序以获取Credentials
之后,你需要修正Go程序,让其从环境变量中读取AK和SK。例如:
package main
import (
"fmt"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/global"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/core/config"
cdn "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cdn/v1"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cdn/v1/model"
"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/cdn/v1/region"
"os"
"strings"
)
func main() {
if len(os.Args) < 2 {
fmt.Println("Usage: go run main.go <your-cdn-url>")
return
}
cdnUrl := os.Args[1]
// 测验从环境变量中获取ak和sk
creds := os.Getenv("huaweiyun-hn-cdn")
if creds == "" {
fmt.Fprintln(os.Stderr, "Error: Credentials environment variable is not set.")
os.Exit(1)
}
parts := strings.SplitN(creds, ":", 2)
if len(parts) != 2 {
fmt.Fprintln(os.Stderr, "Error: Invalid credential format. Expected 'AK:SK'.")
os.Exit(1)
}
ak, sk := parts[0], parts[1]
auth := global.NewCredentialsBuilder().
WithAk(ak).
WithSk(sk).
Build()
hcClient := cdn.NewCdnClient(
cdn.CdnClientBuilder().
WithRegion(region.ValueOf("cn-north-1")).
WithCredential(auth).
WithHttpConfig(config.DefaultHttpConfig()).
Build())
// Create CDN refresh task
createRefreshTask(hcClient, cdnUrl)
// Query remaining refresh and preload quota
queryCdnQuota(hcClient)
}
func createRefreshTask(hcClient *cdn.CdnClient, cdnUrl string) {
refreshTaskRequest := &model.CreateRefreshTasksRequest{}
typeRefreshTask := model.GetRefreshTaskRequestBodyTypeEnum().DIRECTORY
modeRefreshTask := model.GetRefreshTaskRequestBodyModeEnum().DETECT_MODIFY_REFRESH
zhUrlEncodeRefreshTask := false
refreshTaskbody := &model.RefreshTaskRequestBody{
Type: &typeRefreshTask,
Mode: &modeRefreshTask,
ZhUrlEncode: &zhUrlEncodeRefreshTask,
Urls: []string{cdnUrl},
}
refreshTaskRequest.Body = &model.RefreshTaskRequest{
RefreshTask: refreshTaskbody,
}
// Create the refresh task
response, err := hcClient.CreateRefreshTasks(refreshTaskRequest)
if err != nil {
fmt.Fprintf(os.Stderr, "Error creating CDN refresh task: %sn", err)
os.Exit(2)
}
fmt.Printf("CDN refresh task created successfully: %sn", response)
}
func queryCdnQuota(hcClient *cdn.CdnClient) {
request := &model.ShowQuotaRequest{}
response, err := hcClient.ShowQuota(request)
if err != nil {
fmt.Fprintf(os.Stderr, "Error querying CDN quota: %sn", err)
os.Exit(2)
}
// 请保证 response.Quotas 不是nil,否则可能会导致空指针异常
if response.Quotas == nil {
fmt.Fprintln(os.Stderr, "Error: received nil Quotas in response")
os.Exit(2)
}
// 自定义类型名称的映射
typeNameMap := map[string]string{
"file_refresh": "缓存改写剩下Url条数",
"dir_refresh": "缓存改写剩下目录数",
}
// Print out the customized quota information
fmt.Println("CDN quota information:")
for _, quota := range *response.Quotas {
// 查看quota.Type是否为咱们关怀的类型之一
if customName, ok := typeNameMap[*quota.Type]; ok {
remaining := *quota.QuotaLimit - *quota.Used
fmt.Printf("- %s: %dn", customName, remaining)
}
}
}
过程4.3 装备Jenkins使命以传递Credentials
在Jenkins使命的构建环境装备中,运用Credentials Binding插件将新增加的Credentials绑定到相应的环境变量中。在“Build Environment”挑选“Use secret text(s) or file(s)”,命名为xxx并绑定AK/SK到指定凭据:
过程4.4 测试运转
最后,在Jenkins中运转装备好的使命,查看输出以保证CDN改写和余量查询均运转顺畅。
5. 其他的需求
经常输入域名的时分忘掉/单斜线,是否能够主动补全?
设置改阈值?剩下量到20能够主动报警?邮件or短信
高雅的pipeline?
结语
经过上述过程,咱们成功地完成了经过Go言语操作华为云CDN改写服务的功能,并将其集成到Jenkins使命中,一起安全地办理了灵敏的AK/SK凭据。这样的主动化工具关于办理很多的CDN资源来说非常有用,能够大大提高作业效率。 注:以上大纲chatgpt生成代码结构也是,代码基本也是chatgpt生成,形似中心就有几个&指针数据格局有问题修正了一下