0

I have the following use case. We are using AWS presigned url which is SSE-C signed. We need to pass the required headers to use and download from the presigned url. I have the below mentioned code which is working. The problem is that for large files, it is erroring out and there is no visal cue for the customer that the download is in progress or not. Is there an easy way to download files for url(which need headers). if the headers wouldnt have been there, I could have simply done window.open to initiate the browser download. Please suggest.

    function downloadFile() {
  const presignedUrl = 'myurl';


  //header
  const headers = {
    'x-amz-server-side-encryption-customer-algorithm': 'AES256',
    'x-amz-server-side-encryption-customer-key': 'abc',
    'x-amz-server-side-encryption-customer-key-MD5': 'xyz'
  };

  // fech with header
  fetch(presignedUrl, { headers })
    .then(response => {
      if (response.ok) {
        return response.blob();
      } else {
        throw new Error('Failed to download file');
      }
    })
    .then(blob => {
      // Save the file to local machine
      const url = window.URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = url;
      a.download = 'file_download';
      document.body.appendChild(a);
      a.click();
      a.remove();
    })
    .catch(error => {
      console.error(error);
    });
}
jetty
  • 859
  • 2
  • 17
  • 37
  • Well, you could start by changing the `catch()` to do something user-visibile, instead of `console.error(error);`. Then start looking into download progress with `fetch()`; https://stackoverflow.com/questions/47285198/fetch-api-download-progress-indicator – ceejayoz Mar 23 '23 at 12:13
  • Thanks for the input. Is there a way we can delegate this to the browser download? – jetty Mar 23 '23 at 12:24
  • I haven't used it personally, but I believe you're able to include these values when generating the presigned URL, instead of using headers. https://aws.amazon.com/blogs/developer/generating-amazon-s3-pre-signed-urls-with-sse-kms-part-2/ – ceejayoz Mar 23 '23 at 12:26
  • Hello, were you able to find a solution for this? – Vicky21 Aug 08 '23 at 10:59

0 Answers0