Lambda でメモリサイズをあげておかないと、大きいファイルの場合は image magick の実行でエラーになります。
Created
April 9, 2019 10:01
-
-
Save isann/cf1a0a529e1bf395c6e8218fca09ee0a to your computer and use it in GitHub Desktop.
Lambda で imagemagick の convert をするサンプル
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs'); | |
const aws = require('aws-sdk'); | |
const im = require('imagemagick'); | |
const lambda = new aws.Lambda({/*apiVersion: '2014-11-11'*/}); | |
const s3 = new aws.S3({ | |
'apiVersion' : '2006-03-01', | |
'region' : 'ap-northeast-1', | |
}); | |
const bucket = 'XXXXX'; | |
exports.handler = async function (event, context, callback) { | |
// await convertImage(); | |
start(); | |
callback(null, 'ended'); | |
}; | |
function convertImage(srcPath, outputPath) { | |
return new Promise((resolve, reject) => { | |
im.convert([srcPath, '-resize', '1000x668', outputPath], | |
async function (err, stdout) { | |
if (err) { | |
console.log('convert error', err); | |
reject(); | |
} else { | |
console.log('convert success'); | |
resolve(); | |
} | |
}); | |
}); | |
} | |
function get(key) { | |
return new Promise((resolve, reject) => { | |
s3.getObject({ | |
Bucket: bucket, | |
Key : key, | |
}, function (err, data) { | |
if (err) { | |
console.log("get error", err); | |
reject(); | |
} else { | |
console.log("get success"); | |
resolve(data); | |
} | |
}); | |
}); | |
} | |
function put(v) { | |
return new Promise((resolve, reject) => { | |
const m = fs.readFileSync(v); | |
const timeStamp = (+new Date()); | |
s3.putObject({ | |
Bucket : bucket, | |
Key : 'test' + timeStamp + '.jpeg', | |
Body : m, | |
CacheControl: 'max-age=604800', | |
// ContentType: contentType | |
}, function (err, res) { | |
if (err) { | |
console.log("put error"); | |
reject(); | |
} else { | |
console.log("put success"); | |
resolve(); | |
} | |
}); | |
}); | |
} | |
async function start() { | |
await get('IMG_20190408_211835.jpg').then(async (data) => { | |
const timeStamp = (+new Date()); | |
const inputFilePath = '/tmp/XXXXXXX-' + timeStamp + '.jpg'; | |
const outputPath = '/tmp/XXXXXXX-out-' + timeStamp + '.jpg'; | |
fs.writeFileSync(inputFilePath, data.Body); | |
await convertImage(inputFilePath, outputPath).then(async () => { | |
await put(outputPath).then(() => { | |
}, () => { | |
}); | |
}, () => { | |
}); | |
}); | |
console.log('finis.h'); | |
} | |
// start(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment