Expand my Community achievements bar.

SOLVED

Download hero images for specific pages in AEM

Avatar

Level 5

I have say 15 pages under /content/project/target/* . Now all these 15 pages are using some hero image. What I want to do is download those hero images from those 15 pages into my local. Can someone suggest a way and also help with the relevant code/query ?

NOTE : I don't want to go manual. Like take the path for the image hit it and download from assets. Instead I want some efficient way to do it so that in future if there are 1000 odd pages, I can use this technique.

1 Accepted Solution

Avatar

Correct answer by
Level 2

Hi ,

 

You can use groovy console to write the script to download images

https://aemgeeks.com/aem-tools/use-groovy-script-in-aem/

import java.net.URL
import java.io.File

def targetFolder = '/content/project/target'
def localFolder = '/path/to/local/folder'

def pages = session.findNodes("${targetFolder}/*")

pages.each { page ->
def heroImagePath = page.getProperty("heroImagePath").getString()
def heroImageNode = session.getNode(heroImagePath)
def heroImageFile = new File(localFolder, heroImageNode.getName())
def heroImageURL = new URL(heroImageNode.getPath() + '/file')

heroImageFile.withOutputStream { outputStream ->
heroImageURL.withInputStream { inputStream ->
outputStream << inputStream
}
}

log.info("Downloaded hero image ${heroImageNode.getName()} from page ${page.getPath()} to ${heroImageFile.getPath()}")
}

Thanks,

Kajal 

View solution in original post

2 Replies

Avatar

Community Advisor

Hello @arindam6600 

 

What is the purpose of this download. Is it to install these assets in another AEM as packages?

If yes, one way might be to write a script that would get the paths of these assets, create a Package, add these specific paths as filters and build it.

Sharing a sample code, might be outdated.

// Create a new Package Manager instance
PackageManager packageManager = sling.getService(PackageManager.class);

// Create a new package definition
String packageName = "com.example.myproject";
PackageId packageId = new PackageId(packageName, "1.0.0");
PackageDefinition packageDef = new PackageDefinition(packageId);

// Add content paths to the package definition
List<String> paths = new ArrayList<>();
paths.add("/content/myproject");
packageDef.setPaths(paths);

// Build the package and upload it to the Package Manager
Package packageObj = packageManager.assemble(packageDef, null);
packageManager.upload(packageObj);

// Install the package on AEM
packageManager.install(packageId);

API documentation: JcrPackageManager (The Adobe AEM Quickstart and Web Application.)

 

 


Aanchal Sikka

Avatar

Correct answer by
Level 2

Hi ,

 

You can use groovy console to write the script to download images

https://aemgeeks.com/aem-tools/use-groovy-script-in-aem/

import java.net.URL
import java.io.File

def targetFolder = '/content/project/target'
def localFolder = '/path/to/local/folder'

def pages = session.findNodes("${targetFolder}/*")

pages.each { page ->
def heroImagePath = page.getProperty("heroImagePath").getString()
def heroImageNode = session.getNode(heroImagePath)
def heroImageFile = new File(localFolder, heroImageNode.getName())
def heroImageURL = new URL(heroImageNode.getPath() + '/file')

heroImageFile.withOutputStream { outputStream ->
heroImageURL.withInputStream { inputStream ->
outputStream << inputStream
}
}

log.info("Downloaded hero image ${heroImageNode.getName()} from page ${page.getPath()} to ${heroImageFile.getPath()}")
}

Thanks,

Kajal