user984621
user984621

Reputation: 48503

Rails - how to fetch images from S3, resize them and save thumbnails to S3?

We have a lot of images stored in an Amazon S3 bucket and we would need to resize them. So, we would need to grab all images in the bucket and one by one resize them (according to their orientations). What's the best way to do that? To write just a ruby script or is there any way how to do it?

Thanks

Upvotes: 1

Views: 1427

Answers (2)

Litmus
Litmus

Reputation: 10996

A simple shell script can also be used (with some external help)

Install s3cmd. It is a command line tool to interact with s3. Install ImageMagick. This is what rMagic uses under the hood

Then use it in a shell script like this

#!/bin/bash
S3CMD=$(which s3cmd)
CONVERT=$(which convert)
#
# Download the file from s3
$S3CMD get s3://mybucket/path/to/image/file.gif

# convert it to thumbnail
$CONVERT file.gif    -resize 64x64  resize_file.gif

# upload the thumbnail back to s3
$S3CMD put resize_file.git s3://mybucket/path/to/thumbnails/resize_file.gif

# cleanup
rm file.gif resize_file.gif

Note: The sample script above does not have any error checking. You should check the status code of each command before executing the next one.

ImageMagick is very powerful.

Please see this for various way you can use resize an image.

You can also make thumbnails like this

s3cmd is capable of downloading all files from an s3 path. And ImageMagick is capable of batch processing (though the example script does not depict it). If you instead wish to process one image at a time, you should suitably modify the script for looping.

On the other hand, if you are already using paperclip in your application, it comes with some rake tasks. check out the documentation

rake paperclip:refresh:thumbnails

Upvotes: 1

Kyle Truscott
Kyle Truscott

Reputation: 1577

In the past, I've used a combo of the aws-sdk-ruby gem and rmagick in a worker class that:

  • Downloads the original file from S3 locally
  • Applies auto_orient
  • Resizes it for x/y/z
  • Re-uploads the orignal and the new versions back up to S3

You'd be able to queue a process like this in the background (delayed_job/sideqik/resque/etc) whenever you receive a future image.

Here's a gist.

Upvotes: 2

Related Questions