【问题标题】:AWS Lambda PHP Create Function with ZipAWS Lambda PHP 使用 Zip 创建函数
【发布时间】:2015-09-08 12:31:13
【问题描述】:

我正在尝试创建一个 PHP 脚本,该脚本从我在我们的服务器上压缩的一些代码创建一个函数。我手动将文件上传到 lambda,它工作正常。但是当我尝试使用 aws sdk 创建函数时,它会失败并显示错误消息。有人知道吗?

代码:

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'
        )
    ));

错误:

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

我似乎无法在 google 上找到一个很好的例子,而且文档......不太理想。我用 php 创建了 zip 文件,所以我尝试传递该 var、文件的完整路径、文件的相对路径等。最后知道你必须使用 fileb:// 前言,但这并没有结束修理任何东西。

【问题讨论】:

    标签: php amazon-web-services aws-lambda


    【解决方案1】:

    好的,我不知道为什么会这样,但是您需要对您的 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'))
                )
            ));
    

    我不确定为什么需要这样做,因为根据 AWS 员工的文档和帖子,您不必为创建功能使用 base64 编码。他们一定是搞混了。

    【讨论】:

    • 现在它可以工作了,你不需要再使用 base64_encode 了,因为新的 SDK 会为你进行转换,只需使用 file_get_contents('test.zip') 就可以了:github.com/aws/aws-sdk-php/issues/673#event-347510707跨度>
    • 您使用的是哪个版本?我的绝对需要它。
    猜你喜欢
    • 1970-01-01
    • 2019-04-25
    • 1970-01-01
    • 2016-03-25
    • 2018-01-13
    • 2021-03-20
    • 2020-11-12
    • 2020-01-18
    • 2019-04-16
    相关资源
    最近更新 更多