接口描述
功能描述
GetFunction 获取某个函数的详细信息,包括名称、代码、处理方法、关联触发器和超时时间等字段。
接口域名
接口请求域名为: scf.api.qcloud.com
请求
语法示例:
GET https://scf.api.qcloud.com/v2/index.php?Action=GetFunction
&<公共请求参数>
&functionName=<FunctionName>
请求参数
以下请求参数为本接口的接口请求参数,正式调用时需要加上公共请求参数,详见 公共请求参数 页面。其中,此接口的 Action 字段为 GetFunction。
参数名称 | 描述 | 类型 | 必选 |
---|---|---|---|
functionName | 需要获取详情的函数名称 | String | 是 |
code | 0 表示获取函数详情时不返回代码字段,1 表示返回代码字段,默认值为 0 | Int | 否 |
响应
响应示例:
{
"code": 0,
"message": "",
"codeDesc": "Success",
"data": {
"modTime": "2017-06-08 17:36:38",
"functionName": "ziptest",
"triggers": [
{
"modTime": "2017-09-18 16:52:33",
"type": "cos",
"triggerDesc": "{\"event\":\"cos:ObjectCreated:*\"}",
"triggerName": "testscfcode-1251762227.cn-south.myqcloud.com",
"addTime": "2017-09-18 16:52:33"
}
],
"handler": "index.main_handler",
"codeSize": 2600020,
"memorySize": 128,
"version": "LATEST",
"timeout": 3,
"runtime": "Python2.7",
"description": ""
}
}
响应参数
参数名称 | 描述 | 类型 |
---|---|---|
code | 公共错误码,0 表示成功,其他值表示失败 | Int |
message | 模块错误信息描述,与接口相关 | String |
codeDesc | 英文错误码,成功返回 Success,失败有相应的英文说明 | String |
data | json 数据,包含该函数的触发器名称、运行时 memorySize、入口函数、函数描述、版本号、代码大小和代码等信息 | Object |
其中 data 字段除了包含函数创建时的描述、处理程序名称、运行时内存大小等字段外,还包括关联该函数的触发器列表信息。
函数的数据结构如下:
参数名称 | 描述 | 类型 |
---|---|---|
modtime | 函数的最后一次修改时间 | String |
addtime | 函数的创建时间 | String |
functionName | 函数名称 | String |
handler | 函数入口 | String |
codeSize | 函数代码大小 | Long |
memorySize | 配置的内存使用大小,以MB为单位 | Int |
version | 函数版本,当前均为 "LATEST" | String |
timeout | 函数运行超时时间,单位为秒(s) | String |
runtime | 函数运行环境 | String |
description | 函数描述字段 | String |
triggers | 函数触发器列表 | Object |
每个触发器包含的数据结构如下:
参数名称 | 描述 | 类型 |
---|---|---|
modtime | 触发器最后一次修改时间 | String |
addtime | 触发器和函数的绑定时间 | String |
type | 触发器类型 | String |
triggerDesc | 触发器的相关参数 | String |
triggerName | 触发器名称 | String |
实际案例
不返回代码示例
请求
GET https://scf.api.qcloud.com/v2/index.php?
Action=GetFunction
&SecretId=AKIDutrojKl3CKQZNAr763UXks05898Lmciu
&Nonce=32501
&Timestamp=1505724757
&Region=gz
&Signature=eY%2Fmn5Cw%2Fjxnhb%2FQCneeQBJDXkI%3D
&functionName=ziptest
响应
{
"code": 0,
"message": "",
"codeDesc": "Success",
"data": {
"modTime": "2017-06-08 17:36:38",
"functionName": "ziptest",
"triggers": [
{
"modTime": "2017-09-18 16:52:33",
"type": "cos",
"triggerDesc": "{\"event\":\"cos:ObjectCreated:*\"}",
"triggerName": "testscfcode-1251762227.cn-south.myqcloud.com",
"addTime": "2017-09-18 16:52:33"
}
],
"handler": "index.main_handler",
"codeSize": 2600020,
"memorySize": 128,
"version": "LATEST",
"timeout": 3,
"runtime": "Python2.7",
"description": ""
}
}
返回代码示例
请求
GET https://scf.api.qcloud.com/v2/index.php?
Action=GetFunction
&SecretId=AKIDutrojKl3CKQZNAr763UXks05898Lmciu
&Nonce=52947
&Timestamp=1505725241
&Region=gz
&Signature=Mp7r7xiWyyFNyrK5UMrevN9aKC0%3D
&functionName=image
&code=1
响应
{
"code": 0,
"message": "",
"codeDesc": "Success",
"data": {
"modTime": "2017-09-18 17:00:31",
"codeError": "",
"code": "# coding=utf-8\nimport uuid\nimport json\nimport os\nimport logging\nfrom PIL import Image\nimport PIL.Image\nimport commands\nimport datetime\nfrom qcloud_cos import CosClient\nfrom qcloud_cos import DownloadFileRequest\nfrom qcloud_cos import UploadFileRequest\n\nprint('Loading function')\nappid = 1251762227 #使用帐号appid\nsecret_id = u'AKIDutrojKl3CKQZNAr763UXks05898Lmciu' #使用云api secret id\nsecret_key = u'96VJxxxxxxxxxxxxxxxxxxx2' #使用云api secret key\nregion = u'gz' \n\ncos_client = CosClient(appid, secret_id, secret_key, region)\nlogger = logging.getLogger()\n\nupload_bucket=u\"imagedst\" \n\n\ndef resize_image(image_path, resized_path):\n with Image.open(image_path) as image:\n image.thumbnail(tuple(x / 2 for x in image.size))\n image.save(resized_path)\n\ndef delete_local_file(src):\n logger.info(\"delete files and folders\")\n if os.path.isfile(src):\n try: \n os.remove(src) \n except: \n pass \n elif os.path.isdir(src): \n for item in os.listdir(src): \n itemsrc=os.path.join(src,item) \n delete_file_folder(itemsrc) \n try: \n os.rmdir(src) \n except: \n pass \n\ndef main_handler(event, context):\n logger.info(\"start main handler\")\n for record in event['Records']:\n try:\n bucket = record['cos']['cosBucket']['name']\n key = record['cos']['cosObject']['key'] \n download_path = '/tmp/{}{}'.format(uuid.uuid4(), key.split('/')[-1])\n upload_path = '/tmp/resized-{}'.format(key.split('/')[-1])\n print(\"Get from [%s] to download file [%s]\" %(bucket,key))\n\n # download image from cos\n request = DownloadFileRequest(bucket, \"/\"+key.split('/')[-1], download_path)\n download_file_ret = cos_client.download_file(request)\n if download_file_ret['code'] == 0:\n logger.info(\"Download file [%s] Success\" % key)\n logger.info(\"Image compress function start\")\n starttime = datetime.datetime.now()\n\n #compress image here\n resize_image(download_path, upload_path)\n endtime = datetime.datetime.now()\n logger.info(\"compress image take \" + str((endtime-starttime).microseconds/1000) + \"ms\")\n\n #upload the compressed image to resized bucket\n request = UploadFileRequest(upload_bucket, u'/'+key.split('/')[-1].decode('utf-8'), upload_path.decode('utf-8'))\n upload_file_ret = cos_client.upload_file(request)\n logger.info(\"upload image, return message: \" + str(upload_file_ret))\n\n #delete local file\n delete_local_file(str(download_path))\n delete_local_file(str(upload_path))\n else:\n logger.error(\"Download file [%s] Failed, err: %s\" % (key, download_file_ret['message']))\n return -1\n except Exception as e:\n print(e)\n print('Error getting object {} from bucket {}. Make sure the object exists and your bucket is in the same region as this function.'.format(key, bucket))\n raise e",
"description": "",
"triggers": [
{
"modTime": "2017-09-08 17:42:55",
"type": "cos",
"triggerDesc": "{\"event\":\"cos:ObjectCreated:*\"}",
"triggerName": "imagesrc-1251762227.cn-south.myqcloud.com",
"addTime": "2017-09-08 17:42:55"
}
],
"handler": "imageresize.main_handler",
"errNo": 0,
"codeSize": 605400,
"timeout": 3,
"version": "LATEST",
"codeResult": "success",
"memorySize": 128,
"runtime": "Python2.7",
"functionName": "image"
}
}