Updated on 2023-11-21 GMT+08:00

Dependent Libraries

Supported Dependent Libraries

FunctionGraph supports both standard and third-party libraries.

  • Standard libraries

    When using standard libraries, you can import them to your inline code or package and upload them to FunctionGraph.

  • Supported non-standard libraries

    FunctionGraph provides built-in third-party components listed in Table 1 and Table 2. You can import these libraries to your inline code in the same way as you import standard libraries.

    Table 1 Third-party components integrated with the Node.js runtime

    Name

    Usage

    Version

    q

    Asynchronous method encapsulation

    1.5.1

    co

    Asynchronous process control

    4.6.0

    lodash

    Common tool and method library

    4.17.10

    esdk-obs-nodejs

    OBS sdk

    2.1.5

    express

    Simplified web-based application development framework

    4.16.4

    fgs-express

    Provides a Node.js application framework for FunctionGraph and APIG to run serverless applications and REST APIs. This component provides an example of using the Express framework to build serverless web applications or services and RESTful APIs.

    1.0.1

    request

    Simplifies HTTP invocation and supports HTTPS and redirection.

    2.88.0

    Table 2 Non-standard libraries supported by the Python runtime

    Module

    Usage

    Version

    dateutil

    Date and time processing

    2.6.0

    requests

    HTTP library

    2.7.0

    httplib2

    httpclient

    0.10.3

    numpy

    Mathematical computing

    For pip 2.7, numpy==1.16.6.

    For pip 3.10, numpy==1.24.2.

    For pip 3.9, numpy==1.18.5.

    For pip 3.6, numpy==1.18.5.

    redis

    Redis client

    2.10.5

    obsclient

    OBS client

    -

    smnsdk

    SMN access

    1.0.1

  • Other third-party libraries (FunctionGraph has no built-in non-standard third-party libraries except those listed in the preceding table.)

    Package the dependency third-party libraries and upload them to an OBS bucket or on the function details page. These libraries will then be used in your function code.

Importing Dependent Libraries

The code for processing images is as follows:

# -*- coding: utf-8 -*-
from PIL import Image, ImageEnhance

from com.obs.client.obs_client import ObsClient

import sys
import os

current_file_path = os.path.dirname(os.path.realpath(__file__))
# append current path to search paths, so that we can import some third party libraries.
sys.path.append(current_file_path)
region = 'your region'
obs_server = 'obs.xxxxxxcloud.com'
def newObsClient(context):
    ak = context.getAccessKey()
    sk = context.getSecretKey()
    return ObsClient(access_key_id=ak, secret_access_key=sk, server=obs_server,
                     path_style=True, region=region, ssl_verify=False, max_retry_count=5, timeout=20)
def downloadFile(obsClient, bucket, objName, localFile):
    resp = obsClient.getObject(bucket, objName, localFile)
    if resp.status < 300:
        print 'download file', file, 'succeed'
    else:
        print('download failed, errorCode: %s, errorMessage: %s, requestId: %s' % resp.errorCode, resp.errorMessage,
              resp.requestId)
def uploadFileToObs(client, bucket, objName, file):
    resp = client.putFile(bucket, objName, file)
    if resp.status < 300:
        print 'upload file', file, 'succeed'
    else:
        print('upload failed, errorCode: %s, errorMessage: %s, requestId: %s' % resp.errorCode, resp.errorMessage,
              resp.requestId)
def getObjInfoFromObsEvent(event):
    s3 = event['Records'][0]['s3']
    eventName = event['Records'][0]['eventName']
    bucket = s3['bucket']['name']
    objName = s3['object']['key']
    print "*** obsEventName: %s, srcBucketName: %s, objName: %s", eventName, bucket, objName
    return bucket, objName
def set_opacity(im, opacity):
    """Set the transparency."""
    if im.mode != "RGBA":
        im = im.convert('RGBA')
    else:
        im = im.copy()
    alpha = im.split()[3]
    alpha = ImageEnhance.Brightness(alpha).enhance(opacity)
    im.putalpha(alpha)
    return im
def watermark(im, mark, opacity=0.6):
    """Add a watermark."""
    try:
        if opacity < 1:
            mark = set_opacity(mark, opacity)
        if im.mode != 'RGBA':
            im = im.convert('RGBA')
        if im.size[0] < mark.size[0] or im.size[1] < mark.size[1]:
            print "The mark image size is larger size than original image file."
            return False
        x = (im.size[0] - mark.size[0]) / 2
        y = (im.size[1] - mark.size[1]) / 2
        layer = Image.new('RGBA', im.size, )
        layer.paste(mark, (x, y))
        return Image.composite(layer, im, layer)
    except Exception as e:
        print ">>>>>>>>>>> WaterMark EXCEPTION:  " + str(e)
        return False
def watermark_image(localFile, fileName):
    im = Image.open(localFile)
    watermark_image_path = os.path.join(current_file_path, "watermark.png")
    mark = Image.open(watermark_image_path)
    out = watermark(im, mark)
    print "**********finish water mark"
    name = fileName.split('.')
    outFileName = name[0] + '-watermark.' + name[1]
    outFilePath = "/tmp/" + outFileName
    if out:
        out = out.convert('RGB')
        out.save(outFilePath)
    else:
        print "Sorry, Save watermarked file Failed."
    return outFileName, outFilePath
def handler(event, context):
    srcBucket, srcObjName = getObjInfoFromObsEvent(event)
    outputBucket = context.getUserData('obs_output_bucket')
    client = newObsClient(context)
    # download file uploaded by user from obs
    localFile = "/tmp/" + srcObjName
    downloadFile(client, srcBucket, srcObjName, localFile)
    outFileName, outFile = watermark_image(localFile, srcObjName)
    # Upload converted files to a new OBS bucket.
    uploadFileToObs(client, outputBucket, outFileName, outFile)
    return 'OK'

For standard libraries and supported non-standard libraries, you can directly use them in your function.

For non-standard third-party libraries that are not provided by FunctionGraph, you can use them by performing the following steps:

  1. Package the dependent libraries into a ZIP file, upload the ZIP file to an OBS bucket, and obtain the OBS link URL.
  2. Log in to the FunctionGraph console, and choose Functions > Dependencies in the navigation pane.
  3. Click Create Dependency.
  4. Set the dependency name and runtime, specify the OBS link URL, and click OK.
    Figure 1 Setting the dependency
  5. On the function details page, click the Code tab, click Add in the Dependencies area, select the dependency created in 4, and click OK.
    Figure 2 Selecting a dependency

    Each dependency package cannot contain a file with the same name as a code file. Otherwise, the two files may be incorrectly merged or overwritten. For example, if dependency package depends.zip contains a file named index.py, the handler of a function cannot be set to index.handler. Otherwise, a code file also named index.py will be generated.