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

SDK示例

package com.mycompany.FcSample;

import com.aliyuncs.fc.client.FunctionComputeClient;
import com.aliyuncs.fc.model.Code;
import com.aliyuncs.fc.model.Layer;
import com.aliyuncs.fc.request.*;
import com.aliyuncs.fc.response.*;

import org.apache.commons.lang.ArrayUtils;

import java.io.IOException;


public class FcSample {
    private static final String REGION = "cn-hangzhou";

    public static void main(final String[] args) throws IOException {
        String accessKey = System.getenv("ACCESS_KEY_ID");
        String accessSecretKey = System.getenv("ACCESS_KEY_SECRET");
        String accountId = System.getenv("ACCOUNT_ID");

        String layerName = "test-layer";
        String[] compatibleRuntime = {"python3", "node12"};
        String layerDir = "layer";

        // 初始化客户端。
        FunctionComputeClient fcClient = new FunctionComputeClient(REGION, accountId, accessKey, accessSecretKey);

        // 按需设置特定的接入点Endpoint,例如:http://123456.cn-hangzhou.fc.aliyuncs.com。
        // 设置函数客户端接入点Endpoint:http://{accountId}.{regionId}.fc.aliyuncs.com。

        // 发布层版本。
        PublishLayerVersionRequest plvReq = new PublishLayerVersionRequest();
        Code code = new Code().setDir(layerDir);
        plvReq.setCode(code);
        plvReq.setCompatibleRuntime(compatibleRuntime);
        plvReq.setDescription("FC layer test");
        plvReq.setLayerName(layerName);
        PublishLayerVersionResponse plvResp = fcClient.publishLayerVersion(plvReq);
        System.out.println("PublishLayerVersion, request ID " + plvResp.getRequestId() + 
            ", LayerName: " + plvResp.getLayerName() + ", LayerVersion: " + plvResp.getVersion());

        // 查询指定层版本信息。
        GetLayerVersionRequest gvReq = new GetLayerVersionRequest();
        gvReq.setLayerName(layerName);
        gvReq.setVersion(plvResp.getVersion().toString());
        GetLayerVersionResponse gvResp = fcClient.getLayerVerion(gvReq);
        System.out.println("\nGetLayerVersion, request ID " + gvResp.getRequestId() + 
            ", LayerName: " + gvResp.getLayerName() + ", LayerVersion: " + gvResp.getVersion());

        // 获取层列表。
        String nextToken = "";
        System.out.println("\nListLayerResponse:");
        Layer[] layers = new Layer[]{};
        while (true) {
            ListLayerRequest llReq = new ListLayerRequest();
            llReq.setLimit("100");
            llReq.setNextToken(nextToken);
            ListLayerResponse llResp = fcClient.listLayer(llReq);
            Layer[] tmpLayers = llResp.getLayers();
            layers = (Layer[])ArrayUtils.addAll(layers, tmpLayers);
            if (llResp.getNextToken() == null) {
                break;
            }
            nextToken = llResp.getNextToken();
        }
        for(Layer layer : layers) {
            System.out.println("- LayerName: " + (String)layer.getLayerName() +
                ", LayerMaxVersion: " + (String)layer.getVersion());
        }

        // 获取层版本列表。
        ListLayerVersionRequest llvReq = new ListLayerVersionRequest();
        llvReq.setLayerName(layerName);
        llvReq.setLimit(100);
        ListLayerVersionResponse llvResp = fcClient.listLayerVersion(llvReq);
        System.out.println("\nListLayerVersionResponse:");
       layers = llvResp.getLayers();
        for(Layer layer : layers) {
            System.out.println("LayerName: " + layer.getLayerName() + 
                ", LayerVersion: " + layer.getVersion());
        }

        // 删除层版本。
        DeleteLayerVersionRequest dlvReq = new DeleteLayerVersionRequest();
        dlvReq.setLayerName(layerName);
        dlvReq.setVersion(plvResp.getVersion().toString());
        DeleteLayerVersionReponse dlvResp = fcClient.deleteLayerVersion(dlvReq);
        System.out.println("\nDeleteLayerVersion, request ID " + plvResp.getRequestId() + 
            ", LayerName: " + plvResp.getLayerName() + ", LayerVersion: " + plvResp.getVersion());
    }
}