Expand my Community achievements bar.

SOLVED

How to upload image in Dam using standalone application which will generate metadata?

Avatar

Level 2

Hi,

I have a image in local drive. I want to upload that image to in my AEM dam. For that I have written following code.

public class ThroughHttpPost { public static void main(String[] args) { // TODO Auto-generated method stub File file = new File("D:\\images\\test.jpg"); byte[] bytesArray = new byte[(int) file.length()]; FileInputStream fis; try { fis = new FileInputStream(file); fis.read(bytesArray); //read file into bytes[] fis.close(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } DefaultHttpClient httpClient = new DefaultHttpClient();//Client HttpPost postRequest = new HttpPost("http://localhost:4502/content/dam/test");//Post Request to specified URL httpClient.getCredentialsProvider().setCredentials( new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT), new UsernamePasswordCredentials("admin", "admin")); ByteArrayBody bab = new ByteArrayBody(bytesArray, "test.jpg"); MultipartEntityBuilder reqEntity = MultipartEntityBuilder.create(); reqEntity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE); reqEntity.addPart("test.jpg", bab); postRequest.setEntity(reqEntity.build()); try { HttpResponse response = httpClient.execute(postRequest); response.getStatusLine(); System.out.println("done"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }

I am able to push that image to dam. But the problem is that it is taking the image as nt:file. It doesn't have any metadata as shown in attachment. 

1 Accepted Solution

Avatar

Correct answer by
Level 10

The outside Java app should only post to an AEM servlet.

So you need 2 parts: 

1 - outside client app (which you have) that posts files to AEM. 

2 - an AEM Sling servlet that uses the AssetManager API

See this article that does this exact use case from a outside Java Swing client:

  • handles multiple files by reading an XML file (defines where the assets are located, etc)
  • posts each file to a Sling servlet that handles the file and uses AssetManager

https://helpx.adobe.com/experience-manager/using/multiple-digital-assets.html

Use of the AssetManager API is the best way when you want to upload files from an outside client. 

View solution in original post

5 Replies

Avatar

Level 10

When coding the Servlet that accepts the file - use the AssetManager API. See this article -- 

https://helpx.adobe.com/experience-manager/using/uploading-files-aem1.html

Hope this helps... 

Avatar

Level 2

This will help only when my servlet inside OSGI container. But mine is standalone application. I am using it outside the AEM server. In the above link we are getting ResourceResolver from ResourceResolverFactory. In my case it will give null for ResourceResolver. 

For your info If I have tried this using curl command 

curl -u admin:admin -T D:\images\test.jpg http://localhost:4502/content/dam/test/test.jpg

It is working fine. I am getting metadata. But mine is java application.

Avatar

Correct answer by
Level 10

The outside Java app should only post to an AEM servlet.

So you need 2 parts: 

1 - outside client app (which you have) that posts files to AEM. 

2 - an AEM Sling servlet that uses the AssetManager API

See this article that does this exact use case from a outside Java Swing client:

  • handles multiple files by reading an XML file (defines where the assets are located, etc)
  • posts each file to a Sling servlet that handles the file and uses AssetManager

https://helpx.adobe.com/experience-manager/using/multiple-digital-assets.html

Use of the AssetManager API is the best way when you want to upload files from an outside client. 

Avatar

Level 10

We have a video on our youtube channel that shows this use case: https://www.youtube.com/watch?v=smrFuVLfiBc

Avatar

Level 2

Thanks smacdonald. It is working as expected.