This topic describes how to use the SDK for Java to manage layers. For example, you can publish and delete a layer version, and query the information about a layer version.

SDK sample code

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";

        // Initialize the client. 
        FunctionComputeClient fcClient = new FunctionComputeClient(REGION, accountId, accessKey, accessSecretKey);

        // Specify the endpoint as needed, such as http://123456.cn-hangzhou.fc.aliyuncs.com. 
        // Specify the endpoint of the client in the format of http://{accountId}.{regionId}.fc.aliyuncs.com. 

        // Publish a layer version. 
        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());

        // Query the information about a specified layer version. 
        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());

        // Query layers. 
        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());
        }

        // Query the versions of a layer. 
        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());
        }

        // Delete a layer version. 
        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());
    }
}