Retrieve an item from an S3 bucket using an Express JS server and deliver it as the response

When a client makes an authorized request, I need to send a file back as a response. The file is retrieved from the S3 bucket. What steps should I take to retrieve the file from S3 and deliver it to the client?

const s3 = new S3Client({...})

router.get('/getfile', (req, res) => {
  const data = await s3.send(
    new GetObjectCommand({
      Bucket: 'bucket',
      Key: 'path',
    })
  )

  // How can we efficiently send back the file?
  // res.send(fileStream)
})

What is the best method for sending back the data?

Answer №1

Implement the pipe() method to send the stream directly to the response.

const { Body, ContentType, ContentDisposition } = await s3.send(command);

res.setHeader('Content-Type', ContentType);
res.setHeader('Content-Disposition', ContentDisposition || `attachment; filename="untitled"`);

Body.pipe(res);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Experiencing difficulty in rendering API within React component

Trying to display an image from a specific API that I came across here. However, encountering the following error in the console... Error with Fetch API: TypeError - this.state.dogs.map is not functioning properly. Code snippet provided below: <htm ...

What is the most effective way to retrieve data when a user clicks?

I am currently working on creating a menu that showcases three different categories: Streaming, On Tv, and In Theaters. This is inspired by the layout found on The initial data displayed belongs to the Streaming section, which is being fetched via JSON fr ...

Issues with CSS not loading properly on EJS files within subdirectories

This is the structure of my folders (with views located in the root directory): views/contractor/auth/login.ejs When I access that file, the CSS styles are not being applied. The connection to the CSS file, which is located in the public directory in th ...

Certain HTML files linked to a Dreamweaver template (.dwt) are having trouble recognizing jQuery/JavaScript

My current task involves updating the navigation of a website utilizing a Dreamweaver template (DWT) as the base file. Initially, the navigation was created using jQuery navigation UI, but it did not fulfill all our requirements. Subsequently, I substitute ...

Odd behavior in Google Chrome when PHP includes JavaScript code

When working with my application built on Zend Framework, I have a layout.phtml file that contains the <head> section with some javascripts included using the following method: <head> // Some code <?php include('template/javascript ...

When an ad call is successful in ajax, the response includes the usage of document

Currently, we are facing a challenge in loading an ad after an ajax load more. The ad call response contains document.write, which redirects to the ad itself once the call is made. Unfortunately, there is no straightforward method to only get the ad html a ...

Acquiring the event parameter from a function that is not a callback (regular function invocation)

In order to extract the event argument from a standard function invocation, developers often refer to callback functions as a solution: document.addEventListener('something', function(e){e.target.parentNode...etc}); The following snippet showca ...

Arranging an array in reverse order (starting with underscore) can alter the attributes of the objects within

REVISION: To provide more clarity on the issue, I have recorded a bug using quicktime. Here is the link for your reference: For additional code details, please visit: ORIGINAL INQUIRY: A strange issue has come up while working with Meteor and its depen ...

Dynamic sidebar that adheres to the content and is placed alongside it using JavaScript

I am in need of creating a sticky sidebar that will float beside my content column. The purpose of this sidebar is to contain jump links on the page, similar to what can be seen on this page, but with the navigation buttons positioned directly next to the ...

Unable to generate StumbleUpon traffic for a URL through AJAX due to the API returning text/plain

I'm attempting to acquire StumbleUpon views for a specific URL using $.ajax. This is the API I am utilizing: http://www.stumbleupon.com/services/1.01/badge.getinfo?url=http://example.com/ Here is the complete code snippet: $.ajax({ type: "GET ...

Any advice on applying jquery CSS to elements contained within a dynamically loaded div element?

element, I have encountered an issue with integrating a PHP file to process and display a table within a blank div element upon clicking a button. Despite applying styles from my style.css file, the jQuery mobile styles do not seem to be taking effect. I ...

Is there a way to showcase the output of the <div id="height"> on the height of the rectangle?

My Current Project Currently, I am in the process of developing a picture framing calculator using a combination of HTML, CSS, and JavaScript. Functionality of the Calculator For this calculator, users will input values such as: Frame Width (wf): 16 ...

Create a unique variable for each MongoDB document that increases with each iteration

I have a set of records in my database that look like this: [{_id: abc, name: "foo"}, {_id: def, name: "bar"}, {_id: ghi, name: "baz"}] I am looking to update each record by adding a unique customId field. This customId fiel ...

JavaScript's wildcard character, *, in a regular expression will always match something

I am currently attempting to verify whether a given string contains only uppercase letters, numbers, and underscores by utilizing the pattern matching approach with /[A-Z0-9_]*/. Despite this, executing the following code results in a return of true: /[A ...

Using Javascript and jQuery to validate strings within an array

My existing jQuery code works perfectly by returning true if it matches the specified name. jQuery(function($) { var strings = $(".user-nicename").text(); if (strings === "name1") { $('.mention-name').hide(); $('.se ...

A guide on displaying complete text as the container width expands using Tailwind CSS and Next.js

On my platform, users have the ability to create albums and give them names of any length. When a user creates an album, it generates a div along with a link (the album's name) and two icons. I am looking to implement a feature where if a user enters ...

Is there a possibility of encountering an endless update loop within a component's render function?

Hello! I am a beginner in Vue.js and I have encountered an issue with my function. Instead of increasing my variable to 1 as expected, it is increasing it to a random number every time. The console is displaying the following error message: "You may hav ...

Implementing a function similar to 'onload' for a table data cell element

I have a data table in my Django template, with a td element in the table that looks like this: <td onclick="setColor('{{x.name}}')">{{x.name}}</td> I want the 'setColor' function to be executed every time the webpage loa ...

Need help troubleshooting a problem with the <tr><td> tag in vue.js?

I have a table that updates when new data is created, and I want to achieve this using Vue.js I decided to explore Vue.js Components and give it a try. <div id="app1"> <table class="table table-bordered"> <tr> ...

Leveraging React's UseEffect() to retrieve information from GraphQL

I am currently attempting to retrieve data from graphQL. I understand that by placing a function inside the react UseEffect(), I can trigger the function once the data is updated and structured. However, while working on a chatroom feature, I am facing an ...