This topic describes the context in the C# runtime in Function Compute and provides sample code.
What is context?
When Function Compute runs a function, the system passes a context object to the method that is used 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 fields that are supported by the context object.
Field | 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 |
Logger | The logger encapsulated by Function Compute. |
Service | The basic information of the called service. |
OpenTracing | The information about Tracing Analysis. For more information, see Overview. |
public interface IFcContext
{
/// <summary>
/// The AliFc request ID associated with the request.
/// This is the same ID returned to the client that called invoke().
/// This ID is reused for retries on the same request.
/// </summary>
string RequestId { get; }
/// <summary>
/// Gets the function parameter interface.
/// </summary>
/// <value>The function parameter interface.</value>
IFunctionParameter FunctionParam {get;}
/// <summary>
/// AliFc logger associated with the Context object.
/// </summary>
IFcLogger Logger { get; }
/// <summary>
/// Gets the credentials interface.
/// </summary>
/// <value>The credentials interface.</value>
ICredentials Credentials {get;}
/// <summary>
/// Gets the account identifier.
/// </summary>
/// <value>The account identifier.</value>
string AccountId { get; }
/// <summary>
/// Gets the region.
/// </summary>
/// <value>The region.</value>
string Region { get; }
/// <summary>
/// Gets the service meta.
/// </summary>
/// <value>The service meta.</value>
IServiceMeta ServiceMeta { get; }
}
Sample program: Use a temporary key to access OSS
The following sample program provides an example on how to use a temporary key in the context to upload a file to OSS. For more information, see dotnet3-oss.
using System;
using System.IO;
using Aliyun.OSS;
using Aliyun.Serverless.Core;
using Microsoft.Extensions.Logging;
namespace Example
{
public class OssExample
{
public Stream HandleRequest(Stream stream, IFcContext context)
{
context.Logger.LogInformation("Handle request: {0}", context.RequestId);
// The name of the bucket, which must be created in advance.
string bucketName = "my-****";
// The path of the object.
string objectName = "exampledir/exampleobject.txt";
// Specify the endpoint of the region in which the bucket is located. We recommend that you use an internal endpoint. In this example, the internal endpoint https://oss-cn-hangzhou-internal.aliyuncs.com of the China (Hangzhou) region is used.
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 that is attached with the AliyunOSSFullAccess policy.
// We recommend that you use the AliyunFCDefaultRole role.
ICredentials creds = context.Credentials;
// Create an OSSClient instance.
OssClient ossClient =
new OssClient(endpoint,
creds.AccessKeyId,
creds.AccessKeySecret,
creds.SecurityToken);
// Specify the bucket name and the full path of the object. In this example, the bucket name is examplebucket and the full path of the object is exampledir/exampleobject.txt.
ossClient
.PutObject(bucketName,
objectName,
stream);
OssObject obj = ossClient.GetObject(bucketName,objectName);
context.Logger.LogInformation("Put object to oss success: {0}", obj.ToString());
return obj.Content;
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");
}
}
}