全部产品
云市场
云游戏

使用 SDK 执行 HTTP 触发器函数

更新时间:2020-01-09 18:21:58

对于每一个请求,函数计算服务会根据请求头部的 Authorization 字段来校验是否合法(设置了 HTTP 触发器的允许匿名访问的函数除外)。客户端须使用与函数计算服务端一致的签名算法才能通过验证,对于未包含签名字段或者签名错误的请求,函数计算服务将会返回 HTTP 403 错误。详细签名规则可以参考 签名认证。函数计算提供的 SDK 中有签名方法,可以使用 SDK 提供的接口执行 HTTP 触发器(需要身份认证)函数,下面进行详细介绍具体调用。

Java

1. 安装依赖

将 Maven 依赖项添加到 pom.xml 中

  1. ```
  2. <dependency>
  3. <groupId>com.aliyun</groupId>
  4. <artifactId>aliyun-java-sdk-fc</artifactId>
  5. <version>1.7.1</version>
  6. </dependency>
  7. ```

2. 代码示例

  1. import com.alibaba.fastjson.JSONObject;
  2. import com.aliyuncs.fc.client.FunctionComputeClient;
  3. import com.aliyuncs.fc.model.HttpAuthType;
  4. import com.aliyuncs.fc.model.HttpMethod;
  5. import com.aliyuncs.fc.request.*;
  6. import com.aliyuncs.fc.response.*;
  7. import java.io.IOException;
  8. import java.security.InvalidKeyException;
  9. public class testJavaSDK {
  10. private static final String REGION = "cn-hangzhou";
  11. private static final String SERVICE_NAME = "XXX";
  12. private static final String FUNCTION_NAME = "XXX";
  13. public static void main(final String[] args) throws IOException, InvalidKeyException, IllegalStateException {
  14. String accountId = "XXX";
  15. String accessKey = "XXX";
  16. String accessSecretKey = "XXX";
  17. FunctionComputeClient fcClient = new FunctionComputeClient(REGION, accountId, accessKey, accessSecretKey);
  18. HttpInvokeFunctionRequest request = new HttpInvokeFunctionRequest(SERVICE_NAME,FUNCTION_NAME, HttpAuthType.FUNCTION, HttpMethod.POST, null);
  19. JSONObject object = new JSONObject();
  20. object.put("string","string");
  21. object.put("int","123");
  22. String payload = object.toJSONString();
  23. request.setPayload(payload.getBytes());
  24. request.setHeader("Content-Type", "application/json");
  25. InvokeFunctionResponse invkResp = fcClient.invokeFunction(request);
  26. System.out.println(new String(invkResp.getContent()));
  27. }
  28. }

接口详细入参可以查看 SDK 源码

Python

1. 安装依赖

  1. pip install aliyun-fc2

2. 代码示例

  1. # -*- coding: utf-8 -*-
  2. import fc2
  3. client = fc2.Client(
  4. endpoint='<Your Endpoint>',
  5. accessKeyID='<Your AccessKeyID>',
  6. accessKeySecret='<Your AccessKeySecret>')
  7. req = client.do_http_request( "method", "serviceName", "functionName", "path", headers={}, params=None, body=bytes('hello_world'.encode('utf-8')))
  8. print (req.status_code)

接口详细入参可以查看 SDK 源码

PHP

1. 安装依赖

  • 可以通过 Composer 命令安装依赖:
    1. $ composer require aliyunfc/fc-php-sdk
  • 您还可以在 composer.json 文件中声明对 Alibaba Cloud FC SDK for PHP 的依赖关系。
    1. "require": {
    2. "aliyunfc/fc-php-sdk": "~1.2"
    3. }
  • 然后运行 composer install——no dev 来安装依赖项。安装 Composer 依赖关系管理器后,在 PHP 代码中导入依赖关系:

    1. require_once __DIR__ . '/vendor/autoload.php';

    2. 代码示例

    1. <?php
    2. require_once __DIR__ . '/vendor/autoload.php';
    3. use AliyunFC\Client;
    4. $fcClient = new Client([
    5. "endpoint" => '<Your Endpoint>',
    6. "accessKeyID" =>'<Your AccessKeyID>',
    7. "accessKeySecret" =>'<Your AccessKeySecret>'
    8. ]);
    9. $res = $fcClient->doHttpRequest("method", "serviceName", "functionName", "path", $headers = [], $unescapedQueries = [], $data = null);
    10. $s = $res->getStatusCode();
    11. $data = $res->getBody()->getContents();
    12. var_dump($s);
    13. var_dump($data);

    接口详细入参可以查看 SDK 源码

    Node.js

    1. 安装依赖

    1. npm install @alicloud/fc2 --save

    2. 代码示例

    1. 'use strict';
    2. var FCClient = require('@alicloud/fc2');
    3. var client = new FCClient('<account id>', {
    4. accessKeyID: '<access key id>',
    5. accessKeySecret: '<access key secret>',
    6. region: 'cn-shanghai',
    7. });
    8. async function test () {
    9. try {
    10. var resp = await client.get('/proxy/${serviceName}/${functionName}/${path}',null,headers )
    11. console.log('invoke function: %j', resp);
    12. } catch (err) {
    13. console.error(err);
    14. }
    15. }
    16. test().then();

    上述是 GET 请求调用示例,其他请求方式接口详情可以查看 SDK 源码

.Net Core

1. 安装依赖

在 .csproj 文件中添加如下 package:

  1. <ItemGroup>
  2. <PackageReference Include="Aliyun.FC.SDK.NetCore" Version="1.0.0" />
  3. </ItemGroup>

2. 代码示例

  1. using System;
  2. using System.Collections.Generic;
  3. using Aliyun.FunctionCompute.SDK.Client;
  4. using Aliyun.FunctionCompute.SDK.Request;
  5. namespace mynetcore
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. var fcClient = new FCClient("region", "<account id>", "<access key id>", "<access key secret>");
  12. var customHeaders = new Dictionary<string, string> {
  13. };
  14. Dictionary<string, string[]> unescapedQueries = new Dictionary<string, string[]>
  15. {
  16. };
  17. var resposnse = fcClient.InvokeHttpFunction(new HttpInvokeFunctionRequest(string serviceName, string functionName, string method, string path = null, string qualifier = null, byte[] payload = null, unescapedQueries , Dictionary<string, string> customHeaders = null));
  18. Console.WriteLine(resposnse.StatusCode);
  19. }
  20. }
  21. }

接口详细入参可以查看 SDK 源码