This topic describes the context object in the Java runtime in Function Compute and provides sample code.

What is a context object?

When Function Compute runs a function, the system passes a context object to the method that you use to execute the function. The context object contains the information about invocations, services, functions, tracing analysis, and runtime environments. Both Event handlers and HTTP handlers support context objects as the input parameters. The formats and content are the same. The following table describes the parameters that are contained in a context object.

Parameter Description
RequestId The ID of the request. You can record the ID for troubleshooting if an error occurs.
Function The basic information about the invoked function, such as the name, handler, memory, and timeout period of the function.
Credentials

The temporary AccessKey pair that Function Compute obtains by assuming your service-linked role. The temporary AccessKey pair is valid for 36 hours. You can use credentials in your code to access related services such as Object Storage Service (OSS). This way, you can access the services without the need to write your AccessKey pair in the function code. For more information, see Grant permissions across Alibaba Cloud accounts by using a RAM role.

Logger The logger encapsulated by Function Compute.
Service The basic information of the called service.
OpenTracing The Tracing Analysis information. For more information, see Overview.

The following code shows the interface definition:

package com.aliyun.fc.runtime;

public interface Context {

    public String getRequestId();

    public Credentials getExecutionCredentials();

    public FunctionParam getFunctionParam();

    public FunctionComputeLogger getLogger();

    public Service getService();

    public OpenTracing getTracing();

    public int getRetryCount();
}

Sample program: Use a temporary key to access OSS

The following sample code provides an example on how to use a temporary key in a context to upload files to OSS. For detailed code, see java11-oss.

package example;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.aliyun.fc.runtime.Context;
import com.aliyun.fc.runtime.Credentials;
import com.aliyun.fc.runtime.StreamRequestHandler;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;

public class App implements StreamRequestHandler {

    @Override
    public void handleRequest(
            InputStream inputStream, OutputStream outputStream, Context context) throws IOException {

        // The name of the bucket, which must be created in advance. 
        String bucketName = "my-bucket";
        // Specify the endpoint of the region in which the bucket is located. Internal endpoint is recommended. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou-internal.aliyuncs.com. 
        String endpoint = "https://oss-cn-hangzhou-internal.aliyuncs.com";

        // Obtain key information. Before the execution, make sure that the service to which the function belongs is configured with the role is attached with the AliyunOSSFullAccess policy. 
        // We recommend that you use the AliyunFCDefaultRole role. 
        Credentials creds = context.getExecutionCredentials();

        // Create an OSSClient instance. 
        OSS ossClient = new OSSClientBuilder().build(endpoint, creds.getAccessKeyId(), creds.getAccessKeySecret(), creds.getSecurityToken());

        // Specify a byte array. 
        byte[] content = "Hello FC".getBytes();
        // Specify the bucket name such as examplebucket and the full path of the object such as exampledir/exampleobject.txt. The full path cannot contain the bucket name. 
        ossClient.putObject(bucketName, "exampledir/exampleobject.txt", new ByteArrayInputStream(content));

        // Close your OSSClient. 
        ossClient.shutdown();

        outputStream.write(new String("done").getBytes());
    }
}