本文介绍使用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 {
        /*
        阿里云账号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环境变量会自动被设置。
        */
        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";

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

        // 设置函数客户端接入点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);
        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());
    }
}