简体   繁体   English

使用 Zip 的 AWS Lambda PHP 创建函数

[英]AWS Lambda PHP Create Function with Zip

I'm trying to create a PHP script that creates a function from some code that i zip up on our server.我正在尝试创建一个 PHP 脚本,该脚本从我在服务器上压缩的一些代码创建一个函数。 I uploaded the file manually to lambda, and it works fine.我手动将文件上传到 lambda,它工作正常。 But when i try to use the aws sdk to create the function, it fails with an error message.但是当我尝试使用 aws sdk 创建函数时,它失败并显示错误消息。 Anyone got any clue?有人有任何线索吗?

Code:代码:

private function createLambdaFunction() {

    $result = $this->lambdaConn->createFunction(array(
        'FunctionName' => $this->lambdaFunctionName,
        'Runtime' => $this->runtime,
        'Role' => $this->role,
        'Handler' => $this->lambdaFunctionName.".".$this->handler,
        'Description' => $this->description,
        'Timeout' => $this->timeout,
        'MemorySize' => $this->memorySize,
        'Code' => array(
            'ZipFile' => 'fileb://test.zip'
        )
    ));

Error:错误:

PHP Fatal error:  Uncaught Aws\Lambda\Exception\LambdaException: AWS 
Error Code: InvalidParameterValueException, 
Status Code: 400, AWS Request ID: asdf, AWS Error Type: user, 
AWS Error Message: Could not unzip uploaded file. Please check 
your file, then try to upload again., User-Agent: 
aws-sdk-php2/2.8.10 Guzzle/3.9.3 curl/7.35.0 PHP/5.5.9-1ubuntu4.9

I can't seem to find a good example on google, and the documentation is...less than ideal.我似乎无法在谷歌上找到一个很好的例子,而且文档......不太理想。 I created the zip file with php, so I've tried passing that var, the full path to the file, relative path to file, etc. Finally learned you have to use fileb:// preface, but that didn't end up fixing anything.我用 php 创建了 zip 文件,所以我尝试传递那个 var、文件的完整路径、文件的相对路径等。终于知道你必须使用 fileb:// 前言,但这并没有结束修复任何东西。

Okay, I'm not sure why this is the case, but you need to base64 encode your zip file like:好的,我不确定为什么会这样,但是您需要对 zip 文件进行 base64 编码,例如:

            $result = $this->lambdaConn->createFunction(array(
            'FunctionName' => $this->lambdaFunctionName,
            'Runtime' => $this->runtime,
            'Role' => $this->role,
            'Handler' => $this->lambdaFunctionName . "." . $this->handler,
            'Description' => $this->description,
            'Timeout' => $this->timeout,
            'MemorySize' => $this->memorySize,
            'Code' => array(
                'ZipFile' => 'fileb://'.base64_encode(file_get_contents('test.zip'))
            )
        ));

I'm not sure why this is required, as accourding to the doumentation and a post by an AWS employee, you dont have to have base64 encoding for create function.我不确定为什么需要这样做,因为根据 AWS 员工的说明和帖子,您不必为 create 函数使用 base64 编码。 They must have mixed up something or another.他们一定是搞混了。

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

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