1

I'm trying to upload a file to GCS using cloud function. Api gateway is used to invoke it (POST).

The file size may vary but they are less than 32MB.

When I try to upload a file with size 10MB or above (less than 32MB), it is giving an error as 413 Request Entity Too Large: The data value transmitted exceeds the capacity limit.

Based on this article, max HTTP Request size is 32MB for Gen2 functions. But it still fails to read the uploaded file.

Below is the sample code,

import functions_framework

@functions_framework.http
def main(request):
    """HTTP Cloud Function.
    Args:
    request (flask.Request): The request object.
    <https://flask.palletsprojects.com/en/1.1.x/api/#incoming-request-data>
    Returns:
    The response text, or any set of values that can be turned into a
    Response object using `make_response`
    <https://flask.palletsprojects.com/en/1.1.x/api/#flask.make_response>.
    """

    try:
        request_json = request.get_json(silent=True)
        request_args = request.args
        
        print(request.files.get('file').content_type)
        print(request.headers['Fullfilepath'])

        if request_json and 'name' in request_json:
            name = request_json['name']
        elif request_args and 'name' in request_args:
            name = request_args['name']
        else:
            name = 'World'
        return 'Hello {}!'.format(name)
    except Exception as e:
        print(e)
        return "Something went wrong", 500


if __name__ == "__main__":
    main()

Note: If a file size is below 10MB, this works

Could someone explain why its not working with Gen2 cloud function?

EDIT: I use postman make api call. Below is the cURL snippet from postman for the request,

curl --location --request POST 'https://<gen2-function-url>' \
--header 'Fullfilepath: myFile.xlsx' \
--form 'file=@"/C:/Users/jithin/Downloads/myFile.xlsx"'

CURL OUTPUT (less than 10MB - working):

*   Trying 216.249.35.23:443...
* TCP_NODELAY set
* Connected to <gen2-fn> 
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: /etc/ssl/certs
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use h2
* Server certificate:
*  subject: CN=*.a.run.app
*  start date: Sep 26 08:18:32 2022 GMT
*  expire date: Dec 19 08:18:31 2022 GMT
*  subjectAltName: host "<gen2-fn>" matched cert's "*.a.run.app"
*  issuer: C=US; O=Google Trust Services LLC; CN=GTS CA 1C3
*  SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x6587b7c0)
> POST / HTTP/2
> Host: <gen2-fn>
> user-agent: curl/7.68.0
> accept: */*
> fullfilepath:myFile.xlsx
> content-length: 133134
> content-type: multipart/form-data; boundary=------------------------4aa0493a972a819f
>
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* old SSL session ID is stale, removing
* Connection state changed (MAX_CONCURRENT_STREAMS == 100)!
* We are completely uploaded and fine
< HTTP/2 200
< content-type: text/html; charset=utf-8
< x-cloud-trace-context: ac66dub6c7d036958f95o05;o=1
< date: Wed, 02 Nov 2022 08:48:59 GMT
< server: Google Frontend
< content-length: 12
< alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
<
* Connection #0 to host <gen2-fn> left intact
Hello World!% 

CURL OUTPUT (more than 10MB - NOT working):

*   Trying 216.249.35.23:443...
* TCP_NODELAY set
* Connected to <gen2-fn>
* ALPN, offering h2
* ALPN, offering http/1.1
* successfully set certificate verify locations:
*   CAfile: /etc/ssl/certs/ca-certificates.crt
  CApath: /etc/ssl/certs
* TLSv1.3 (OUT), TLS handshake, Client hello (1):
* TLSv1.3 (IN), TLS handshake, Server hello (2):
* TLSv1.3 (IN), TLS handshake, Encrypted Extensions (8):
* TLSv1.3 (IN), TLS handshake, Certificate (11):
* TLSv1.3 (IN), TLS handshake, CERT verify (15):
* TLSv1.3 (IN), TLS handshake, Finished (20):
* TLSv1.3 (OUT), TLS change cipher, Change cipher spec (1):
* TLSv1.3 (OUT), TLS handshake, Finished (20):
* SSL connection using TLSv1.3 / TLS_AES_256_GCM_SHA384
* ALPN, server accepted to use h2
* Server certificate:
*  subject: CN=*.a.run.app
*  start date: Oct 17 08:15:34 2022 GMT
*  expire date: Jan  9 08:15:33 2023 GMT
*  subjectAltName: host "<gen2-fn>" matched cert's "*.a.run.app"
*  issuer: C=US; O=Google Trust Services LLC; CN=GTS CA 1C3
*  SSL certificate verify ok.
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Using Stream ID: 1 (easy handle 0x5432697c0)
> POST / HTTP/2
> Host: <gen2-fn>
> user-agent: curl/7.68.0
> accept: */*
> fullfilepath:myFile1.xlsx
> content-length: 16492439
> content-type: multipart/form-data; boundary=------------------------d75d043c4ffd6fce
>
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* TLSv1.3 (IN), TLS handshake, Newsession Ticket (4):
* old SSL session ID is stale, removing
* Connection state changed (MAX_CONCURRENT_STREAMS == 100)!
* We are completely uploaded and fine
< HTTP/2 500
< content-type: text/html; charset=utf-8
< x-cloud-trace-context: 464039806234sdf503247ec892803a20;o=1
< date: Wed, 02 Nov 2022 08:48:21 GMT
< server: Google Frontend
< content-length: 20
< alt-svc: h3=":443"; ma=2592000,h3-29=":443"; ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43"
<
* Connection #0 to host <gen2-fn> left intact
Something went wrong% 
Jithin Joseph
  • 143
  • 3
  • 12
  • The code in your question does not generate the error. The error happens before your code is executed. The Google Frontend (GFE) enforces request body limits. Edit your question and show the client code that is generating the request. – John Hanley Nov 02 '22 at 06:38
  • @JohnHanley Updated the question, I use postman to call the api and passes an excel file in form-data – Jithin Joseph Nov 02 '22 at 06:49
  • 1
    1) Can you inspect the HTTP request body to see if the data is being transformed/encoded? The curl `-v` will display details. 2) Try using curl's `--data-binary` variant as that does not modify the request body. – John Hanley Nov 02 '22 at 07:05
  • Sure ill try that. But files less than 10MB is working tho. – Jithin Joseph Nov 02 '22 at 07:29
  • Your post states **413 Request Entity Too Large**. However, the actual error is created by your code at line `return "Something went wrong", 500`. Your code has a runtime exception. That is what you need to debug. – John Hanley Nov 02 '22 at 09:11
  • @JohnHanley It worked after I uploaded file as binary. Still confused why it didn't worked when form-data is used. When form-data is used for files less than 10MB they are working. Any idea why I cant use form data? **413 Request Entity Too Large** this is the exception which I'm logging(actual error). – Jithin Joseph Nov 03 '22 at 05:25

0 Answers0