Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S3 event handler #1021

Closed
ederjbezerra opened this issue Sep 16, 2021 · 1 comment
Closed

S3 event handler #1021

ederjbezerra opened this issue Sep 16, 2021 · 1 comment
Labels

Comments

@ederjbezerra
Copy link

ederjbezerra commented Sep 16, 2021

I'm trying to handle object created event and do some jobs with uploaded files but no success since bref shows "This handler expected to be invoked with a API Gateway or ALB event. Instead, the handler was invoked with invalid event data"

See my logs:

2021-09-16T19:29:28.528-03:00 | START RequestId: e0b925a1-2a00-4e7b-b8cf-cf471c9696e5 Version: $LATEST
-- | --
  | 2021-09-16T19:29:28.528-03:00 | [16-Sep-2021 22:29:28] NOTICE: fpm is running, pid 9
  | 2021-09-16T19:29:28.528-03:00 | [16-Sep-2021 22:29:28] NOTICE: ready to handle connections
  | 2021-09-16T19:29:28.532-03:00Copiare0b925a1-2a00-4e7b-b8cf-cf471c9696e5	Invoke Error	{     "errorType": "Bref\\Event\\InvalidLambdaEvent",     "errorMessage": "This handler expected to be invoked with a API Gateway or ALB event. Instead, the handler was invoked with invalid event data: Array\n(\n    [Records] => Array\n        (\n            [0] => Array\n                (\n                    [eventVersion] => 2.1\n                    [eventSource] => aws:s3\n                    [awsRegion] => us-west-2\n                    [eventTime] => 2021-09-16T22:29:21.306Z\n                    [eventName] => ObjectCreated:Post\n                    [userIdentity] => Array\n                        (\n                            [principalId] => AWS:AIDAJYZXC2IIOR3H6KJYO\n                        )\n\n                    [requestParameters] => Array\n                        (\n                            [sourceIPAddress] => 3.231.2.104\n                        )\n\n                    [responseElements] => Array\n                        (\n                            [x-amz-request-id] => CZYF3MDH0KVFZ819\n                            [x-amz-id-2] => Bk6HORcSfBHvlyGh7i86f4j5gjN6ic2l1Cmyln8Kkk4Ylrj0GcIjCx4b5jygnvC9BJb3snXozFm/tUmw428JKyuj44fYvncW\n                        )\n\n                    [s3] => Array\n                        (\n                            [s3SchemaVersion] => 1.0\n                            [configurationId] => lambda-up-finished-dev-api-68a737133cefb25bff959852b8f04754\n                            [bucket] => Array\n                                (\n                                    [name] => my-bucket-temp\n                                    [ownerIdentity] => Array\n                                        (\n                                            [principalId] => A2TMY5M46PWGMA\n                                        )\n\n                                    [arn] => arn:aws:s3:::my-bucket-temp\n                                )\n\n                            [object] => Array\n                                (\n                                    [key] => 14_f3021f288a75342dbe3011cfce270042\n                                    [size] => 49610\n                                    [eTag] => 5c105f2d27dc83597453727c86756ceb\n                                    [sequencer] => 006143C5460B463DA3\n                                )\n\n                        )\n\n                )\n\n        )\n\n)\n",     "stack": [         "#0 /var/task/vendor/bref/bref/src/Event/Http/HttpHandler.php(23): Bref\\Event\\Http\\HttpRequestEvent->__construct()",         "#1 /var/task/vendor/bref/bref/src/Runtime/Invoker.php(29): Bref\\Event\\Http\\HttpHandler->handle()",         "#2 /var/task/vendor/bref/bref/src/Runtime/LambdaRuntime.php(102): Bref\\Runtime\\Invoker->invoke()",         "#3 /opt/bootstrap(43): Bref\\Runtime\\LambdaRuntime->processNextEvent()",         "#4 {main}"     ] }

My serverless.yml


service: lambda-up-finished

provider:
    name: aws
    region: us-west-2
    runtime: provided.al2
    memorySize: 256
    vpc:
      securityGroupIds:
        - sg-1ec1bc6b
      subnetIds:
        - subnet-f45abc7d9
        - subnet-81f25u2db

