Bref: Document how to enable `multipart/form-data` for file uploads

Created on 22 May 2019  路  17Comments  路  Source: brefphp/bref

Have set a simple project with file upload to s3 from my handler index.php
Issue is that the uploaded files get corrupted and not able to open.

documentation

Most helpful comment

I will put the link to docs here to save some time: https://bref.sh/docs/runtimes/http.html#binary-requests-and-responses

All 17 comments

Hi, we are going to need more information than that to be able to help. Just 2 weeks ago I wrote a file upload example with Symfony and Slim and it worked fine.

template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: ''

Resources:
    MyFunction:
        Type: AWS::Serverless::Function
        Properties:
            FunctionName: 'testing-image'
            Description: ''
            CodeUri: .
            Handler: index.php
            Timeout: 30 # in seconds (API Gateway has a timeout of 30 seconds)
            MemorySize: 1024 # The memory size is related to the pricing and CPU power
            Runtime: provided
            Layers:
                - 'arn:aws:lambda:eu-central-1:209497400698:layer:php-72-fpm:6'
            Events:
                # The function will match all HTTP URLs
                HttpRoot:
                    Type: Api
                    Properties:
                        Path: /
                        Method: ANY
                HttpSubPaths:
                    Type: Api
                    Properties:
                        Path: /{proxy+}
                        Method: ANY

Outputs:
    DemoHttpApi:
        Description: 'URL of our function in the *Prod* environment'
        Value: !Sub 'https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/'

index.php

$newFile = time().$_FILES['myFile']['name'];
 $params = array('Bucket' => 'my-bucket', 'Key'=>'testimage/'.$newFile, 'SourceFile'=>$_FILES['myFile']['tmp_name'] );
    $result = $s3->putObject($params);

html which post the file to deployed api
form action="https://xxxxxxxxx-api.eu-central-1.amazonaws.com/Stage" method="POST" enctype="multipart/form-data"

input type="file" name="myFile"

input type="submit" name="submit" value="Submit"

That's it simple straight test. I post as file and put this to s3 through lambda. 147.1KB and s3 get file of 279.1KB

Need your guidance to resolve please.

Please help in on this issue.

@ashish-dirkmedia-de please respect other's time, this has only been 2 hours.

Can you tell us which version of Bref you are using?

updated to the latest one 0.3.9 same issue was facing in 0.3.3 aswell
replicating should be very easy. Please do have a look. Thanks

Thank you I can reproduce as well (the uploaded file has a larger size than the original file).

Reproduced as well with older layers and previous Bref versions, does not seem related to the hollodotme/fastcgi migration.

Mmh I wonder if this isn't related to #271 (API Gateway configuration). Here is an example: https://github.com/awslabs/aws-sam-cli/issues/312#issuecomment-368475646

Yes it works. thanks.

Good to know, thank you @ashish-dirkmedia-de for the update.

Could you share specifically what you did to make it work? That way I can add this to the documentation. Thanks!

  1. Your Api Gateway => Settings => Binary Media Types => add "multipart/form-data" => Save Changes
  2. Your Api Gateway => Actions => Deploy API
    Both the steps are important

OK you did it manually I see. I think we should try to document a solution with template.yaml so that it can be automated.

I faced the same issue: my upload were bigger/corrupted.

I've added to my template:

Globals:
    Api:
        BinaryMediaTypes:
            - "*~1*"

And now it works.
But I had to destroy/recreate the stack, not sure why.

I am having a similar issue. I am getting 415 code when sending request from POSTMAN.
Controller:
@POST @Path("/testPost") @Produces(MediaType.APPLICATION_JSON) @Consumes(MediaType.MULTIPART_FORM_DATA) public Response postTest(@FormDataParam("metadata") String metadata){ return Response.status(200).entity("post").build(); }
template:
Globals: Api: EndpointConfiguration: REGIONAL BinaryMediaTypes: # These are equivalent to image/gif and image/png when deployed - "*~1*"
Output
2019-08-02 10:54:33 127.0.0.1 - - [02/Aug/2019 10:54:33] "POST /assets/testPost HTTP/1.1" 415 -
In postman, I have a POST req with no headers and form-data called metadata with 123456 as value

Hi Guys,
Could you please post an example about this using AWS Sam, that would be helpful? Thanks in advance :)

Closing since file uploads are supported and documented now.

@bibinphilip this is out of topic for this issue.

I will put the link to docs here to save some time: https://bref.sh/docs/runtimes/http.html#binary-requests-and-responses

Was this page helpful?
0 / 5 - 0 ratings

Related issues

anandvns picture anandvns  路  8Comments

eddiejan picture eddiejan  路  7Comments

kwn picture kwn  路  8Comments

mnapoli picture mnapoli  路  4Comments

mnapoli picture mnapoli  路  6Comments