简体   繁体   English

如何将本地 php function 连接到 AWS lambda?

[英]How to connect your local php function to AWS lambda?

I am very new to this.我对此很陌生。 I only know about S3 and also created my functionality.我只知道 S3 并且还创建了我的功能。 But, now I want to use Lambda in my php project.但是,现在我想在我的 php 项目中使用 Lambda。 I am using AWS PHP SDK for this.我为此使用AWS PHP SDK I created a lambda function on AWS in which I am passing file name.我在 AWS 上创建了一个 lambda function,我在其中传递了文件名。

The lambda function searches the file from S3 and gives me the data inside the file, but now what I want to do is that I pass a file name inside my php function which then passes that filename to my Lambda function and searches the file from S3 then returns the data inside the file. The lambda function searches the file from S3 and gives me the data inside the file, but now what I want to do is that I pass a file name inside my php function which then passes that filename to my Lambda function and searches the file from S3然后返回文件内的数据。

This is my lambda function on S3这是我在 S3 上的 lambda function

import boto3

bucket = 'get-file-lambda-function'
filename = 'myfilename.json'
    
def lambda_handler(event, context):
    s3 = boto3.client('s3')
    file = s3.get_object(Bucket=bucket, Key=filename)
    body = file.get()['Body'].read()
    
return body

This is my php function which gets the data from lambda function.这是我的 php function 从 lambda ZC1C425268E68395D1AB5074 获取数据。 Now, I want to pass a file name inside my php function.现在,我想在我的 php function 中传递一个文件名。 And lambda search only that file from S3. lambda 仅从 S3 搜索该文件。

public function get_file_data() 
{
    $client = LambdaClient::factory(AWS configrations);

    $filename = /*selected by me*/

    $fileData = $client->invoke([
        // Lambda function name
        'FunctionName' => 'get-file-lambda-function',
    ]);

    return $fileData;
}

i am a beginner so I do not know about this right now.我是初学者,所以我现在不知道。 Would anyone please guide me that how can I achieve this?有人可以指导我如何实现这一目标吗?

This is actually quite simple for your requirement.对于您的要求,这实际上非常简单。

In your PHP app you would make use of the Payload argument when you call invoke.在您的 PHP 应用程序中,您将在调用调用时使用Payload参数。 An example is below下面是一个例子

public function get_file_data($file_name) 
{
    $client = LambdaClient::factory(AWS configuration);

    $filename = /*selected by me*/

    $fileData = $client->invoke([
        // Lambda function name
        'FunctionName' => 'get-file-lambda-function',
        'Payload' => json_encode(['bucket' => 'get-file-lambda-function', 'key' => $file_name])
    ]);

    return $fileData;
}

Then in your Lambda function you would access this data by using the event parameter.然后在您的 Lambda function 中,您将使用事件参数访问此数据。 An example of this below下面的一个例子

import boto3

s3 = boto3.client('s3')

def lambda_handler(event, context):
    bucket = event['bucket']
    filename = event['key']
    file = s3.get_object(Bucket=bucket, Key=filename)
    body = file.get()['Body'].read()

    return body

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM