本文介绍如何使用SDK对层版本执行发布、查询及删除等操作的代码示例。

SDK示例

/*
    阿里云账号AccessKey拥有所有API的访问权限,建议您使用RAM用户进行API访问或日常运维。
    建议不要把AccessKey ID和AccessKey Secret保存到工程代码里,否则可能导致AccessKey泄露,威胁您账号下所有资源的安全。
    本示例以将AccessKey和AccessSecretKey保存在环境变量中实现身份验证为例。
    运行本示例前请先在本地环境中设置环境变量ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET。
    在FC Runtime运行环境下,配置执行权限后,ALIBABA_CLOUD_ACCESS_KEY_ID和ALIBABA_CLOUD_ACCESS_KEY_SECRET环境变量会自动被设置。
*/
client, _ := fc.NewClient(
    os.Getenv("ENDPOINT"), "2016-08-15",
    os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_ID"),
    os.Getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET"),
    fc.WithTransport(&http.Transport{MaxIdleConnsPerHost: 100}))

// 层名称。
layerName := "test-layer"
// 准备ZIP格式的层文件。
layZipFile := "./hello_world.zip"
// 指定兼容的运行时环境。
compatibleRuntime := []string{"python3", "nodejs12"}

// 发布层版本。
fmt.Println("Publish layer versions")
data, err := ioutil.ReadFile(layZipFile)
if err != nil {
    fmt.Fprintln(os.Stderr, err)
    return
}
publishLayerVersionOutput, err := client.PublishLayerVersion(fc.NewPublishLayerVersionInput().
    WithLayerName(layerName).
    WithCode(fc.NewCode().WithZipFile(data)).
    WithCompatibleRuntime(compatibleRuntime).
    WithDescription("my layer"),
)
if err != nil {
    fmt.Fprintln(os.Stderr, err)
} else {
    fmt.Printf("PublishLayerVersion response: %+v \n\n", publishLayerVersionOutput)
}

// 查询指定层版本信息。
fmt.Printf("Get the layer of version %d\n", publishLayerVersionOutput.Layer.Version)
getLayerVersionOutput, err := client.GetLayerVersion(
    fc.NewGetLayerVersionInput(layerName, publishLayerVersionOutput.Layer.Version))
if err != nil {
    fmt.Fprintln(os.Stderr, err)
} else {
    fmt.Printf("GetLayerVersion response: %+v \n\n", getLayerVersionOutput.Layer)
}

// 获取层列表。
fmt.Println("List layers")
nextToken := ""
layers := []*fc.Layer{}
for {
    listLayersOutput, err := client.ListLayers(
        fc.NewListLayersInput().
            WithLimit(100).
            WithNextToken(nextToken))
    if err != nil {
        fmt.Fprintln(os.Stderr, err)
        break
    }
    if len(listLayersOutput.Layers) != 0 {
        layers = append(layers, listLayersOutput.Layers...)
    }
    if listLayersOutput.NextToken == nil {
        break
    }
    nextToken = *listLayersOutput.NextToken

}
fmt.Println("ListLayers response:")
for _, layer := range layers {
    fmt.Printf("- layerName: %s, layerMaxVersion: %d\n", layer.LayerName, layer.Version)
}

// 获取层版本列表。
fmt.Println("List layer versions")
// 层的起始版本,默认从1开始。
startVersion := int32(1)
fmt.Println("ListLayerVersions response:")
layerVersions := []*fc.Layer{}
for {
    listLayerVersionsOutput, err := client.ListLayerVersions(
        fc.NewListLayerVersionsInput(layerName, startVersion).
            WithLimit(100))
    if err != nil {
        if err, ok := err.(*fc.ServiceError); ok &&
            err.HTTPStatus == http.StatusNotFound {
            break
        }
        fmt.Fprintln(os.Stderr, err)
        break
    }
    if len(listLayerVersionsOutput.Layers) > 0 {
        layerVersions = append(layerVersions, listLayerVersionsOutput.Layers...)
    }
    if listLayerVersionsOutput.NextVersion == nil ||
        *listLayerVersionsOutput.NextVersion == 0 {
        break
    }
    startVersion = *listLayerVersionsOutput.NextVersion
}

for _, layer := range layerVersions {
    fmt.Printf("- layerName: %s, layerVersion: %d\n", layer.LayerName, layer.Version)
}

// 删除层版本。
fmt.Printf("Delete the layer of version %d \n", publishLayerVersionOutput.Layer.Version)
deleteLayerVersionOutput, err := client.DeleteLayerVersion(
    fc.NewDeleteLayerVersionInput(layerName, publishLayerVersionOutput.Layer.Version))
if err != nil {
    fmt.Fprintln(os.Stderr, err)
} else {
    fmt.Printf("DeleteLayerVersion response: %+v \n\n", deleteLayerVersionOutput)
}