Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

DAM file InputStream is not reflecting original content in document management systems like Dropbox

Avatar

Level 3

Hi All,

Is there any way to upload a file directly from AEM DAM location like "/content/dam/myFiles" to document management system like Dropbox.

I referred this article http://www.tothenew.com/blog/making-dropbox-documents-available-in-aem/ for "Making Dropbox documents available in AEM".

Using above article I'm able to download dropbox file into AEM DAM successfully, now I'm trying to upload a file from DAM location to Dropbox.

I referred the Dropbox Java Core API v2 as well and found following code for uploading a file to Dropbox :

// Upload "test.txt" to Dropbox

InputStream inputStream = new FileInputStream("test.txt");

FileMetadata metadata = client.files().uploadBuilder("/test.txt").uploadAndFinish(inputStream);

In above code, InputStream is required to upload a file into Dropbox, which is I'm able to retrieve using Asset Interface method getOriginal()

...

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

Resource original = asset.getOriginal();

InputStream inputStream = original.adaptTo(InputStream.class);

...

Here when I'm passing this retrieved InputStream from AEM into Dropbox upload method "uploadAndFinish(inputStream)", It is not uploading the file with its original form but as encrypted or corrupted form.

Please provide your suggestion and answers.

I'm Using AEM 6.2 instance.

Thanks,
Arpit Bora

1 Accepted Solution

Avatar

Correct answer by
Level 3

Thanks everyone for spending your valuable time on this issue,

Using Dropbox REST call to "https://content.dropboxapi.com/1/files_put/auto/<path>?param=val " endpoint using ByteArrayPartSource class of "org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource" package solve my issue,

...

ByteArrayPartSource source = new ByteArrayPartSource(file.getName(),IOUtils.toByteArray(inputStream));

...

Now I'm able to upload file succesfull to Dropbox

Thanks and Regards,

Arpit Bora

View solution in original post

14 Replies

Avatar

Community Advisor

What error are you getting ? Can you log the data on a log file and check if you are able to fetch the correct information ?

Avatar

Administrator

Moving this question to AEM assets Topic.

But please have a look at this:-InputStream object to read from DAM Asset

// An Asset doesn't have an InputStream per se. You need to use one of the renditions.

querybuilder = resource.getResourceResolver().adaptTo(QueryBuilder.class);

Query query = querybuilder.createQuery(PredicateGroup.create(map), session);

SearchResult result = query.getResult();

for (Hit hit : result.getHits())

     { try

          { Resource assetResource = hit.getResource();

            Asset asset = assetResource.adaptTo(Asset.class); // you probably need a null check here...

            Rendition original = asset.getOriginal();

            if (original != null)

                { // it is rare, but some assets might not have an original rendition

                   InputStream stream = original.getStream(); // do something with the stream

                }

          } catch (Exception e) { e.printStackTrace();

           }

}

More ways that I found out [not tested]:- Adobe CQ/Adobe AEM: How to read an external file in CQ

PS:- I am not an expert in assets side, just sharing what I found in the forums.

~kautuk



Kautuk Sahni

Avatar

Level 3

There is no error Veena_07 my code executing successfully, the issue is with InputStream. The InputStream is uploading the file in Dropbox with encrypted or corrupted form.

Avatar

Level 3

Thanks kautuksahni for the suggestion, I tried your piece of code the output is same.

Avatar

Administrator

Can you save this Stream to the filesystem or at any other JCR node to validate if this is an issue with Inputstream or if Dropbox API need some configuration.

~kautuk



Kautuk Sahni

Avatar

Level 3

Hi kautuksahni I tried to save Stream to other JCR node (Asset node) like

...

if (DamUtil.isAsset(resource)) {

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

     if (asset != null) {

         asset.addRendition("original", inputStream, mimeType);

         session.save();

     }

     inputStream.close();

}

...

It is saving successfully but when I'm trying to open the file from my dam location it popup error box with message file content is corrupted.

Avatar

Community Advisor

So that means, you are not reading the content properly.  I will try this and send you a working code may be ? Actually I think I have something similar somewhere , let me get it and share it ... But it won't be nothing different from whatever above all have commented .

Avatar

Community Advisor

How are you fetching the mimeType ?

Avatar

Level 10

See this article - it will show you how to query assets and then convert them to an input stream. In this article - the custom service places the assets into a ZIP file: Adobe Experience Manager Help | Downloading Adobe Experience Manager DAM Assets using Sling Servlets...

Avatar

Community Advisor

ArpitBora

     Since you are trying to upload the asset to Dropbox and not to DAM , this doesn't really answer your original question, but to make sure your input-stream is correct I would recommend you to use Asset Manager API to upload the assets programatically to DAM.

You can find a sample here Adobe Experience Manager Help | Uploading files to Adobe Experience Manager DAM using AssetManager A...

1302016_pastedImage_2.png

  I have a module in my project where we upload file to DAM daily using the below piece of code

Here

is = input stream

filePath = path where you need the file to be uploaded (DAM path)

The API is here com.day.cq.dam.api.AssetManager

     But to answer you original question, once you figure out that your inputstream is proper, we need to look in DropBox API direction and understand if you are missing any dots there.

Avatar

Level 3

I'm using java.nio.file.Files class probeContentType(Path path)method to retrive file mimeType.

Avatar

Correct answer by
Level 3

Thanks everyone for spending your valuable time on this issue,

Using Dropbox REST call to "https://content.dropboxapi.com/1/files_put/auto/<path>?param=val " endpoint using ByteArrayPartSource class of "org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource" package solve my issue,

...

ByteArrayPartSource source = new ByteArrayPartSource(file.getName(),IOUtils.toByteArray(inputStream));

...

Now I'm able to upload file succesfull to Dropbox

Thanks and Regards,

Arpit Bora

Avatar

Level 10

This would make an awesome HELPX article - if you want to submit your code to make it available in an AEM HELPX article - send me an email scottm@adobe.com. We will put your name on the article. This how the AEM Community scales and this will help ppl in the future.