更新时间:2023-11-21 GMT+08:00

创建实例示例

本章节对创建实例AK/SK方式使用SDK进行示例说明。

创建实例示例代码只需将AK/SK信息替换为实际AK/SK,代码中可以使用初始化CreateInstanceReq的Name和Model配置实例名称和模型,配置完成后运行即可。

  1. 创建实例调用示例代码如下:
    package main
     
    import (
        "fmt"
        "github.com/huaweicloud/huaweicloud-sdk-go-v3/core/auth/basic"
         imagesearch "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/imagesearch/v1"
        "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/imagesearch/v1/model"
         region "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/imagesearch/v1/region"
    )
     
    func main() {
       // 认证用的ak和sk硬编码到代码中或者明文存储都有很大的安全风险,建议在配置文件或者环境变量中密文存放,使用时解密,确保安全;
       // 本示例以ak和sk保存在环境变量中来实现身份验证为例,运行本示例前请先在本地环境中设置环境变量HUAWEICLOUD_SDK_AK和HUAWEICLOUD_SDK_SK。
       ak := os.Getenv("HUAWEICLOUD_SDK_AK")
       sk := os.Getenv("HUAWEICLOUD_SDK_SK")
     
        auth := basic.NewCredentialsBuilder().
            WithAk(ak).
            WithSk(sk).
            Build()
        //设置请求客户端,示例中region为北京四
        client := imagesearch.NewImageSearchClient(
            imagesearch.ImagesearchClientBuilder().
                WithRegion(region.ValueOf("cn-north-4")).
                WithCredential(auth).
                Build())
        //设置实例名称和模型
    request := &model.RunCreateInstanceRequest{}
    tags := []string{"animal", "plant"}
        request.Body = &model.CreateInstanceReq{
            Name: "instance-name",
            Model: "common-search",
             Tags: &tags,
        }
        response, err := client.RunCreateInstance(request)
        if err == nil {
            fmt.Printf("%+v\n", response)
        } else {
            fmt.Println(err)
        }
    }
  2. 创建实例结果输出到控制台表示执行成功。创建实例结果输出到控制台。
    RunCreateInstanceResponse{"domain": "通用图片搜索","desc":"","registerDate":1638321711363,"expiredDate":-1,"level":30000000,"status":"NORMAL","instanceName":"instance-name"}