3 Replies Latest reply on Jun 5, 2013 1:00 PM by iyer.s

    accessing/stroing file from/to DAm programatically

    manojbharal

      how do I write java code to upload a file in DAM ?

       

      How Do I retrieve a file from DAM to save/export on local system. ?

        • 1. Re: accessing/stroing file from/to DAm programatically
          rush_pawan Level 4

          I would refer you to go through "com.day.cq.dam.api" and mainly go through "DamManager, AssetManager, and Asset" classes, it will give you most of the information you needed.

          As an example

          1. To retrieve a dam asset if you know the path

                    Resource resource = resourceResolver.getResource(path);

                    Asset asset=resource.adaptTo(Asset.class);

           

          2. To upload some data

           

          AssetManager manager = resourceResolver.adaptTo(AssetManager.class);

          manager .createAsset(String path, InputStream is, String mimeType, boolean doSave)

           

          Let me know if you need more information.

           

          Thanks,

          Pawan

          • 2. Re: accessing/stroing file from/to DAm programatically
            iyer.s Level 1

            Hello,

            I have been trying to create pdf file within DAM like below, the pdf file is getting created, but, i cannot open the pdf.

            I guess,the file content is not being treated as binary, have you guys seen this issue before?

            How do i get this fixed?!

             

                        InputStream is = new ByteArrayInputStream(fileContent.getBytes());

                        String fileName = "/content/dam/ani/us/pdf/" + UUID.randomUUID().toString() + ".pdf";

             

                        ResourceResolver resolver = slingRequest.getResourceResolver();

                        AssetManager manager = resolver.adaptTo(AssetManager.class);

                        Asset asset = manager.createAsset(fileName, is, "application/pdf", true);

             

            Best,

            Sivaram

            • 3. Re: accessing/stroing file from/to DAm programatically
              iyer.s Level 1

              I dont see any error in the logs, except for the below WARNing in the error.log -

              --

               

              05.06.2013 15:58:26.004 *WARN* [JobHandler: /etc/workflow/instances/2013-06-05/model_113333709904912:/content/dam/ani/us/pdf/952c9427 -ba94-49e4-a6d7-154846ce9d8b.pdf/jcr:content/renditions/original] com.day.cq.dam.handler.standard.pdf.PdfHandler getImage: error while creating image from PDF [/content/dam/ani/us/pdf/952c9427-ba94-49e4-a6d7-154846ce9d8b.pdf/jcr:content/renditions/ original]:  java.io.IOException: Error: Expected an integer type, actual=''

                  at org.apache.pdfbox.pdfparser.BaseParser.readInt(BaseParser.java:1398)

                  at org.apache.pdfbox.pdfparser.PDFObjectStreamParser.parse(PDFObjectStreamParser.java:97)

                  at org.apache.pdfbox.cos.COSDocument.dereferenceObjectStreams(COSDocument.java:532)

                  at org.apache.pdfbox.pdfparser.PDFParser.parse(PDFParser.java:222)

                  at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1070)

                  at org.apache.pdfbox.pdmodel.PDDocument.load(PDDocument.java:1037)

                  at com.day.cq.dam.handler.standard.pdf.PdfHandler.getImage(PdfHandler.java:262)

                  at com.day.cq.dam.core.impl.cache.CQBufferedImageCache.getImage(CQBufferedImageCache.java:99 )

                  at com.day.cq.dam.commons.handler.AbstractAssetHandler.createThumbnails(AbstractAssetHandler .java:174)

                  at com.day.cq.dam.commons.handler.AbstractAssetHandler.createThumbnails(AbstractAssetHandler .java:158)

                  at com.day.cq.dam.core.process.CreateThumbnailProcess.execute(CreateThumbnailProcess.java:11 3)

                  at com.day.cq.workflow.impl.job.HandlerBase.executeProcess(HandlerBase.java:184)

                  at com.day.cq.workflow.impl.job.JobHandler.process(JobHandler.java:112)

                  at org.apache.sling.event.jobs.JobUtil$1.run(JobUtil.java:272)

                  at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)

                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

                  at java.lang.Thread.run(Unknown Source)