Python
更新时间:2024-07-05
本示例演示用 Python SDK 创建和执行您的 CFC 函数。
安装
- 在 官方网站 下载Python SDK。
- 进入下载目录。
- 安装SDK之前,需要先执行命令
pip install pycrypto安装pycrypto依赖。 - 执行以下命令安装SDK包:
Bash
1python setup.py install
使用样例
Plain Text
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3from baidubce.services.cfc.cfc_client import CfcClient
4from baidubce.bce_client_configuration import BceClientConfiguration
5from baidubce.auth.bce_credentials import BceCredentials
6
7function_name = "testHelloWorld" #您可以自定义您的函数名
8# 创建一个cfc客户端
9#有三个endpoint可以选择:cfc.bj.baidubce.com,cfc.gz.baidubce.com,cfc.su.baidubce.com
10host = '<Your Endpoint>'
11AK = '<Your AccessKeyID>'
12SK = '<Your AccessKeySecret>'
13
14# 也可以通过环境变量的形式获取ak,sk
15# AK = os.environ["BCE_ACCESS_KEY_ID"]
16# SK = os.environ["BCE_ACCESS_KEY_SECRET"]
17
18config = BceClientConfiguration(credentials=BceCredentials(AK, SK), endpoint=host)
19cfc_client = CfcClient(config)
20
21#下面是您要发布的zip包的 base64-encoded, 注意zip包压缩时不能包含顶层文件夹目录
22base64_file = '<Your base64-encoded Code>'
23
24#创建函数
25create_response = cfc_client.create_function(function_name,
26 description="CFC SDK Demo",
27 handler="<Your index>.handler",
28 memory_size=128,
29 region='bj',
30 zip_file=base64_file,
31 publish=False,
32 run_time='python2',
33 timeout=3,
34 dry_run=False)
35
36
37# get function use brn
38brn = create_response.FunctionBrn
39
40# 执行函数
41invocations_response = cfc_client.invocations(brn)
42print invocations_response.read()
