To use Python for programming in Function Compute, you must define a Python function as the handler function. This topic describes Python event functions.
Background information
Function Compute supports the following Python runtime environments:
- Python 2.7 with Python 2.7 runtime
- Python 3.6 with Python 3 runtime
Definition
The following example shows the definition of a simple handler function, which is an event function:
def my_handler(event, context):
return 'hello world'
Function name
my_handler
must correspond to the handler
field that you specify when you create a function. For example, if you set handler
to main.my_handler
when you create an event function, Function Compute loads the my_handler
function defined in the main.py
file.
The event parameter
- The event parameter indicates the data that is passed in when you call a function.
In Python 2.7, this parameter is of the
str
type. In Python 3, this parameter is of thebytes
type. The event parameter is an input parameter of the function. - You can convert this parameter as needed because it is not defined by the function.
For example, if a JSON string is passed in, you can convert it to a data dictionary.
{ "key": "value" }
For example, event
is passed in and value
is returned.
# -*- coding: utf-8 -*-
import json
def my_handler(event, context):
evt = json.loads(event)
return evt['key']
The context parameter
The context parameter contains the runtime information of a function, such as the
request ID
and temporary AccessKey pair
. You can use the runtime information in your code. This parameter is of the FCContext
type.
The context parameter is defined as follows:
class Credentials:
def __init__(self, access_key_id, access_key_secret, security_token):
self.access_key_id = access_key_id
self.access_key_secret = access_key_secret
self.security_token = security_token
class ServiceMeta:
def __init__(self, service_name, log_project, log_store, qualifier, version_id):
self.name = service_name
self.log_project = log_project
self.log_store = log_store
self.qualifier = qualifier
self.version_id = version_id
class FunctionMeta:
def __init__(self, name, handler, memory, timeout, initializer, initialization_timeout):
self.name = name
self.handler = handler
self.memory = memory
self.timeout = timeout
self.initializer = initializer
self.initialization_timeout = initialization_timeout
class FCContext:
def __init__(self, account_id, request_id, credentials, function_meta, service_meta, region):
self.request_id = request_id
self.credentials = credentials
self.function = function_meta
self.service = service_meta
self.region = region
self.account_id = account_id
The following table lists the fields contained in the context
parameter.
Field | Description |
---|---|
requestId | The ID of the call request. You can record the ID for troubleshooting if an error occurs. |
function | Basic information of the current function, such as the name, handler, memory, and timeout period of the function. |
credentials | The temporary AccessKey pair obtained by Function Compute from assuming your service role. The temporary AccessKey pair is valid for six hours. For more information, see Permissions for services. You can use credentials in your code to access the related service such as Object Storage Service (OSS). This allows you not to write your AccessKey pair in the function code. |
service | The information about the service to which the function belongs. This field contains the service name, the project and Logstore information of Log Service to which the service is connected, and qualifier and version_id that indicate the service version information. The qualifier parameter indicates the service version or alias that is specified when the function is called. The version_id parameter indicates the version of the service that is called. |
region | The region to which the function applies, such as cn-shanghai. |
accountId | The ID of the Alibaba Cloud account that calls the function. |
The following sample code shows how to upload a file to OSS by using a temporary AccessKey pair.
import json
import oss2
def my_handler(event, context):
evt = json.loads(event)
creds = context.credentials
# do not forget security_token
auth = oss2.StsAuth(creds.access_key_id, creds.access_key_secret, creds.security_token)
bucket = oss2.Bucket(auth, evt['endpoint'], evt['bucket'])
bucket.put_object(evt['objectName'], evt['message'])
return 'success'