View images on S3 with API Gateway + Lambda

Overview

I'm addicted to the specifications, so I'll leave a note.

things to do

  1. Create Lambda
  2. Create API Gateway

It is assumed that S3 has already been created.

1. Create Lambda

Create Lambda with a suitable name. Here, it is get_image.

I also chose Python 3.8 as the runtime.

スクリーンショット 2020-06-27 22.24.52.png

Then, register the following code in the function code.

import boto3
import base64

def get_img_from_s3():
    s3 = boto3.client('s3')
    bucket_name = 'BUCKET_NAME'
    file_path = 'FILE_PATH'
    responce = s3.get_object(Bucket=bucket_name, Key=file_path)
    body = responce['Body'].read()
    body = base64.b64encode(body)
    return body
    

def lambda_handler(event, context):
    img = get_img_from_s3()
    return img

Pass the name of the S3 bucket to bucket_name and the path of the S3 object (image) you want to read to file_name, and read the object from S3.

After that, the read binary format object is encoded in base64 and returned.

Grant Lambda access to S3

The created Lambda does not have the access authority to S3, and if it is left as it is, the access will be denied, so give the access authority to S3.

First, open the Lambda access permission page you created. スクリーンショット 2020-06-27 23.07.20.png Since the automatically created execution role is assigned here, open the page for this execution role. スクリーンショット 2020-06-27 22.47.45.png Press the blue Attach Policy button in the center of the page. スクリーンショット 2020-06-27 22.50.07.png In this way, various policies come out. スクリーンショット 2020-06-27 22.51.21.png Searching for S3 will bring up the policy for S3. This time, the image is read from S3, so attach the policy ʻAmazonS3ReadOnlyAccess`. Now you can read the files stored in S3 from Lambda.

This completes the Lambda settings.

2. Create API Gateway

Next, create API Gateway.

スクリーンショット 2020-06-27 23.17.36.png

REST API was selected as the API type, and the API name was appropriately set to get_image. スクリーンショット 2020-06-27 23.19.33.png Next, I added a GET method and specified Lambda as the integration type and the Lambda function called get_image created earlier as the Lambda function.

Now that the API Gateway has been created, set the method response. スクリーンショット 2020-06-27 23.25.06.png First, Content-Type was added to the response header, and ʻimage / png was added to the content type of the response body. (Since I read png this time, I chose ʻimage / png.)

Next, set the integrated response. スクリーンショット 2020-06-27 23.31.30.png

By default, the content processing method is pass-through, but select Convert to binary (if necessary). Also, let the mapping value of the header mapping be '*'.

This completes the API Gateway settings.

Finally, when you test the API スクリーンショット 2020-06-27 23.42.50.png I was able to return the png binary like this!

When you actually deploy the API and access the URL, the image will be displayed.

reference

Implementation by Node.js Introduction to Lambda + API Gateway. Image DL

Recommended Posts

View images on S3 with API Gateway + Lambda
Send images taken with ESP32-WROOM-32 to AWS (API Gateway → Lambda → S3)
[AWS] Create API with API Gateway + Lambda
Easy REST API with API Gateway / Lambda / DynamoDB
[AWS SAM] Create API with DynamoDB + Lambda + API Gateway
LINE BOT with Python + AWS Lambda + API Gateway
[AWS] Try tracing API Gateway + Lambda with X-Ray
Quickly take a query string with API Gateway-> Lambda (Python)
Create API with Python, lambda, API Gateway quickly using AWS SAM
Mount S3 on Ubuntu with goofys
Move CloudWatch logs to S3 on a regular basis with Lambda
I tried ChatOps with Slack x API Gateway x Lambda (Python) x RDS
LINE BOT (Messaging API) development with API Gateway and Lambda (Python) [Part 2]
I tried to make "Sakurai-san" a LINE BOT with API Gateway + Lambda
Try server-side encryption on S3 with boto3
Transcription of images with GCP's Vision API
[AWS] Link Lambda and S3 with boto3
Connect to s3 with AWS Lambda Python
[AWS] Do SSI-like things with S3 / Lambda
Try implementing a Cisco Spark bot with AWS Lambda + Amazon API Gateway (Python)
Export RDS snapshot to S3 with Lambda (Python)
Upload images to S3 with GUI using tkinter
Pass Cognito Id to Lambda via API Gateway
Amazon API Gateway and AWS Lambda Python version
Embed other images on the raster with ArcPy
Manipulate S3 objects with Boto3 (high-level API and low-level API)
Deploy Python3 function with Serverless Framework on AWS Lambda
I want to AWS Lambda with Python on Mac!
Made "Unofficial Apple Refurbished Product Introduction" BOT with LINE Messaging API (v2) + API Gateway + lambda (python)