This topic shows you how to perform operations by using Function Compute SDK for Python to call the required API operations with efficiency. For example, you can create functions and triggers, and invoke functions.

Prerequisites

Before you begin, make sure that the following operations are completed:

Sample code of the SDK for Python

The following code provides an example on how to create and delete services and functions:

# -*- coding: utf-8 -*-

import fc2

client = fc2.Client(
    endpoint='<Your Endpoint>',
    accessKeyID='<Your AccessKeyID>',
    accessKeySecret='<Your AccessKeySecret>')

// Create a service.
client.create_service('service_name')

// Set a virtual private cloud (VPC) configuration when you create the service.
vpcConfig = {
 'vpcId': '<Your Vpc Id>',
        'vSwitchIds': '<[Your VSwitch Ids]>',
        'securityGroupId': '<Your Security Group Id>'
}

// Set the VPC configuration when you create the service.
// Set a role when you set the VPC configuration.
vpc_role = 'acs:ram::12345678:role/aliyunvpcrole'

// Set an Apsara File Storage NAS configuration when you create the service.
nasConfig = {
       "userId": '<The NAS file system user id>',
       "groupId": '<The NAS file system group id>',
       "mountPoints": [
            {
                "serverAddrserverAddr" : '<The NAS file system mount target>',
                "mountDir" : '<The mount dir to the local file system>',
            }
       ],
}

service = client.create_service(name, role=vpc_role, vpcConfig=vpcConfig, nasConfig=nasConfig)

// Create a function.
// The main.zip package is in the current directory. The package contains the main.py file, which contains the myhandler function.
// Set an environment variable. Example: {'testKey': 'testValue'}.
client.create_function('service_name', 'function_name', 'python3',  'main.my_handler', codeZipFile = 'main.zip', environmentVariables = {'testKey': 'testValue'})

// Create an initializer function.
// main.my_initializer is the entry point of the initialization interface.
client.create_function('service_name', 'function_name', 'python3',  'main.my_handler', "main.my_initializer", codeZipFile = 'main.zip', environmentVariables = {'testKey': 'testValue'})

// Synchronously invoke the function.
client.invoke_function('service_name', 'function_name')

// Create a trigger.
// Create an Object Storage Service (OSS) trigger.
oss_trigger_config = {
        'events': ['oss:ObjectCreated:*'],
        'filter': {
            'key': {
                'prefix': 'prefix',
                'suffix': 'suffix'
            }
        }
}
source_arn = 'acs:oss:cn-shanghai:12345678:bucketName'
invocation_role = 'acs:ram::12345678:role/aliyunosseventnotificationrole'
client.create_trigger('service_name', 'function_name', 'trigger_name', 'oss',oss_trigger_config, source_arn, invocation_role)

// Create a Log Service trigger.
log_trigger_config = {
        'sourceConfig': {
            'logstore': 'log_store_source'
        },
        'jobConfig': {
            'triggerInterval': 60,
            'maxRetryTime': 10
        },
        'functionParameter': {},
        'logConfig': {
            'project': 'log_project',
            'logstore': 'log_store'
        },
        'enable': False
}
source_arn = 'acs:log:cn-shanghai:12345678:project/log_project'
invocation_role = 'acs:ram::12345678:role/aliyunlogetlrole'
client.create_trigger('service_name', 'function_name', 'trigger_name', 'oss',log_trigger_config, source_arn, invocation_role)
// Create a time trigger.
time_trigger_config = {
        'payload': 'awesome-fc'
        'cronExpression': '0 5 * * * *'
        'enable': true
}
client.create_trigger('service_name', 'function_name', 'trigger_name', 'timer', time_trigger_config, '', '')

// Invoke the function with input parameters.
client.invoke_function('service_name', 'function_name', payload=bytes('hello_world'))

// Read an image and invoke the function with file data as the input parameters.
src = open('src_image_file_path', 'rb') # Note: please open it as binary.
r = client.invoke_function('service_name', 'function_name', payload=src)

// Save the result as an output image.
dst = open('dst_image_file_path', 'wb')
dst.write(r.data)
src.close()
dst.close()

// Asynchronously invoke the function.
client.invoke_function('service_name', 'function_name', headers = {'x-fc-invocation-type': 'Async'})

// Query services.
client.list_services()

// Query a limited number of functions with a specific prefix.
client.list_functions('service_name', prefix='the_prefix', limit=10)

// Delete the service that you create.
client.delete_service('service_name')

// Delete the function that you create.
client.delete_function('service_name', 'function_name')