title: upload file to s3 using nodejs
body: <p>I have coded a function which upload file to s3 which using <code>aws-sdk</code>, but I've got a problem.</p>
  
  <p>When I replace <code>Body</code> by <code>Body: "test"</code> then the function will execute successfully and I can see the content <code>test</code> in <code>s3</code>.</p>
  
  <p>But I want to upload a document such as <em>pdf</em> file, it can't run and throws an error as:</p>
  
  <blockquote>
    <p>UnhandledPromiseRejectionWarning: TypeError [ERR_INVALID_ARG_TYPE]:
    The "path" argument must be one of type string, Buffer, or URL.
    Received type object at Object.open (fs.js:406:3) at ReadStream.open
    (internal/fs/streams.js:110:12) at new ReadStream
    (internal/fs/streams.js:99:10) at Object.createReadStream
    (fs.js:1725:10)</p>
  </blockquote>
  
  <p>This is my code:</p>
  
  <pre><code>import { createWriteStream, createReadStream } from "fs";
  import AWS from 'aws-sdk';
  const fs = require('fs');
  
  
  const uploadS3 = async ({file}) =&gt; {
    const { stream, filename} = file;
    AWS.config.update({
      accessKeyId: '*******',
      secretAccessKey: '********',
      region: 'us-east-1' 
    });
  
    let params = {
      Bucket: "test-files-staging",
      Key: 'documents/' + filename,
      Body: fs.createReadStream(file),
      ACL: 'public-read'
    };
    await new AWS.S3().putObject(params).promise().then(() =&gt; {
      console.log('Success!!!')
    }).catch((err) =&gt; { console.log(`Error: ${err}`) })
  }
  </code></pre>
  
  <p>Thanks in advance.</p>