Expand my Community achievements bar.

July 31st AEM Gems Webinar: Elevate your AEM development to master the integration of private GitHub repositories within AEM Cloud Manager.
SOLVED

Asset download from Adobe DAM via third party portal.

Avatar

Level 4

Hi All,

 

We have requirement where third party portal want to download assets from Adobe DAM. We have exposed API/Servlet for them listed below . This is working fine if asset size is small in size and getting downloaded but for large asset it's throwing 500 server error on third party portal . Can someone suggest if anything need to be handled from API/Servlet  or third Party Portal only ? 

 

https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager-assets/servlet-code-to-dow...

 

Thanks in advance !

 

Note: Client don't want to download assets in ZIP format , so using above code in AEM servlet. 

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

@Anoop_Garg  Let me know if that worked after increase time for timeout. For me with 500 MB of asset below code worked 

 

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.service.component.annotations.Component;

@Component(
service = { javax.servlet.Servlet.class },
property = {
"sling.servlet.methods=GET",
"sling.servlet.paths=/bin/downloadlargeasset"
}
)
public class DownloadLargeAssetServlet extends SlingSafeMethodsServlet {

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=largefile.txt");

// Simulating the creation of a large file (500 MB)
InputStream largeFileStream = createLargeFile();

try (ServletOutputStream outputStream = response.getOutputStream()) {
// Stream the content in chunks to avoid loading the entire file into memory
IOUtils.copyLarge(largeFileStream, outputStream);
} finally {
IOUtils.closeQuietly(largeFileStream);
}
}

private InputStream createLargeFile() {
// Simulate creating a large file (500 MB)
byte[] largeFileContent = new byte[500 * 1024 * 1024];
for (int i = 0; i < largeFileContent.length; i++) {
largeFileContent[i] = (byte) (i % 256);
}
return IOUtils.toInputStream(new String(largeFileContent));
}
}

View solution in original post

9 Replies

Avatar

Community Advisor

@Anoop_Garg  can you try setting below header once and try. Let me know if this is working

response.setHeader("Transfer-Encoding", "chunked"); 

 

And also tell me what size of asset is not working for you 

Avatar

Level 4

Thanks @Jagadeesh_Prakash ! Issue is happening for assets above 500 MB. I tried to download assets directly from my custom servlet and its working fine. Added above header as well but still same . Might be I need to ask consumer to increase time for timeout as big binary will take time to send the response. 

Avatar

Correct answer by
Community Advisor

@Anoop_Garg  Let me know if that worked after increase time for timeout. For me with 500 MB of asset below code worked 

 

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.IOUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingSafeMethodsServlet;
import org.osgi.service.component.annotations.Component;

@Component(
service = { javax.servlet.Servlet.class },
property = {
"sling.servlet.methods=GET",
"sling.servlet.paths=/bin/downloadlargeasset"
}
)
public class DownloadLargeAssetServlet extends SlingSafeMethodsServlet {

@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=largefile.txt");

// Simulating the creation of a large file (500 MB)
InputStream largeFileStream = createLargeFile();

try (ServletOutputStream outputStream = response.getOutputStream()) {
// Stream the content in chunks to avoid loading the entire file into memory
IOUtils.copyLarge(largeFileStream, outputStream);
} finally {
IOUtils.closeQuietly(largeFileStream);
}
}

private InputStream createLargeFile() {
// Simulate creating a large file (500 MB)
byte[] largeFileContent = new byte[500 * 1024 * 1024];
for (int i = 0; i < largeFileContent.length; i++) {
largeFileContent[i] = (byte) (i % 256);
}
return IOUtils.toInputStream(new String(largeFileContent));
}
}

Avatar

Level 1

Hi @Jagadeesh_Prakash , we tried this approach but the files which were getting downloaded were of same size and were not opening upon download.

Avatar

Administrator

@Anoop_Garg I hope you found the AEM community helpful. We look forward to seeing you return as either a learner or a contributor. The community flourishes with SMEs like you. Be an ambassober and ask your AEM peers to also contribute here. Happy AEM learning!



Kautuk Sahni

Avatar

Administrator

@Anoop_Garg Did you find the suggestions from users helpful? Please let us know if more information is required. Otherwise, please mark the answer as correct for posterity. If you have found out solution yourself, please share it with the community.



Kautuk Sahni