Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package codes.recursive
import com.oracle.bmc.Region
import com.oracle.bmc.auth.AuthenticationDetailsProvider
import com.oracle.bmc.auth.ConfigFileAuthenticationDetailsProvider
import com.oracle.bmc.objectstorage.ObjectStorage
import com.oracle.bmc.objectstorage.ObjectStorageClient
import com.oracle.bmc.objectstorage.model.ObjectSummary
import com.oracle.bmc.objectstorage.requests.GetObjectRequest
import com.oracle.bmc.objectstorage.requests.ListObjectsRequest
import com.oracle.bmc.objectstorage.requests.PutObjectRequest
import com.oracle.bmc.objectstorage.responses.GetObjectResponse
import com.oracle.bmc.objectstorage.responses.ListObjectsResponse
import javax.imageio.ImageIO
import java.awt.*
import java.awt.image.BufferedImage
AuthenticationDetailsProvider provider =
new ConfigFileAuthenticationDetailsProvider(
'~/.oci/config',
'DEFAULT')
ObjectStorage client = new ObjectStorageClient(provider)
client.setRegion(Region.US_ASHBURN_1)
def ns = '[your namespace]'
def bucket = '[your bucket]'
ListObjectsRequest listObjectsRequest = ListObjectsRequest.builder()
.bucketName(bucket)
.namespaceName(ns)
.fields("size")
.build()
ListObjectsResponse listObjectsResponse = client.listObjects(listObjectsRequest)
listObjectsResponse.listObjects.objects.each { ObjectSummary it ->
def kb = it.size / 1024
if( kb > 500 ) {
def getObjectReq = GetObjectRequest.builder()
.namespaceName(ns)
.bucketName(bucket)
.objectName(it.name)
.build()
GetObjectResponse getObjectResponse = client.getObject(getObjectReq)
BufferedImage img = ImageIO.read(getObjectResponse.inputStream)
final int w = img.getWidth()
final int h = img.getHeight()
int type = img.getType() == 0? BufferedImage.TYPE_INT_ARGB : img.getType()
final int w2 = Math.ceil(w/2).toInteger()
final int h2 = Math.ceil(h/2).toInteger()
BufferedImage resizedBuffImg = new BufferedImage(w2, h2, type)
Graphics2D g = resizedBuffImg.createGraphics()
g.drawImage(img, 0, 0, w2, h2, null)
g.dispose()
def newFile = new File("/tmp/resizedimages/${it.name}")
newFile.getParentFile().mkdirs()
String ext = it.name.tokenize(".").last()
ImageIO.write(resizedBuffImg, ext, newFile)
PutObjectRequest putObjectRequest = PutObjectRequest.builder()
.objectName(it.name)
.bucketName(bucket)
.namespaceName(ns)
.putObjectBody( newFile.newInputStream() )
.build()
client.putObject(putObjectRequest)
println "$it.name was reduced from ${kb}kb to ${newFile.size()/1024}kb"
}
}
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.