LoginSignup
1
3

More than 3 years have passed since last update.

serverless-framework + serverless-offline で binaryデータを送受信する

Posted at

アップロード(multipart)

  • lambda-multipart-parser

追加

yarn add lambda-multipart-parser

functionへ追加

handler.ts
export const hoge: APIGatewayProxyHandler = async (event, _context) => {
    const MultipartParser = require("lambda-multipart-parser");
    const parsed = await MultipartParser.parse(event);
    const file0 = parsed.files[0];
    const content: Buffer = file0.content;
    ...
);

ダウンロード

設定

  • serverless-apigw-binary

- serverless-apigwy-binary

をインストールし、設定する

yarn add serverless-apigw-binary
yarn add serverless-apigwy-binary
serverless.yml
plugins:
  - serverless-apigw-binary
  - serverless-apigwy-binary

custom:
  apigwBinary:
    types:
      - "image/jpeg"
      - "image/png"

functions:
  hoge:
    handler: handler.hoge
    events:
      - http:
          method: get
          path: hoge
          contentHandling: CONVERT_TO_BINARY

functionへ追加

handler.ts

export const hoge: APIGatewayProxyHandler = async (event, _context) => {
    const img64: string; //image as base64
    return { statusCode: 200, headers: { "Content-Type": "image/png" }, isBase64Encoded: true, body: img64 };
};
1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3