Skip to content

Instantly share code, notes, and snippets.

@impankratov
Created May 21, 2019 06:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save impankratov/1e776b740ad1b2906294ca4cd29b5281 to your computer and use it in GitHub Desktop.
Save impankratov/1e776b740ad1b2906294ca4cd29b5281 to your computer and use it in GitHub Desktop.
Get size of an image from S3 bucket
// 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