-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Best way to pass binary content through ApiGateway unmodified #566
Comments
Reading the documentation closer it looks like I can set it with Globals:
Api:
BinaryMediaTypes:
- application~1octet-stream |
Ok, so I tried this as above, but it seems that it defaults to base64 encoding the binary, and not sure where/how I would be able to set Basically, I want to reverse-proxy a request through apigateway/lambda to another endpoint, and the binary payloads should be passed through unmodified. I assume this will require some combination of ContentHandling and/or Ref:
|
Reading a little deeper.. the base64 encoding may actually be occurring at the
The following issue (on a related library) makes me think that setting the So this makes me think that there is still something within SAM/CloudFormation that should allow us to tell it to decode the base64 and return binary again.. based on the previous docs, my guess would be setting a Looking at these OpenAPI extensions:
It sounds like adding the following to the OpenAPI body would enable this: x-amazon-apigateway-integration:
contentHandling: CONVERT_TO_BINARY Looking at the generated/processed template json (within CloudFormation): "x-amazon-apigateway-integration" : {
"uri" : {
"Fn::Sub" : "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${Proxy.Arn}/invocations"
},
"httpMethod" : "POST",
"type" : "aws_proxy"
} It looks as though we aren't setting any of those keys at the moment.. but they do match the rest of the extensions detailed above. So I guess ultimately, my question/request ends up as:
|
Decided to try implementing this myself using the more manual version of FooTestApi:
Type: AWS::Serverless::Api
Properties:
Name: FooTestApi
StageName: Prod
DefinitionBody:
swagger: '2.0'
info:
version: '1.0'
title: !Ref AWS::StackName
paths:
"/{proxy+}":
x-amazon-apigateway-any-method:
x-amazon-apigateway-integration:
type: aws_proxy
uri: !Sub arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${AgentProxy.Arn}/invocations
httpMethod: POST
contentHandling: CONVERT_TO_BINARY
responses: {} I also created this permission manually (though I expect if I set the AgentProxyCatchAllPermissionProdFooTestApi:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref AgentProxy
SourceArn: !Sub
- arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/*/*
- __Stage__: Prod
__ApiId__: !Ref FooTestApi
Principal: apigateway.amazonaws.com
Action: lambda:invokeFunction This seemed to result in the expected field being available in the produced CloudFormation json, confirming my theory: "FooTestApi" : {
"Properties" : {
"Name" : "FooTestApi",
"Parameters" : {
"endpointConfigurationTypes" : "REGIONAL"
},
"Body" : {
"info" : {
"title" : {
"Ref" : "AWS::StackName"
},
"version" : "1.0"
},
"swagger" : "2.0",
"paths" : {
"/{proxy+}" : {
"x-amazon-apigateway-any-method" : {
"x-amazon-apigateway-integration" : {
"type" : "aws_proxy",
"httpMethod" : "POST",
"uri" : {
"Fn::Sub" : "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${AgentProxy.Arn}/invocations"
},
"contentHandling" : "CONVERT_TO_BINARY"
},
"responses" : {}
}
}
}
},
...snip... Interestingly, it didn't seem to add my binary media type setting (set in the Globals), even though it showed up as a key in the produced CloudFormation json (aws/aws-sam-cli#653 ?):
Next, I tried entirely removing |
So, to resolve this properly, it seems that:
|
Sorry about this missing feature. Closing in favor of #553. |
PR for fixing BinaryMediaTypes getting lost in update #954 |
To get around needing BinaryMediaTypes:
- "*~1*" on your API definition. However, I think this will have the side effect of converting all responses to binary (unless |
Hi everybody, So does AWS Api gateway support to receive a binary payload? then in api gateway can help to convert that binary to base64 for me? Thanks very much for any tips... |
@thanhlq In lambda proxy integration data is always converted to base64 if the |
I have a lambda function that is integrated to API Gateway with a Lambda Proxy integration. Basically the lambda function is grabbing a zip file from S3 and returning it as payload by auto downloading the file when the endpoint is hit in a browser. The contentType I sent is application/octet-stream. However, when the zip file is downloaded, it turns into a cpgz. I am not sure how to resolve this issue. My API gateway was created via the console for proof of concept and not via the cfn template (though eventually we will deploy it through our cfn template) hence I dont know how to pass in the contentHandling parameter. Do you have any thoughts on how I can solve this issue? |
Hi all
I added the binary media types in my Properties:
StageName: dev
OpenApiVersion: '3.0.0'
BinaryMediaTypes:
- application~1octet-stream
- application~1zip
- application~1gzip And after deployment I do see them in the console. (I used sam My application also sets This should be happening right? What am I missing? |
I searched the existing issues for
ContentHandling
and most that contain it (as part of their example code) seem to be setting at as part of a fully custom OpenAPI/Swagger spec.I was wondering if there is a canonical way for setting this on the generated 'implicit ApiGateway', or if I should be resorting to manually creating my entire spec for this kind of behaviour?
The text was updated successfully, but these errors were encountered: