0

I'm trying to get an image as a response using the public URL of the file :

var request = require('request'); 
request('https://bucket-name.s3.amazonaws.com/file-name').pipe(res);

when I send the request this is the response I get

enter image description here

I need to know how can I get an image file as response instead of that.

Here's the upload function that works fine

 const fileContent = fs.readFileSync(fileName);

// Setting up S3 upload parameters
const params = {
 Bucket: BUCKET_NAME,
 Key: '30.png', // File name you want to save as in S3
 Body: fileContent,
};

// Uploading files to the bucket
s3.upload(params, function (err, data) {
 if (err) {
   throw err;
 }
 console.log(`File uploaded successfully. ${data.Location}`);
});
};```


Maurice
  • 11,482
  • 2
  • 25
  • 45
nermineslimane
  • 541
  • 4
  • 21
  • The response you're getting is the image, only that it tries to convert the binary info to text. Is your question how to save a file from a public URL? – Maurice Feb 22 '21 at 13:00
  • Does this answer your question? [How to download a file with Node.js (without using third-party libraries)?](https://stackoverflow.com/questions/11944932/how-to-download-a-file-with-node-js-without-using-third-party-libraries) – Maurice Feb 22 '21 at 13:03
  • It's actualy an image file , i want to be able to display the image as response not that coded thing i'm getting – nermineslimane Feb 22 '21 at 13:04
  • Where? On a website? – Maurice Feb 22 '21 at 13:10
  • i'm using postman to get the response that later on is going to be displayed on the web which mean that instead of getting it in circular structure i need to get it in JSON structure – nermineslimane Feb 22 '21 at 13:28
  • You can just use `` in your website and let the browser handle it. I don't see why you need to download it yourself. – Maurice Feb 22 '21 at 13:34
  • I'm working on the backend so I need to return the image in JSON format – nermineslimane Feb 22 '21 at 13:37
  • Why would you burden your backend with the image download and transmission if it's public anyway and the client can just fetch it directly? – Maurice Feb 22 '21 at 13:40
  • Because it needs to be displayed not downloaded – nermineslimane Feb 22 '21 at 13:43
  • That's just word games :-) - to display the image, a client needs to have the bytes that make up the image on their end. The client's browser can fetch these bytes directly from S3 do display the image, this doesn't imply long term storage. It can also talk to your backend, which sends JSON back in which the image's bytes are encoded after you've fetched these bytes from S3. That's just an extra step and you may end up needing more compute resources and paying for extra bandwidth with no benefit. – Maurice Feb 22 '21 at 13:47
  • I am dealing with a similar issue but trying to take the image and immediately upload it to another s3 bucket. See my question here if you think you can help: https://stackoverflow.com/questions/73196077/using-aws-download-presigned-url-in-node-to-transfer-file-to-aws-bucket – Craig Howell Aug 03 '22 at 01:58

0 Answers0