1

I'm trying to send a file using fs.createReadStream() but it doesn't work or give any errors.

I tried using request to put the file to s3 after steaming it from node.

const fs = require("fs");
const request = require("request");
const path = require("path");
let imagePath = path.join(__dirname,"../../public/images/img.jpg");

fs.createReadStream(imagePath).pipe(request.put(signedRequest));

when I change the first part to get an image from a url;

request.get('http://example.com/img.png').pipe(request.put(signedRequest));

it works and uploads the image to s3.

Is there a reason to why this is happening? Or is there any other method I can use to send a file from node to s3?

hussam
  • 174
  • 1
  • 10

1 Answers1

1

Try aws-sdk npm package.
Your code would look like this:

const params = {
  Bucket: bucket,
  Key: fileName,
  Body: data,
};

const options = {
  ACL: 'private',
  CacheControl: 'max-age=86400',
  ContentType: 'text/plain',
};

const s3 = new S3({
  region: 'us-east-1',
  apiVersion: '2006-03-01',
});

await s3.upload(params, options).promise();


Here is useful links: 1, 2

Grynets
  • 2,477
  • 1
  • 17
  • 41
  • Thank you for your reply. I'm using aws-sdk to get the signed request before making a put request. I managed to solve my issue by using fs.readFile() and sending the file by axios instead of request. – hussam Sep 11 '19 at 20:03