Skip to main content

Aliyun FunctionCompute SDK2

Project description

https://badge.fury.io/py/aliyun-fc.svg https://travis-ci.org/aliyun/fc-python-sdk.svg?branch=master https://coveralls.io/repos/github/aliyun/fc-python-sdk/badge.svg?branch=master

Overview

The SDK of this version is dependent on the third-party HTTP library requests.

Running environment

Python 2.7, Python 3.6

Installation

Install the official release version through PIP (taking Linux as an example):

$ pip install aliyun-fc

You can also install the unzipped installer package directly:

$ sudo python setup.py install

Notice

We suggest using fc2, which corresponds to the fc2-master branch,The main difference between fc and fc2 is:

1, The invoke fuction user can add custom headers

def invoke_function(self, serviceName, functionName, payload=None,
        customHeaders = {'x-fc-invocation-type': 'Sync', 'x-fc-log-type' : 'None'}):
    """
    Invoke the function synchronously.
    :param serviceName: (required, string) the name of the service.
    :param functionName: (required, string) the name of the function.
    :param payload: (optional, bytes or seekable file-like object): the input of the function.
    Invoke the function synchronously or asynchronously.
    :param logType: (optional, string) 'None' or 'Tail'. When invoke a function synchronously,
    you can set the log type to 'Tail' to get the last 4KB base64-encoded function log.
    :param traceId: (optional, string) a uuid to do the request tracing.
    :param customHeaders: (required, dict) user-defined request header.
                        'x-fc-invocation-type' : require, 'Sync'/'Async' ,only two choice
                        'x-fc-trace-id' : require, default is 'None'
                        'x-fc-trace-id' : option
                        # other can add user define header
    :return: function output InvokeFunctionResponse object.

    """
    return self._invoke_function(
        serviceName, functionName, payload=payload, customHeaders=customHeaders)

2, The reponse returned by the user is the following object, where data represents the “reponse” corresponding to fc.

class InvokeFunctionResponse(object):
    def __init__(self, headers, data):
        self._headers = headers
        self._data = data

    @property
    def headers(self):
        return self._headers

    # user function return value
    @property
    def data(self):
        return self._data

Install the official fc2 release version through PIP (taking Linux as an example):

$ pip install aliyun-fc2

Getting started

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

import fc

# To know the endpoint and access key id/secret info, please refer to:
# https://help.aliyun.com/document_detail/52984.html
client = fc.Client(
    endpoint='<Your Endpoint>',
    accessKeyID='<Your AccessKeyID>',
    accessKeySecret='<Your AccessKeySecret>')

# Create service.
client.create_service('service_name')

# Create function.
# the current directory has a main.zip file (main.py which has a function of myhandler)
client.create_function('service_name', 'function_name', 'main.my_handler', codeZipFile = 'main.zip')

# Invoke function synchronously.
client.invoke_function('service_name', 'function_name')

# Invoke a function with a input parameter.
client.invoke_function('service_name', 'function_name', payload=bytes('hello_world'))

# Read a image and invoke a function with the file data as input parameter.
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 the output image.
dst = open('dst_image_file_path', 'wb')
dst.write(r) # if fc2, dst.write(r.data)
src.close()
dst.close()

# Invoke function asynchronously.
client.async_invoke_function('service_name', 'function_name')

# List services.
client.list_services()

# List functions with prefix and limit.
client.list_functions('service_name', prefix='the_prefix', limit=10)

# Delete service.
client.delete_service('service_name')

# Delete function.
client.delete_function('service_name', 'function_name')

Testing

To run the tests, please set the access key id/secret, endpoint as environment variables. Take the Linux system for example:

$ export ENDPOINT=<endpoint>
$ export ACCESS_KEY_ID=<AccessKeyId>
$ export ACCESS_KEY_SECRET=<AccessKeySecret>
$ export STS_TOKEN=<roleARN>

Run the test in the following method:

$ nosetests                          # First install nose

More resources

Contacting us

License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

aliyun-fc2-2.0.1.tar.gz (10.5 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page