Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Script scala para baixar imagens do Amazon S3, criar um thumb e subí-lo de volta para lá. Video explicando o Script: | ----- | Scala script that downloads images from Amazon S3, creates a thumb and sends it back. The video mentioned above explains the code, but it is only in portuguese. I might add ca…
import com.amazonaws.auth.PropertiesCredentials
import scala.collection.JavaConverters._
object migrator extends App {
val imagens = new File("/tmp/imagens")
if (!imagens.exists) imagens.mkdir
val s3 = new AmazonS3Client(new PropertiesCredentials(new File("aws.credentials")))
val bucketName = "jcranky-dev"
val sufixos = List("-small.png", "-medium.png", "-large.png", "-verylarge.png")
var objListing = s3.listObjects(bucketName)
while (objListing.isTruncated) {
objListing = s3.listNextBatchOfObjects(objListing)
def processListing(listing: ObjectListing) = {
val sumarios = listing.getObjectSummaries.asScala.toList
val filtrada = sumarios.filterNot(s => sufixos.exists(s.getKey.endsWith(_)))
filtrada.foreach(s => process(s))
def process(s: S3ObjectSummary) = {
val imgFile = new File(imagens, s.getKey)
s3.getObject(new GetObjectRequest(bucketName, s.getKey), imgFile)
val noExtKey = s.getKey.take(s.getKey.length-4)
new ImageThumber(imgFile, noExtKey).generateThumb(VeryLargeThumb)
val thumbFile = new File(imagens, s"${noExtKey}-verylarge.png")
val putRequest = new PutObjectRequest(bucketName, thumbFile.getName, thumbFile)
println("sending to s3: " + thumbFile.getName)
// ctrl + c lojinha
import java.awt.AlphaComposite
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
class ImageThumber(image: File, imageKey: String) {
def generateThumbs(): List[(File, String)] = ImageThumber.sizes map generateThumb
def imageNameGen(imageKey: String, thumbSize: ThumbSize): String = imageKey + thumbSize.suffix + ".png"
def generateThumb(thumbSize: ThumbSize): (File, String) = {
val imageBuf =
val height = imageBuf.getHeight
val width = imageBuf.getWidth
val imageName = imageNameGen(imageKey, thumbSize)
if (width <= thumbSize.width && height <= thumbSize.height)
(image, imageName)
else {
val (newWidth, newHeight) = ImageThumber.newSizesFor(thumbSize, width, height)
(writeImage(newWidth, newHeight, imageBuf, thumbSize, imageName), imageName)
private def writeImage(width: Int, height: Int, imageBuf: BufferedImage, thumbSize: ThumbSize, imageName: String) = {
val scaledImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB)
val g = scaledImage.createGraphics
g.drawImage(imageBuf, 0, 0, width, height, null);
val destFile = new File(image.getParentFile, imageName)
ImageIO.write(scaledImage, "png", destFile)
object ImageThumber {
val sizes = List(SmallThumb, MediumThumb, LargeThumb, VeryLargeThumb)
def newSizesFor(thumbSize: ThumbSize, originalWidth: Int, originalHeight: Int): (Int, Int) = {
var newWidth: Double = originalWidth
var newHeight: Double = originalHeight
if (newWidth > thumbSize.width){
newWidth = thumbSize.width
newHeight = originalHeight * newWidth / originalWidth
if (newHeight > thumbSize.height) {
val oldHeight = newHeight
newHeight = thumbSize.height
newWidth = newHeight * newWidth / oldHeight
(newWidth.toInt, newHeight.toInt)
sealed case class ThumbSize(width: Int, height: Int, suffix: String)
object SmallThumb extends ThumbSize(100, 100, "-small")
object MediumThumb extends ThumbSize(200, 200, "-medium")
object LargeThumb extends ThumbSize(300, 300, "-large")
object VeryLargeThumb extends ThumbSize(600, 600, "-verylarge")
object OriginalSize extends ThumbSize(0, 0, "")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.