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

Avatar

Avatar

vasim_akram_sha

Avatar

vasim_akram_sha

vasim_akram_sha

15-02-2017

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. 

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K
smacdonald2008

15-02-2017

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. 

Answers (4)

Answers (4)

Avatar

Avatar

vasim_akram_sha

Avatar

vasim_akram_sha

vasim_akram_sha

16-02-2017

Thanks smacdonald. It is working as expected.

Avatar

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K
smacdonald2008

15-02-2017

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

Avatar

Avatar

vasim_akram_sha

Avatar

vasim_akram_sha

vasim_akram_sha

15-02-2017

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

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K

Avatar

smacdonald2008

Total Posts

12.7K

Likes

1.4K

Correct Reply

2.3K
smacdonald2008

15-02-2017

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...