plugins:
    - ./vendor/bref/bref

functions:
    api:
        handler: index.php
        description: ''
        timeout: 28 # in seconds (API Gateway has a timeout of 29 seconds)
        layers:
            - ${bref:layer.php-74-fpm}
            - 'arn:aws:lambda:us-west-2:844410403720:layer:database:7' #my database layer that works in many other files

        events:
            - s3:
                bucket: my-bucket-temp
                event: s3:ObjectCreated:*
                existing: true

        role: arn:aws:iam::844410403720:role/aws-LambdaRole-17PSBG4C72XN9

# Exclude files from deployment
package:
    patterns:
        - '!node_modules/**'
        - '!tests/**'

My index.php

<?php
error_reporting(E_ERROR);
ini_set('display_errors',1);

require __DIR__ . '/vendor/autoload.php';

use Bref\Context\Context;
use Bref\Event\S3\S3Event;
use Bref\Event\S3\S3Handler;

require_once('libs/aws/aws.phar');
use Aws\S3\S3Client;

class Handler extends S3Handler
{
    public function handleS3(S3Event $event, Context $context): void
    {              
        $bucketName = $event->getRecords()[0]->getBucket()->getName();
        $fileName = $event->getRecords()[0]->getObject()->getKey();
        $sentData = explode('_',$fileName);
        $userid=$sentData[0];
        $newFile=explode('.',$sentData[1]);
        $tipo=$newFile[1];
        $newFile=$newFile[0];

        $s3 = S3Client::factory(array(
            'version' => 'latest',
            'region'  => 'us-west-2',
            'credentials' => array(
              'key' => 'xxxxxxxxxxxx',
              'secret'  => 'zzzzzzzzzzzzzzzzzzzzz'
            )
        ));

        require_once('config.php');

        switch($tipo){
            case 'perfil':
                db_exec($dbwrite,"INSERT INTO userImages (userid,keyname) values (?,?)",array($userid,$newFile));
                $perfilAtual = db_exec($dbread, "SELECT keyname FROM userImages WHERE userid=? AND perfil=1", array($userid));
                
                if ($perfilAtual->rowCount()>0){
                    $perfilAtual = $perfilAtual->fetch(PDO::FETCH_ASSOC);
                    if ($s3->deleteObject(['Bucket' => $bucketName, 'Key' => $perfilAtual['keyname']]))
                    db_exec($dbwrite, "DELETE FROM userImages WHERE userid=? AND perfil='1'", array($userid));
                }
                $bucketDestiny='bucket-imagens';
            break;

            case 'jpg':
            case 'jpeg':
            case 'png':
                db_exec($dbwrite,"INSERT INTO userImages (userid,keyname) values (?,?)",array($userid,$newFile,));
                $perfilAtual = db_exec($dbread, "SELECT keyname FROM userImages WHERE userid=? AND perfil=1", array($userid));
                $bucketDestiny='bucket-imagens';
            break;
            
            case 'mp4':
            case 'wav':
            case 'mp3':
                $bucketDestiny='bucket-audios';
            break;
        }

        if($s3->copyObject(['Bucket'=>$bucketDestiny,'Key'=>$newFile,'CopySource'=>'/my-bucket-temp/'.$newFile])){
            $s3->putObjectTaggingAsync(['Bucket'=>$bucketDestiny,'Key'=>$newFile,
            'Tagging' => [
                'TagSet' => [
                    [
                        'Key' => 'owner', 
                        'Value' => $userid, 
                    ]
                ]
            ]]);
        }

        //delete original uploaded file
        $s3->deleteObject(['Bucket'=>$bucketName,'Key'=>$fileName]);
    }
}
return new Handler();
@ederjbezerra
Copy link
Author

ederjbezerra commented Sep 17, 2021

Unfortunately, even choosing the right option when bref init asks what kind of application you will run(web app ou event-driven) it uses fpm, I just noticed it. Changing by php-74 make it work properly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant