How to push a file saved to S3 to the frontend for download?

Viewed 28

On my web app, I have a link which the user can click and download a CSV file containing some information.

When the user clicks on the download link, the app retrieves the records from DynamoDB and saves the results as a file to S3 in an asynchronous manner because it may take around 15seconds for the retrieval and saving to complete.

This would imply that when the user clicks on the download link, the file isn't available immediately on S3 to serve to the user.

In this case, how can I push the file to the user on the frontend as soon as the file has been saved successfully to S3 and ready for download?

1 Answers

For long-running jobs, a typical strategy is to track a sort of job ID in the database and update the database when it's done. For example, you might have an API that looks like this:

POST /jobs

{ ... job parameters ... }

And in response:

{
  "id": "c7758160-12c2-448a-b982-1f613e4a9593"
  "status": "processing"
}

Then later on, you can poll for a result:

GET /jobs/c7758160-12c2-448a-b982-1f613e4a9593

If the job status is done or whatever, then you can hit the appropriate S3 URL. (Perhaps your API server returns a signed URL.)

Instead of polling, you can also use server-sent events for the updates.

As an alternative method, for shorter running jobs, you can accept the API request, start the job processing, and only when it's done issue an HTTP 302/307 redirect to the S3 URL. I wouldn't do this for anything typically longer than a couple seconds though.

Related