Search in sources :

Example 1 with Code

use of com.aliyuncs.fc.model.Code in project fc-java-sdk by aliyun.

the class FunctionComputeClientTest method createFunction.

private CreateFunctionResponse createFunction(String functionName) {
    CreateFunctionRequest createFuncReq = new CreateFunctionRequest(SERVICE_NAME);
    createFuncReq.setFunctionName(functionName);
    createFuncReq.setDescription(FUNCTION_DESC_OLD);
    createFuncReq.setMemorySize(128);
    createFuncReq.setHandler("hello_world.handler");
    createFuncReq.setRuntime("nodejs4.4");
    createFuncReq.setCode(new Code().setOssBucketName(CODE_BUCKET).setOssObjectName(CODE_OBJECT));
    createFuncReq.setTimeout(10);
    return client.createFunction(createFuncReq);
}
Also used : CreateFunctionRequest(com.aliyuncs.fc.request.CreateFunctionRequest) Code(com.aliyuncs.fc.model.Code)

Example 2 with Code

use of com.aliyuncs.fc.model.Code in project fc-java-sdk by aliyun.

the class FunctionComputeClientTest method testCreateFunctionSetZipFile.

@Test
public void testCreateFunctionSetZipFile() throws IOException {
    createService(SERVICE_NAME);
    // Create a function
    CreateFunctionRequest createFuncReq = new CreateFunctionRequest(SERVICE_NAME);
    createFuncReq.setFunctionName(FUNCTION_NAME);
    createFuncReq.setDescription("Function for test");
    createFuncReq.setMemorySize(128);
    createFuncReq.setHandler("hello_world.handler");
    createFuncReq.setRuntime("nodejs4.4");
    // Setup code directory
    String tmpDir = "/tmp/fc_test_" + UUID.randomUUID();
    String funcFilePath = tmpDir + "/" + "hello_world.js";
    new File(tmpDir).mkdir();
    PrintWriter out = new PrintWriter(funcFilePath);
    out.println("'use strict'; module.exports.handler = function(event, context, callback) {console.log('hello world'); callback(null, 'hello world');};");
    out.close();
    String zipFilePath = tmpDir + "/" + "hello_world.zip";
    ZipUtils.zipDir(new File(tmpDir), zipFilePath);
    File zipFile = new File(zipFilePath);
    byte[] buffer = new byte[(int) zipFile.length()];
    FileInputStream fis = new FileInputStream(zipFilePath);
    fis.read(buffer);
    fis.close();
    Code code = new Code().setZipFile(buffer);
    createFuncReq.setCode(code);
    createFuncReq.setTimeout(10);
    client.createFunction(createFuncReq);
    // Invoke the function
    InvokeFunctionRequest request = new InvokeFunctionRequest(SERVICE_NAME, FUNCTION_NAME);
    InvokeFunctionResponse response = client.invokeFunction(request);
    assertEquals("hello world", new String(response.getContent()));
    // Cleanups
    client.deleteFunction(new DeleteFunctionRequest(SERVICE_NAME, FUNCTION_NAME));
    client.deleteService(new DeleteServiceRequest(SERVICE_NAME));
    new File(zipFilePath).delete();
    new File(funcFilePath).delete();
    new File(tmpDir).delete();
}
Also used : InvokeFunctionRequest(com.aliyuncs.fc.request.InvokeFunctionRequest) InvokeFunctionResponse(com.aliyuncs.fc.response.InvokeFunctionResponse) CreateFunctionRequest(com.aliyuncs.fc.request.CreateFunctionRequest) DeleteServiceRequest(com.aliyuncs.fc.request.DeleteServiceRequest) DeleteFunctionRequest(com.aliyuncs.fc.request.DeleteFunctionRequest) File(java.io.File) Code(com.aliyuncs.fc.model.Code) FileInputStream(java.io.FileInputStream) PrintWriter(java.io.PrintWriter) Test(org.junit.Test)

Aggregations

Code (com.aliyuncs.fc.model.Code)2 CreateFunctionRequest (com.aliyuncs.fc.request.CreateFunctionRequest)2 DeleteFunctionRequest (com.aliyuncs.fc.request.DeleteFunctionRequest)1 DeleteServiceRequest (com.aliyuncs.fc.request.DeleteServiceRequest)1 InvokeFunctionRequest (com.aliyuncs.fc.request.InvokeFunctionRequest)1 InvokeFunctionResponse (com.aliyuncs.fc.response.InvokeFunctionResponse)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 PrintWriter (java.io.PrintWriter)1 Test (org.junit.Test)1