Download hero images for specific pages in AEM | Community
Skip to main content
Level 4
February 28, 2023
Solved

Download hero images for specific pages in AEM

  • February 28, 2023
  • 2 replies
  • 1313 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by KajalI

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 

2 replies

aanchal-sikka
Community Advisor
Community Advisor
March 1, 2023

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
KajalIAccepted solution
Level 2
March 1, 2023

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