Created
May 21, 2019 06:46
-
-
Save impankratov/1e776b740ad1b2906294ca4cd29b5281 to your computer and use it in GitHub Desktop.
Get size of an image from S3 bucket
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
// Wrote this, but because of this issue: | |
// https://github.com/image-size/image-size/issues/37 | |
// had to switch to https://www.npmjs.com/package/probe-image-size | |
import * as sizeOf from 'image-size'; | |
export const getImageSize = (bucket: string) => ( | |
key: string | |
): Promise<ImageSize> => | |
new Promise((resolve, reject) => { | |
const req = s3.getObject({ | |
Bucket: bucket, | |
Key: key | |
}); | |
let buffer = Buffer.from([]); | |
req | |
.on('httpData', chunk => { | |
buffer = Buffer.concat([buffer, chunk]); | |
try { | |
const fs = require('fs'); | |
fs.writeFileSync('buffer.json', JSON.stringify(buffer)); | |
resolve(sizeOf(buffer)); | |
} catch (error) { | |
// Handle image-size related errors | |
reject(error); | |
} | |
req.abort(); | |
}) | |
// Handle aws related errors | |
.on('error', reject) | |
.send(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment