3
3

More than 3 years have passed since last update.

[AWS][nodejs] S3にある JPEG ファイルの EXIF 情報を削除して書き戻す

Posted at

必要な npm パッケージは aws-sdk, file-type, piexif

S3.getObject でデータ取得して、file-type で JPEG 画像かチェックして、piexif で EXIF を削除して、S3.putObject で書き戻す感じです。

const AWS = require('aws-sdk');
const FyleType = require('file-type');
const piexif = require('piexif');

const removeExif = (region, bucket, key) => {
    const S3 = new AWS.S3({region: region});

    return Promise.resolve({Bucket: bucket, Key: key})
    .then(params => S3.getObject(params).promise())
    .then(data => new Promise(async (resolve, reject) => {
        // mime type のチェック
        const fileInfo = await FyleType.fromBuffer(data.Body);
        if (fileInfo.mime === 'image/jpeg') {
            data.ContentType = fileInfo.mime;
            resolve(data);
        } else {
            reject(Error("It's not jpeg format."));
        }
    }))
    .then(data => new Promise((resolve, reject) => {
        // EXIF 情報の削除
        try {
            const newBody = piexif.remove(data.Body.toString('binary'));
            resolve({
                Bucket: bucket,
                Key: key,
                ContentType: data.ContentType,
                Body: Buffer.from(newBody, 'binary')
            });
        }
        catch (err) {
            reject(err);
        }
    }))
    .then(params => S3.putObject(params).promise())
    .then(data => Promise.resolve(data))
    .catch(err => Promise.reject(err));
}

Lambda ファンクションとして動くようにして、PUT のタイミングで EXIF 情報を削除するとか、Lambda@Edge で動くようにして EXIF 情報を秘匿するとかのサンプルにしてください。

Lambda@Edge で画像ファイルに対するレスポンスを編集するためのサンプルとしては HM さんのも参考になるかも
https://github.com/humanmade/tachyon

3
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
3
3