LoginSignup
10
5

More than 5 years have passed since last update.

gz形式でS3に飛んで来たものをlambdaで処理する

Posted at

S3をトリガーにlambdaを作成していたのですがファイル形式がgzだったのでうわぁぁあってなってたら先輩が冷静に解決してくれたのでメモφ(・

ソースコード

index.js

const aws = require("aws-sdk");
const s3 = new aws.S3();
const zlib = require("zlib");

exports.handler = (event, context, callback) => {
  const params = {
    Bucket: "BucketName",
    Key: event.Records[0].s3.object.key
  };
  s3.getObject(params, (err, getData) => {
    if (err) {
      callback(err);
    } else {
        const body = getData.Body;
        zlib.gunzip(body, (err, data) => {
            if (err) {
            callback(err);
            } else {
            // 解凍したデータ
            const stringData = data.toString("utf-8");
            callback(null);
            }
      });
    }
  });
};

zlibはもともと入っているモジュールらしく特に追加で必要なものはないので意外と簡単で便利でした

10
5
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
10
5