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

SDK example

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 {
        /*
        The AccessKey pair of an Alibaba Cloud account can be used to access all API operations. Using these credentials to perform operations in Function Compute is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. 
        We recommend that you do not save the AccessKey ID and AccessKey secret to your project code. Otherwise, the AccessKey pair may be leaked and the security of all resources in your account may be compromised. 
        In this example, the AccessKey pair is saved to the environment variables for authentication. 
        Configure the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables in your local environment before you run the sample code. 
        In the runtime environments of Function Compute, the ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET environment variables are automatically configured after you configure the execution permissions. 
        */
        String accessKey = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
        String accessSecretKey = System.getenv("ALIBABA_CLOUD_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 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 the 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 layer versions. 
        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);
        String deleteLayerVersion = plvResp.getVersion().toString();
        dlvReq.setVersion(deleteLayerVersion);
        DeleteLayerVersionReponse dlvResp = fcClient.deleteLayerVersion(dlvReq);
        System.out.println("\nDeleteLayerVersion, request ID " + dlvResp.getRequestId() +
            ", LayerName: " + gvResp.getLayerName() + ", LayerVersion: " + gvResp.getVersion());
    }
}