All Products
Search
Document Center

Template Use Guide

Last Updated: Oct 16, 2018

The Function Compute templates provide sample codes that are applicable to multiple scenarios. You can modify the sample code or directly use the template. The template description is displayed on the template selection page on the console, including template input, output and precautions. In addition, some templates can be used only after the corresponding triggers are configured, for example, obtain an OSS object, download and decompress it. The following lists official templates, allowing you to easily check related information.

api-gateway-nodejs6 (API Gateway return strings)

This template is used as API Gateway backend, which shows how to return different data, including HTML pages, JSON strings, and image formats.

  • Input parameters
    {“queryParameters”:{“response”:”html|json|image”}}
  • Output parameters
    html, or json, or image

get-object-meta (obtain object attributes)

After an object with a specified prefix or suffix is uploaded to a bucket, the function obtains the region, bucket name, and object name using the event, writes the obtained object meta information to a file, and uploads the file to a specified bucket.

  • Input parameters
    Upload a file over the Post Object API of OSS and trigger function execution. This API is generally called when files are uploaded through the OSS console. The function can obtain the region, bucket name, object name, and other information about your updated file from the event parameter.
  • Output parameters
    A file used to record the meta information of the uploaded object is generated and uploaded to the specified OSS bucket.
  • Precautions
    When running this example, you must set the OSS trigger with the trigger event oss: ObjectCreated: PostObject. Meanwhile, you must add the Get Object and Put Object permissions to the service role. When you create a service role, Function Compute is allowed by default to write logs to the specified logstore.

image-crawler (image crawlers)

The image demo is crawled by network crawlers. After a URL is provided, all .jpg files are crawled and stored in OSS.

  • Input parameters
  1. url: Any URL that contains images
  2. EndPoint: Endpoint of the input bucket
  3. BucketName: Name of the input bucket
  • Output parameters
    Result: ok: The operation succeeds. | error: An error occurs.
  • Precautions
    You must activate Log Service and grant permissions to the function to view logs. You also need to activate OSS and grant the read and write permissions.
  • Permission policies
    AliyunOSSFullAccess and AliyunLogFullAccess

The preceding service-level role policies must be added to your service role. If your role already contains the policies, ignore this operation.

image-recognition (image recognition)

Scenario: The images are automatically classified. You can upload the images through OSS, trigger Function Compute for image recognition, and analyze the classified storage solution based on the recognition result.

  • Procedure
    Step 1: Trigger Function Compute using the Put or Post OSS event.
    Step 2: Function Compute calls Alibaba Cloud Image Recognition to analyze the image content in real time.
    Step 3: Function Compute resolves the recognition results, classifies the results, and stores them in different OSS buckets. Prerequisites: Step 1: Activate Log Service and authorize Function Compute. Step 2: Activate OSS and create three buckets, including onebucketforwatching, twobucketsfor’plant, and animal’image.
  • Input parameters
  1. EndPoint: Endpoint of the input bucket
  2. BucketName: Name of the input bucket
  • Output parameters
    Result: ok: The operation succeeds.|error: An error occurs.
  • Precautions
    You must activate Log Service, Image Recognition, and OSS, and grant permissions to the function to view logs. You also need to authorize the role to read and write OSS.
  • Permission policies: Add the following permissions:
  1. AliyunOSSFullAccess
  2. AliyunLogFullAccess

logstore-replication (log backup)

This function subscribes to the real-time data of Log Service logstore, and copies data to another logstore. For more information, see aliyun-log-fc-functions.

  • Input parameters
    functioinevent is obtained based on shardcurosr added to logstore. The input parameters specify the start and end locations of the shard where the function reads data, and provide additional configurations such as data processing content and data writing location for the custom logic of the function. When data is written to the shard, Log Service triggers scheduled function execution.

  • Output parameters
    The successfully executed function copies data of the source logstore, and returns a JSONObject-serialized string, including ingestLines (number of lines of the read log), ingestBytes (number of bytes of the read log), shipLines (number of lines of the output log), and shipBytes (number of bytes of the output log). If any exception occurs during execution, for example, the function fails to read or write data to logstore, an IOException is thrown to terminate the function.

  • Precautions
    This function reads data and write the data to logstore in batches. You are advised to set the memory size of the function to a value greater than 384 MB. If the logging traffic on a single shard in the logstore is heavy, we recommend that you set a time-out longer than 120 s.

  • Permission policies: Add the following permissions:
  1. AliyunLogFullAccess

marketplace-weather-demo (use the marketplace)

Function Compute provides you with service packages in the marketplace. In this example, the weather forecast service of a supplier is used to provide free weather report, air quality, and clothes forecast. [NOTE]: You must activate the weather forecast service of the supplier in the marketplace. You can obtain the service appcode on the console to use this application.

  • Input parameters
    appcode: Enter the product appcode provided by the supplier.

  • Output parameters
    content: The overall weather forecast, air quality, and other data are returned in JSON format.

oss-download-zip-upload (OSS object download and compression)

By using this sample template, you can download images from a bucket of OSS, pack the images into a ZIP file, and upload the ZIP file to a bucket. You must set the role access policy to allow reading and writing OSS for the service corresponding to the function.

  • Input parameters
  1. EndPoint: Endpoint of the input bucket
  2. BucketName: Name of the input bucket
  • Output parameters
    ZipFileSize: Size of the packed file, in byte.

  • Permission policies: Add the following permissions:

  1. AliyunOSSFullAccess
  2. AliyunLogFullAccess

oss-get-object-info (obtain object information)

By using this template function, you can obtain the details of the object in the specified bucket of OSS. You must set the role access policy to allow reading OSS for the service corresponding to the function.

  • Input parameters
  1. EndPoint: Endpoint of the input bucket
  2. BucketName: Name of the input bucket
  • Output parameters
  1. last_modified: Last update time of the object
  2. key: Object name
  • Permission policies: Add the following permissions:
  1. AliyunOSSReadOnlyAccess
  2. AliyunLogFullAccess

read-write-tablestore-nodejs6 (read and write Table Store)

Read and write Table Store in the function: Write a line of data using GetRow and read the line of data using GetRow.

  • Input parameters
    None

  • Output parameters
    GetRow result

  • Precautions
    You must activate Table Store and create instances and tables in advance.

  • Permission policies: Add the following permissions:

  1. AliyunOTSFullAccess
  2. AliyunLogFullAccess

read-write-tablestore-python27 (read and write Table Store)

Read and write Table Store in the function: Write a line of data using GetRow and read the line of data using GetRow.

  • Input parameters
    None

  • Output parameters
    GetRow result

  • Precautions
    You must activate Table Store and create instances and tables in advance.

  • Permission policies: Add the following permissions:

  1. AliyunOTSFullAccess
  2. AliyunLogFullAccess

return-apigateway-string (API Gateway return string)

By using this example, you can learn about how to use Function Compute to obtain parameters transmitted from API Gateway, and how to use Function Compute to create a standard API Gateway response string.

  • Input parameters
    APIPara: Configure request parameters in API Gateway.

  • Output parameters
    api_rep: The standard API Gateway response string is returned. >api_rep: The standard API Gateway response string is returned.