skip to Main Content

Can someone tell me how to return a stream from an AWS Api Gateway?
My API calls a lambda written in C#, which successfully returns a string, but I don’t know how to modify it to return a stream.
I need this because it will be used to download a zipped file that is 100MB.

This is a simplified version of my current lambda handler.

    public APIGatewayHttpApiV2ProxyResponse MainHandler(APIGatewayProxyRequest request, ILambdaContext context)
    {
        ...
        var response = new APIGatewayHttpApiV2ProxyResponse
        {
            StatusCode = (int)HttpStatusCode.OK,
            Body = "MainHandler was called",
            Headers = new Dictionary<string, string> { { "Content-Type", "text/plain" } }
        };

        return response;
    }

I don’t know how to modify this to return a stream, instead of text.

I have found examples of how to consume a stream using HttpClient:

c#: How to Post async request and get stream with httpclient?

but no examples of how to create one from AWS.

2

Answers


  1. You would hit the API Gateway payload limit of 10MB regardless of your return type with 100MB.

    Your best option would be to save the data you want to return to S3, generate a pre-signed URL and then return the URL via your Lambda to the client.

    Amazon Lambda functions and the API Gateway are not for returning large files.

    Login or Signup to reply.
  2. It’s important to understand that both the API gateway and lambda functions have limitations and they’re not designed to deal with long open HTTP connections. API gateway has a hard response size limit of 10MB, while the lambda function has a hard limit of 6MB(sync) and 256kb(async), please refer to the below docs for more info

    https://docs.aws.amazon.com/lambda/latest/dg/gettingstarted-limits.html
    https://docs.aws.amazon.com/apigateway/latest/developerguide/limits.html

    I quote two solutions from the following answer API Gateway stream large size content in response

    • Have your EC2 machine upload the result to S3 and have API Gateway return a pre-signed url to download the response from S3. This would stream the download, but would have to wait for the EC2 -> S3 upload to complete first.

    • Use Elastic Beanstalk, that way you would be in control of the server and able to keep your connections open for as long as you wanted, and send as much data as you want.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search