It is possible to do this using serveless by using serverless-apigw-binary and adding these to serverless.yaml:
plugins:
-serverless - apigw - binary
custom:
apigwBinary:
types: #list of mime - types -
'multipart/form-data' -
'application/pdf'
But since lambda expects the payload in application/json format from the API gateway, the binary data cannot be passed directly. Therefore the settings for ContentHandling should be set to “CONVERT_TO_TEXT”. In the yaml file this translates into:
contentHandling: CONVERT_TO_TEXT
Serverless.yaml
plugins:
-serverless - apigw - binary
custom:
apigwBinary:
types: #list of mime - types -
'multipart/form-data' -
'application/pdf'
function:
pdf:
handler: handler.pdf
events:
-http:
path: /pdf
method: post
integration: lambda
request:
contentHandling: CONVERT_TO_TEXT
passThrough: WHEN_NO_TEMPLATES
template:
application / pdf: "{'body': $input.json('$')}"
multipart / form - data: "{'body': $input.json('$')}"
response:
contentHandling: CONVERT_TO_BINARY
headers:
Content - Type: "'aplication/json'"
I have been struggling with this for a while anycodings_aws-lambda now. I need to create a resource in API anycodings_aws-lambda gateway linking to a lambda function that anycodings_aws-lambda takes a pdf file as input sent as a anycodings_aws-lambda multipart/form-data POST request. To make it anycodings_aws-lambda simple, I am just returning the file for anycodings_aws-lambda now.,Get Feature Importances using SKLearn RFECV with Multi-Output Regression Chain, Possible?,Add a button beside each of every row of the table using python tkinter,How to add a margin to an inner Angular Component using the CSS of the component that includes it
curl
curl - vvv - X POST - H "Content-Type: multipart/form-data" - F "content=@file.pdf"
https: //...MYAPIHERE.../pdf
Servelerlss.yaml
function:
pdf:
handler: handler.pdf
events:
-http:
path: /pdf
method: post
integration: lambda
request:
template:
application / json: "$input.json('$')"
response:
headers:
Content - Type: "'aplication/json'"
handler.py
def pdf(event, context): pdf = event.get('content') out = { 'statusCode': 200, 'isBase64Encoded': False, 'headers': { "content-type": "application/json" }, 'body': json.dumps({ 'input': pdf, 'inputType': 'url', # 'tags': list(tags.keys()), 'error': None }) } return (out)
It is possible to do this using anycodings_aws-api-gateway serveless by using anycodings_aws-api-gateway serverless-apigw-binary and adding these anycodings_aws-api-gateway to serverless.yaml:
plugins:
-serverless - apigw - binary
custom:
apigwBinary:
types: #list of mime - types -
'multipart/form-data' -
'application/pdf'
But since lambda expects the payload in anycodings_aws-api-gateway application/json format from the API anycodings_aws-api-gateway gateway, the binary data cannot be anycodings_aws-api-gateway passed directly. Therefore the settings anycodings_aws-api-gateway for ContentHandling should be set to anycodings_aws-api-gateway “CONVERT_TO_TEXTâ€Â. anycodings_aws-api-gateway In the yaml file this translates into:
contentHandling: CONVERT_TO_TEXT
Serverless.yaml
plugins:
-serverless - apigw - binary
custom:
apigwBinary:
types: #list of mime - types -
'multipart/form-data' -
'application/pdf'
function:
pdf:
handler: handler.pdf
events:
-http:
path: /pdf
method: post
integration: lambda
request:
contentHandling: CONVERT_TO_TEXT
passThrough: WHEN_NO_TEMPLATES
template:
application / pdf: "{'body': $input.json('$')}"
multipart / form - data: "{'body': $input.json('$')}"
response:
contentHandling: CONVERT_TO_BINARY
headers:
Content - Type: "'aplication/json'"
In this tutorial, you use Amazon API Gateway to create a REST API and a resource (DynamoDBManager). You define one method (POST) on the resource, and create a Lambda function (LambdaFunctionOverHttps) that backs the POST method. That way, when you call the API through an HTTPS endpoint, API Gateway invokes the Lambda function.,In this section, you create an API Gateway REST API (DynamoDBOperations) with one resource (DynamoDBManager) and one method (POST). You associate the POST method with your Lambda function. Then, you test the setup.,In the following steps, you create a resource named DynamoDBManager in your REST API.,The POST method that you define on the DynamoDBManager resource supports the following Amazon DynamoDB operations:
To complete the following steps, you need a command line terminal or shell to run commands. Commands and the expected output are listed in separate blocks:
aws--version
You should see the following output:
aws - cli / 2.0 .57 Python / 3.7 .4 Darwin / 19.6 .0 exe / x86_64
Choose the JSON tab, and then paste the following custom policy into the JSON editor.
{
"Version": "2012-10-17",
"Statement": [{
"Sid": "Stmt1428341300017",
"Action": [
"dynamodb:DeleteItem",
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:Query",
"dynamodb:Scan",
"dynamodb:UpdateItem"
],
"Effect": "Allow",
"Resource": "*"
},
{
"Sid": "",
"Resource": "*",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Effect": "Allow"
}
]
}
Create a deployment package.
zip
function.zip index.js
Create a Lambda function using the create-function
AWS Command Line Interface (AWS CLI) command. For the
role
parameter, enter the execution role's Amazon Resource Name (ARN), which you copied
earlier.
aws lambda create - function -- function -name LambdaFunctionOverHttps\
--zip - file fileb: //function.zip --handler index.handler --runtime nodejs12.x \
--role arn: aws: iam::123456789012: role / service - role / lambda - apigateway - role
Choose the JSON tab, and then paste the following custom policy into the JSON editor.
{
"Version": "2012-10-17",
"Statement": [{
"Sid": "Stmt1428341300017",
"Action": [
"dynamodb:DeleteItem",
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:Query",
"dynamodb:Scan",
"dynamodb:UpdateItem"
],
"Effect": "Allow",
"Resource": "*"
},
{
"Sid": "",
"Resource": "*",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Effect": "Allow"
}
]
}
console.log('Loading function');
var AWS = require('aws-sdk');
var dynamo = new AWS.DynamoDB.DocumentClient();
/**
* Provide an event that contains the following keys:
*
* - operation: one of the operations in the switch statement below
* - tableName: required for operations that interact with DynamoDB
* - payload: a parameter to pass to the operation being performed
*/
exports.handler = function(event, context, callback) {
//console.log('Received event:', JSON.stringify(event, null, 2));
var operation = event.operation;
if (event.tableName) {
event.payload.TableName = event.tableName;
}
switch (operation) {
case 'create':
dynamo.put(event.payload, callback);
break;
case 'read':
dynamo.get(event.payload, callback);
break;
case 'update':
dynamo.update(event.payload, callback);
break;
case 'delete':
dynamo.delete(event.payload, callback);
break;
case 'list':
dynamo.scan(event.payload, callback);
break;
case 'echo':
callback(null, "Success");
break;
case 'ping':
callback(null, "pong");
break;
default:
callback(`Unknown operation: ${operation}`);
}
};
Create a deployment package.
zip
function.zip index.js
Create a Lambda function using the create-function
AWS Command Line Interface (AWS CLI) command. For the
role
parameter, enter the execution role's Amazon Resource Name (ARN), which you copied
earlier.
aws lambda create - function -- function -name LambdaFunctionOverHttps\
--zip - file fileb: //function.zip --handler index.handler --runtime nodejs12.x \
--role arn: aws: iam::123456789012: role / service - role / lambda - apigateway - role
from __future__ import print_function import boto3 import json print('Loading function') def handler(event, context): '' 'Provide an event that contains the following keys: - operation: one of the operations in the operations dict below - tableName: required for operations that interact with DynamoDB - payload: a parameter to pass to the operation being performed '' ' #print("Received event: " + json.dumps(event, indent = 2)) operation = event['operation'] if 'tableName' in event: dynamo = boto3.resource('dynamodb').Table(event['tableName']) operations = { 'create': lambda x: dynamo.put_item( ** x), 'read': lambda x: dynamo.get_item( ** x), 'update': lambda x: dynamo.update_item( ** x), 'delete': lambda x: dynamo.delete_item( ** x), 'list': lambda x: dynamo.scan( ** x), 'echo': lambda x: x, 'ping': lambda x: 'pong' } if operation in operations: return operations[operation](event.get('payload')) else: raise ValueError('Unrecognized operation "{}"'.format(operation))