Skip to content

Instantly share code, notes, and snippets.

@devotox
Last active January 21, 2021 00:09
Show Gist options
  • Save devotox/3618d34e8c870eff8a5cf4d35e0685f0 to your computer and use it in GitHub Desktop.
Save devotox/3618d34e8c870eff8a5cf4d35e0685f0 to your computer and use it in GitHub Desktop.

Revisions

  1. devotox revised this gist Sep 14, 2017. 1 changed file with 10 additions and 10 deletions.
    20 changes: 10 additions & 10 deletions s3-optimize-images.js
    Original file line number Diff line number Diff line change
    @@ -1,26 +1,26 @@
    #!/usr/bin/node

    const fs = require('fs');
    const path = require('path');
    const AWS = require('aws-sdk');
    const Promise = require('bluebird');
    const imagemin = require('imagemin');
    const fs = require('fs');
    const path = require('path');
    const AWS = require('aws-sdk');
    const Promise = require('bluebird');
    const imagemin = require('imagemin');
    const imageminJpegRecompress = require('imagemin-jpeg-recompress');

    const config = {
    sslEnabled: true,
    bucket: process.env.AWS_BUCKET,
    region: process.env.AWS_REGION,
    bucket: process.env.AWS_BUCKET,
    region: process.env.AWS_REGION,
    accessKeyId: process.env.AWS_ACCESS_KEY,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
    };

    AWS.config.update(config);

    const s3Bucket = config.bucket;
    const s3Endpoint = 's3.' + config.region + '.amazonaws.com';
    const s3Bucket = config.bucket;
    const s3Endpoint = 's3.' + config.region + '.amazonaws.com';
    const s3PublicEndpoint = 'https://' + config.bucket + '.s3.amazonaws.com/';
    const s3 = new AWS.S3({ signatureVersion: 'v4', endpoint: s3Endpoint });
    const s3 = new AWS.S3({ signatureVersion: 'v4', endpoint: s3Endpoint });

    const optimizeImage = (image) => {
    let imageminOptions = {
  2. devotox revised this gist Sep 14, 2017. 1 changed file with 17 additions and 17 deletions.
    34 changes: 17 additions & 17 deletions s3-optimize-images.js
    Original file line number Diff line number Diff line change
    @@ -37,15 +37,15 @@ const optimizeImage = (image) => {
    };

    return imagemin.buffer(image.Body, imageminOptions)
    .then((buffer) => {
    console.info('Optimizing Image Finished:', image.Key);
    console.log('Prev Size', Math.round(image.Body.toString().length / 1000) + 'KB');
    console.log('New Size', Math.round(buffer.toString().length / 1000) + 'KB\n');
    return { Body: buffer, Key: image.Key };
    })
    .catch((error) => {
    console.error('Image Optimization Error:', image.Key, error);
    });
    .then((buffer) => {
    console.info('Optimizing Image Finished:', image.Key);
    console.log('Prev Size', Math.round(image.Body.toString().length / 1000) + 'KB');
    console.log('New Size', Math.round(buffer.toString().length / 1000) + 'KB\n');
    return { Body: buffer, Key: image.Key };
    })
    .catch((error) => {
    console.error('Image Optimization Error:', image.Key, error);
    });
    };

    const listImages = (params) => {
    @@ -90,9 +90,9 @@ const setImage = (image) => {

    const optimize = (image) => {
    return getImage(image)
    .then(optimizeImage)
    .then(setImage)
    .catch(console.error);
    .then(optimizeImage)
    .then(setImage)
    .catch(console.error);
    };

    const run = (Marker, oneRun) => {
    @@ -102,12 +102,12 @@ const run = (Marker, oneRun) => {
    console.log('\nNext Marker', Marker);

    return listImages(params)
    .then((data) => {
    console.log('\nTotal Length:', data.Contents.length);
    .then((data) => {
    console.log('\nTotal Length:', data.Contents.length);

    return Promise.map(data.Contents, optimize)
    .then(() => data.IsTruncated && !oneRun ? run(data.Contents.slice(-1)[0].Key) : null);
    });
    return Promise.map(data.Contents, optimize)
    .then(() => data.IsTruncated && !oneRun ? run(data.Contents.slice(-1)[0].Key) : null);
    });
    };

    return run(process.argv[2], process.argv[3]);
  3. devotox revised this gist Sep 14, 2017. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions s3-optimize-images.js
    Original file line number Diff line number Diff line change
    @@ -35,6 +35,7 @@ const optimizeImage = (image) => {
    })
    ]
    };

    return imagemin.buffer(image.Body, imageminOptions)
    .then((buffer) => {
    console.info('Optimizing Image Finished:', image.Key);
  4. devotox revised this gist Sep 14, 2017. No changes.
  5. devotox revised this gist Sep 14, 2017. No changes.
  6. devotox revised this gist Sep 14, 2017. 1 changed file with 24 additions and 24 deletions.
    48 changes: 24 additions & 24 deletions s3-optimize-images.js
    Original file line number Diff line number Diff line change
    @@ -1,29 +1,29 @@
    #!/usr/bin/node

    const fs = require('fs');
    const path = require('path');
    const AWS = require('aws-sdk');
    const Promise = require('bluebird');
    const imagemin = require('imagemin');
    const imageminJpegRecompress = require('imagemin-jpeg-recompress');
    const fs = require('fs');
    const path = require('path');
    const AWS = require('aws-sdk');
    const Promise = require('bluebird');
    const imagemin = require('imagemin');
    const imageminJpegRecompress = require('imagemin-jpeg-recompress');

    const config = {
    sslEnabled: true,
    bucket: process.env.AWS_BUCKET,
    region: process.env.AWS_REGION,
    sslEnabled: true,
    bucket: process.env.AWS_BUCKET,
    region: process.env.AWS_REGION,
    accessKeyId: process.env.AWS_ACCESS_KEY,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
    };

    AWS.config.update(config);

    const s3_bucket = config.bucket;
    const s3_endpoint = 's3.' + config.region + '.amazonaws.com';
    const s3_public_endpoint = 'https://' + config.bucket + '.s3.amazonaws.com/';
    const s3 = new AWS.S3({ signatureVersion: 'v4', endpoint: s3_endpoint });
    const s3Bucket = config.bucket;
    const s3Endpoint = 's3.' + config.region + '.amazonaws.com';
    const s3PublicEndpoint = 'https://' + config.bucket + '.s3.amazonaws.com/';
    const s3 = new AWS.S3({ signatureVersion: 'v4', endpoint: s3Endpoint });

    const optimize_image = (image) => {
    let imagemin_options = {
    const optimizeImage = (image) => {
    let imageminOptions = {
    plugins: [
    imageminJpegRecompress({
    progressive: true,
    @@ -35,7 +35,7 @@ const optimize_image = (image) => {
    })
    ]
    };
    return imagemin.buffer(image.Body, imagemin_options)
    return imagemin.buffer(image.Body, imageminOptions)
    .then((buffer) => {
    console.info('Optimizing Image Finished:', image.Key);
    console.log('Prev Size', Math.round(image.Body.toString().length / 1000) + 'KB');
    @@ -47,15 +47,15 @@ const optimize_image = (image) => {
    });
    };

    const list_images = (params) => {
    const listImages = (params) => {
    return new Promise((resolve, reject) => {
    s3.listObjects(params, (error, images) => {
    return error ? reject(error) : resolve(images);
    });
    });
    };

    const get_image = (image) => {
    const getImage = (image) => {
    let params = { Bucket: config.bucket, Key: image.Key };
    return new Promise((resolve, reject) => {
    s3.getObject(params, (error, image) => {
    @@ -65,7 +65,7 @@ const get_image = (image) => {
    });
    };

    const set_image = (image) => {
    const setImage = (image) => {
    if(!(image && image.Key && image.Body)) {
    return Promise.resolve();
    }
    @@ -81,16 +81,16 @@ const set_image = (image) => {

    return new Promise((resolve, reject) => {
    s3.putObject(params, (error) => {
    console.log('\nUploaded', params.Key, error);
    console.log('\nUploaded', params.Key, error ? error : '');
    return error ? reject(error) : resolve();
    });
    });
    };

    const optimize = (image) => {
    return get_image(image)
    .then(optimize_image)
    .then(set_image)
    return getImage(image)
    .then(optimizeImage)
    .then(setImage)
    .catch(console.error);
    };

    @@ -100,7 +100,7 @@ const run = (Marker, oneRun) => {

    console.log('\nNext Marker', Marker);

    return list_images(params)
    return listImages(params)
    .then((data) => {
    console.log('\nTotal Length:', data.Contents.length);

  7. devotox revised this gist Sep 14, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion s3-optimize-images.js
    Original file line number Diff line number Diff line change
    @@ -5,7 +5,7 @@ const path = require('path');
    const AWS = require('aws-sdk');
    const Promise = require('bluebird');
    const imagemin = require('imagemin');
    const imageminJpegRecompress = require('imagemin-jpeg-recompress');
    const imageminJpegRecompress = require('imagemin-jpeg-recompress');

    const config = {
    sslEnabled: true,
  8. devotox revised this gist Sep 14, 2017. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions s3-optimize-images.js
    Original file line number Diff line number Diff line change
    @@ -1,16 +1,16 @@
    #!/usr/bin/node

    const fs = require('fs');
    const path = require('path');
    var AWS = require('aws-sdk');
    const fs = require('fs');
    const path = require('path');
    const AWS = require('aws-sdk');
    const Promise = require('bluebird');
    const imagemin = require('imagemin');
    const imageminJpegRecompress = require('imagemin-jpeg-recompress');

    const config = {
    sslEnabled: true,
    bucket: process.env.AWS_BUCKET,
    region: process.env.AWS_REGION,
    sslEnabled: true,
    bucket: process.env.AWS_BUCKET,
    region: process.env.AWS_REGION,
    accessKeyId: process.env.AWS_ACCESS_KEY,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
    };
  9. devotox created this gist Sep 14, 2017.
    112 changes: 112 additions & 0 deletions s3-optimize-images.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,112 @@
    #!/usr/bin/node

    const fs = require('fs');
    const path = require('path');
    var AWS = require('aws-sdk');
    const Promise = require('bluebird');
    const imagemin = require('imagemin');
    const imageminJpegRecompress = require('imagemin-jpeg-recompress');

    const config = {
    sslEnabled: true,
    bucket: process.env.AWS_BUCKET,
    region: process.env.AWS_REGION,
    accessKeyId: process.env.AWS_ACCESS_KEY,
    secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY
    };

    AWS.config.update(config);

    const s3_bucket = config.bucket;
    const s3_endpoint = 's3.' + config.region + '.amazonaws.com';
    const s3_public_endpoint = 'https://' + config.bucket + '.s3.amazonaws.com/';
    const s3 = new AWS.S3({ signatureVersion: 'v4', endpoint: s3_endpoint });

    const optimize_image = (image) => {
    let imagemin_options = {
    plugins: [
    imageminJpegRecompress({
    progressive: true,
    quality: 'high',
    accuracy: true,
    target: 0.995,
    min: 60,
    max: 95
    })
    ]
    };
    return imagemin.buffer(image.Body, imagemin_options)
    .then((buffer) => {
    console.info('Optimizing Image Finished:', image.Key);
    console.log('Prev Size', Math.round(image.Body.toString().length / 1000) + 'KB');
    console.log('New Size', Math.round(buffer.toString().length / 1000) + 'KB\n');
    return { Body: buffer, Key: image.Key };
    })
    .catch((error) => {
    console.error('Image Optimization Error:', image.Key, error);
    });
    };

    const list_images = (params) => {
    return new Promise((resolve, reject) => {
    s3.listObjects(params, (error, images) => {
    return error ? reject(error) : resolve(images);
    });
    });
    };

    const get_image = (image) => {
    let params = { Bucket: config.bucket, Key: image.Key };
    return new Promise((resolve, reject) => {
    s3.getObject(params, (error, image) => {
    image ? image.Key = params.Key : null;
    return error ? reject(error) : resolve(image);
    });
    });
    };

    const set_image = (image) => {
    if(!(image && image.Key && image.Body)) {
    return Promise.resolve();
    }

    let params = {
    Body: image.Body,
    Key: image.Key,
    Bucket: config.bucket,
    ACL: "public-read",
    ContentType: "image/jpeg",
    CacheControl: "max-age=86400, public"
    };

    return new Promise((resolve, reject) => {
    s3.putObject(params, (error) => {
    console.log('\nUploaded', params.Key, error);
    return error ? reject(error) : resolve();
    });
    });
    };

    const optimize = (image) => {
    return get_image(image)
    .then(optimize_image)
    .then(set_image)
    .catch(console.error);
    };

    const run = (Marker, oneRun) => {
    let params = { Bucket: config.bucket, MaxKeys: oneRun ? 1 : 100 };
    params.Marker = Marker;

    console.log('\nNext Marker', Marker);

    return list_images(params)
    .then((data) => {
    console.log('\nTotal Length:', data.Contents.length);

    return Promise.map(data.Contents, optimize)
    .then(() => data.IsTruncated && !oneRun ? run(data.Contents.slice(-1)[0].Key) : null);
    });
    };

    return run(process.argv[2], process.argv[3]);