I have a requirement to upload a CSV file using Rest API, process this file in AWS Lambda, then provide a response in API back to the user. How can I achieve this? I thought of uploading the file to s3 using API and then triggering a lambda function based on s3 events, but in this case, I won't be able to provide a response to the user.
7
-
What type of response are you looking for when the user upload the file?Bert Cafecito– Bert Cafecito2023-01-25 06:14:54 +00:00Commented Jan 25, 2023 at 6:14
-
@AlbertMarrero after processing output will be generated in form of new csv and that will be returned to user in API response.sid8491– sid84912023-01-25 08:02:08 +00:00Commented Jan 25, 2023 at 8:02
-
Do you have a limit on how big the file uploaded can be? I feel that you are doing too much in one call, and maybe you need to break up. I am concerned that you might reach a Lambda timeout if you do too much.Bert Cafecito– Bert Cafecito2023-01-25 08:36:12 +00:00Commented Jan 25, 2023 at 8:36
-
1@AlbertMarrero file will be max 3 MB. how can I break this into multiple steps? I was thinking of creating PUT API to upload the file, then creating a trigger to invoke lambda, process the file, and upload the file to s3. problem is, how can I now send the output file link to user?sid8491– sid84912023-01-25 09:24:17 +00:00Commented Jan 25, 2023 at 9:24
-
For the output file link, you can return a pre-signed URL.Bert Cafecito– Bert Cafecito2023-01-26 07:14:31 +00:00Commented Jan 26, 2023 at 7:14
|
Show 2 more comments
1 Answer
I am adapting this article to your question.
Create a PUT method for your API for uploading CSV file
- Choose Actions, and then choose Create Method.
- From the dropdown list, choose PUT and then choose the check mark icon.
- Under the Integration type category, choose AWS Service.
- For AWS Region, choose us-east-1 or the AWS Region you see on the Bucket properties page.
- For AWS Service, choose Simple Storage Service (S3).
- Leave AWS Subdomain empty.
- For the HTTP method, choose PUT.
- For Action Type, choose Use path override.
- For Path override (optional), enter {bucket}/{key}.
- For the Execution role, enter the Amazon Resource Name (ARN) for the IAM policy for the API Gateway role.
- For Content Handling, choose Passthrough.
- Choose Save.
Configure parameter mappings for the PUT method
- In the Resources panel of your API page, choose PUT.
- Choose Integration Request.
- Expand URL Path Parameters.
- Choose Add path.
- For Name, enter bucket.
- For Mapped from, enter Method.request.path.folder.
- Choose the checkmark icon at the end of the row.
- Repeat steps 4 through 7. In step 5, set Name to key. Step 6 sets Mapped from to Method.request.path.object.
Create a GET method for your API for retrieving a CSV file
- In the Resources panel of your API page, choose /{object}.
- Choose Actions, and then choose Create Method.
- From the dropdown list, choose GET and then choose the check mark icon.
- Under the Integration type category, choose AWS Service.
- For AWS Region, choose us-east-1, or the Region you see on the Bucket properties page.
- For AWS Service, choose Simple Storage Service (S3).
- Leave AWS Subdomain empty.
- For the HTTP method, choose GET.
- For Action Type, choose Use path override.
- For Path override (optional), enter {bucket}/{key}.
- For the Execution role, enter the Amazon Resource Name (ARN) for the IAM policy for the API Gateway role.
- For Content Handling, choose Passthrough.
- Choose Save.
Configure parameter mappings for the GET method
- In the Resources panel of your API page, choose GET.
- Choose Integration Request.
- Expand URL Path Parameters.
- Choose Add path.
- For Name, enter bucket.
- For Mapped from, enter Method.request.path.folder.
- Choose the checkmark icon at the end of the row.
- Repeat steps 4 through 7. In step 5, set Name to key. Step 6 sets Mapped from to Method.request.path.object.
Set up response mapping to see the CSV in the browser
- In the Resources panel of your API page, choose GET.
- Choose Method Response.
- Expand 200.
- Under Response Body for 200, remove application/JSON.
- Under Response headers for 200, choose Add header.
- For Name, enter content type.
- Choose the check mark icon to save.
- Choose Method execution to go back to the Method Execution pane.
- Choose Integration Response.
- Expand 200, and then expand Header Mappings.
- Choose the pencil icon at the end of the row named content-type.
- Enter text/CSV to see a CSV file.
Set up binary media types for the API
- In the navigation pane of your API page, choose Settings.
- In the Binary Media Types section, choose Add Binary Media Type.
- In the text box, add the following string: /
Note: Avoid putting the string in quotes. You can substitute a wildcard for a particular Multipurpose Internet Mail Extensions (MIME) type you want to treat as a binary media type. For example, choose "image/jpeg" to have API Gateway treat JPEG images as binary media types. If you add /, then API Gateway treats all media types as binary media types.
- Choose Save Changes.
Resolve CORS error with binary settings for API
- If you want to use the aforementioned APIs (PUT and GET) in a web application, you might encounter a CORS error.
- To resolve the CORS error with binary settings turned on, start CORS from the API Gateway console.
- In the Resources panel of your API page, select /{object}.
- For Actions, choose Enable CORS.
- Choose Enable CORS and replace existing CORS headers.
- Use the update integration CLI command to update the content handling property. This update allows the binary content to be handled for preflight options requests with mock integration.
- Run the two CLI commands that follow by updating the API ID, Resource ID, and AWS Region. You can obtain the API ID and Resource ID from the top of the Gateway API console when you select the {object} resource.
Finally, deploy your API and upload a CSV file via your API
curl -i --location --request PUT 'https://abc12345.execute-api.us-west-2.amazonaws.com/v1/testfolder/test.csv' --header 'Content-Type: text/csv' --data-binary '@/Path/to/file/test.csv'