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 ?
Thanks in advance !
Note: Client don't want to download assets in ZIP format , so using above code in AEM servlet.
Solved! Go to Solution.
Views
Replies
Total Likes
@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));
}
}
@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
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.
@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));
}
}
Hi @Jagadeesh_Prakash , we tried this approach but the files which were getting downloaded were of same size and were not opening upon download.
This worked after increasing the timeout.
@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!
Views
Replies
Total Likes
Try enabling Gzip response for your servlet.
response.setHeader("Content-Encoding", "gzip");
Hi @Anoop_Garg
If you are on cloud, you can check : https://experienceleague.adobe.com/docs/experience-manager-cloud-service/content/assets/manage/asset...
Or Assets selectors if this helps : https://experienceleague.adobe.com/docs/experience-manager-65/assets/managing/asset-selector.html?la...
@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.
Views
Replies
Total Likes
Views
Likes
Replies