MCPcopy Index your code
hub / github.com/aliyun/fc-java-sdk

github.com/aliyun/fc-java-sdk @1.8.18

Chat with this repo
repository ↗ · DeepWiki ↗ · release 1.8.18 ↗ · + Follow
1,951 symbols 5,192 edges 206 files 106 documented · 5%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

fc-java-sdk

maven version build status

Requirements

  • Java 1.6 and above

License

MIT

Install

Add Maven dependencies into pom.xml

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-fc</artifactId>
    <version>1.8.18</version>
</dependency>

Example

Create the code directory and write counter nodejs code

mkdir /tmp/fc_code
cat <<EOF > /tmp/fc_code/counter.js
'use strict';

var counter = 0;
exports.initializer = function(ctx, callback) {
    ++counter;
    callback(null, "");
};

exports.handler = function(event, context, callback) {
  console.log('counter is %d', counter);
  callback(null, String(counter));
};
EOF

Run below with your own ENDPOINT, ACCESS_KEY/SECRET_KEY and ACCOUNT_ID environment variables

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

import java.io.IOException;
import java.net.HttpURLConnection;

public class FcSample {
    private static final String CODE_DIR = "/tmp/fc_code";
    private static final String REGION = "cn-shanghai";
    private static final String SERVICE_NAME = "test_service";
    private static final String FUNCTION_NAME = "test_function";

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

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

        // Set to a specific endpoint in case needed, endpoint sample: http://123456.cn-hangzhou.fc.aliyuncs.com
        // fcClient.setEndpoint("http://{accountId}.{regionId}.fc.aliyuncs.com.");

        // Create a service
        CreateServiceRequest csReq = new CreateServiceRequest();
        csReq.setServiceName(SERVICE_NAME);
        csReq.setDescription("FC test service");
        csReq.setRole(role);
        CreateServiceResponse csResp = fcClient.createService(csReq);
        System.out.println("Created service, request ID " + csResp.getRequestId());

        // Create a function
        CreateFunctionRequest cfReq = new CreateFunctionRequest(SERVICE_NAME);
        cfReq.setFunctionName(FUNCTION_NAME);
        cfReq.setDescription("Function for test");
        cfReq.setMemorySize(128);
        cfReq.setRuntime("nodejs4.4");
        cfReq.setHandler("counter.handler");
        // Used in initializer situations.
        cfReq.setInitializer("counter.initializer");
        Code code = new Code().setDir(CODE_DIR);
        cfReq.setCode(code);
        cfReq.setTimeout(10);
        CreateFunctionResponse cfResp = fcClient.createFunction(cfReq);
        System.out.println("Created function, request ID " + cfResp.getRequestId());

        // Invoke the function with a string as function event parameter, Sync mode
        InvokeFunctionRequest invkReq = new InvokeFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
        String payload = "Hello FunctionCompute!";
        invkReq.setPayload(payload.getBytes());
        InvokeFunctionResponse invkResp = fcClient.invokeFunction(invkReq);
        System.out.println(new String(invkResp.getContent()));

        // Invoke the function, Async mode
        invkReq.setInvocationType(Const.INVOCATION_TYPE_ASYNC);
        invkResp = fcClient.invokeFunction(invkReq);
        if (HttpURLConnection.HTTP_ACCEPTED == invkResp.getStatus()) {
            System.out.println("Async invocation has been queued for execution, request ID: " + invkResp.getRequestId());
        } else {
            System.out.println("Async invocation was not accepted");
        }
        // Delete the function
        DeleteFunctionRequest dfReq = new DeleteFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
        DeleteFunctionResponse dfResp = fcClient.deleteFunction(dfReq);
        System.out.println("Deleted function, request ID " + dfResp.getRequestId());

        // Delete the service
        DeleteServiceRequest dsReq = new DeleteServiceRequest(SERVICE_NAME);
        DeleteServiceResponse dsResp = fcClient.deleteService(dsReq);
        System.out.println("Deleted service, request ID " + dsResp.getRequestId());
    }
}

API Spec

See: https://help.aliyun.com/document_detail/52877.html

Extension points exported contracts — how you extend this code

FcCallback (Interface)
(no doc) [4 implementers]
src/main/java/com/aliyuncs/fc/client/FcCallback.java
AsyncClientInterface (Interface)
(no doc) [2 implementers]
src/main/java/com/aliyuncs/fc/client/AsyncClientInterface.java

Core symbols most depended-on inside this repo

getStatus
called by 107
src/main/java/com/aliyuncs/fc/http/HttpResponse.java
getContent
called by 105
src/main/java/com/aliyuncs/fc/http/HttpResponse.java
get
called by 79
src/main/java/com/aliyuncs/fc/core/FutureCallback.java
setStatus
called by 57
src/main/java/com/aliyuncs/fc/http/HttpResponse.java
doAction
called by 55
src/main/java/com/aliyuncs/fc/client/DefaultFcClient.java
setContent
called by 50
src/main/java/com/aliyuncs/fc/http/HttpResponse.java
getMessage
called by 49
src/main/java/com/aliyuncs/fc/exceptions/ClientException.java
setHeaders
called by 48
src/main/java/com/aliyuncs/fc/http/HttpResponse.java

Shape

Method 1,737
Class 208
Enum 4
Interface 2

Languages

Java100%

Modules by API surface

src/test/java/com/aliyuncs/fc/FunctionComputeClientTest.java94 symbols
src/main/java/com/aliyuncs/fc/client/FunctionComputeClient.java60 symbols
src/main/java/com/aliyuncs/fc/request/CreateFunctionRequest.java41 symbols
src/main/java/com/aliyuncs/fc/request/UpdateFunctionRequest.java40 symbols
src/main/java/com/aliyuncs/fc/config/Config.java38 symbols
src/main/java/com/aliyuncs/fc/model/LogTriggerConfig.java33 symbols
src/test/java/com/aliyuncs/fc/client/AsyncClientTest.java26 symbols
src/main/java/com/aliyuncs/fc/response/ListLayerVersionResponse.java26 symbols
src/main/java/com/aliyuncs/fc/response/ListLayerResponse.java26 symbols
src/main/java/com/aliyuncs/fc/request/UpdateServiceRequest.java25 symbols
src/main/java/com/aliyuncs/fc/request/CreateServiceRequest.java23 symbols
src/main/java/com/aliyuncs/fc/model/FunctionMetadata.java23 symbols

For agents

$ claude mcp add fc-java-sdk \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact