Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fileWriteStreamHandle doesn't call the passed function in express #675

Closed
dev-alianwar opened this issue Jan 11, 2021 · 20 comments
Closed

Comments

@dev-alianwar
Copy link

dev-alianwar commented Jan 11, 2021

Avantages

const { PassThrough } = require("stream");
const aws = require("aws-sdk");

const s3 = new aws.S3({
  apiVersion: "2006-03-01",
  accessKeyId: process.env.AWS_ACCESS_KEY_ID,
  secretAccessKey: process.env.AWS_SECRET_ACCESS_KEY,
});


function uploadFile(req, res, next) {

  const uploadStream = (filename) => {
    const pass = PassThrough();
    
    /* This method is not being called*/
    console.log('uploadStream() called);

    s3.upload(
        {
        Bucket: process.env.AWS_BUCKET,
        Key: filename,
        Body: pass,
        },
        (err, data) => {
        console.log(err, data);
        if (err) {
            console.log(err);
        }
        console.log(data);
        }
    );
    
    return pass;
  };

  var form = formidable({
    maxFileSize: 5 * 1024 * 1024,
    minFileSize: 1,
    allowEmptyFiles:false,
    fileWriteStreamHandler: uploadStream // This method is not being called
  });
}

I have followed the example in link https://github.com/node-formidable/formidable/blob/master/examples/store-files-on-s3.js

@dev-alianwar dev-alianwar added the bug Something isn't working label Jan 11, 2021
@GrosSacASac
Copy link
Contributor

What version of formidable are you using ?

console.log is missing a single quote '

@GrosSacASac GrosSacASac added need more info and removed bug Something isn't working labels Jan 11, 2021
@tunnckoCore
Copy link
Member

@ali-chaudhry this from the master branch isn't published nor tagged yet.

To work you need to install from master with npm i node-formidable/formidable#master

@sinoon
Copy link

sinoon commented Jan 18, 2021

hi by the way, why not yet publish v2 ?

@tunnckoCore
Copy link
Member

tunnckoCore commented Jan 18, 2021

@sinoon there is a published canary/v2 (npm i formidable@canary) from May 2020 with all the updates.
Pushed to master since then isn't published. I'm getting my machine setup this week.

@dev-alianwar
Copy link
Author

What version of formidable are you using ?

console.log is missing a single quote '

Sorry for late reply, I have fixed the issue

@GrosSacASac
Copy link
Contributor

How ?

@cloudworkpro-dave
Copy link

@GrosSacASac Hello, thanks in advance for your help. I'm having the same issue and didn't see any solution by downloading master. Is there a reason why this is closed? Doesn't this issue affect all the posted examples and such?

@GrosSacASac
Copy link
Contributor

The examples should work. Put a breakpoint to find out why the function is not called.

@rista404
Copy link

Hey guys, I'm having the same issue. The function is simply not being called. I installed the canary version of the package.

Additionally, the example is invalid as it is missing a new keyword before the PassThrough().

@GrosSacASac
Copy link
Contributor

Canary is outdated

@GrosSacASac
Copy link
Contributor

Fixed the example I think

@Golem8
Copy link

Golem8 commented Jun 28, 2021

I'm still not able to upload to s3 with the example. uploadStream does not appear to be called. It saves the uploads in the tempdir. 'here' is not printed to the console when uploading

const http = require('http');
const { PassThrough } = require('stream');
// eslint-disable-next-line import/no-unresolved
const AWS = require('aws-sdk');
const formidable = require('formidable');

const s3Client = new AWS.S3({
    credentials: {
      accessKeyId: process.env.AWS_ACCESS_KEY_ID,
      secretAccessKey: process.env.AWS_SECRET_KEY,
    },
  });

const uploadStream = (file) => {
    console.log('here');
    const pass = new PassThrough();
    s3Client.upload(
      {
        Bucket: 'demo-bucket',
        Key: file.newFilename,
        Body: pass,
      },
      (err, data) => {
        console.log(err, data);
      },
    );
  
    return pass;
  };
  
  const server = http.createServer((req, res) => {
    if (req.url === '/api/upload' && req.method.toLowerCase() === 'post') {
      // parse a file upload
      const form = formidable({
        fileWriteStreamHandler: uploadStream,
      });
  
      form.parse(req, () => {
        res.writeHead(200);
        res.end();
      });
  
      return;
    }
  
    // show a file upload form
    res.writeHead(200, { 'Content-Type': 'text/html' });
    res.end(`
      <h2>With Node.js <code>"http"</code> module</h2>
      <form action="/api/upload" enctype="multipart/form-data" method="post">
        <div>Text field title: <input type="text" name="title" /></div>
        <div>File: <input type="file" name="file"/></div>
        <input type="submit" value="Upload" />
      </form>
    `);
  });
  
  server.listen(3000, () => {
    console.log('Server listening on http://localhost:3000 ...');
  });```

@GrosSacASac
Copy link
Contributor

npm ls formidable

what is your output if you type this command in the cli ?

@Golem8
Copy link

Golem8 commented Jun 29, 2021

image

@GrosSacASac
Copy link
Contributor

yeah that's the problem, fileWriteStreamHandler does not exist in version 1.2.2

@GrosSacASac
Copy link
Contributor

npm i github:node-formidable/formidable
or
npm i github:node-formidable/formidable#3.x

@Golem8
Copy link

Golem8 commented Jun 29, 2021

Awesome thanks!

@wtesler
Copy link

wtesler commented Sep 30, 2021

npm i github:node-formidable/formidable or npm i github:node-formidable/formidable#3.x

What is version 3 of formidable? I only see 1.2.2 stable and then 2.x canary/dev. Where is version 3?

@GrosSacASac
Copy link
Contributor

@dev-alianwar
Copy link
Author

What version of formidable are you using ?
console.log is missing a single quote '

Sorry for late reply, I have fixed the issue

Thank you for looking into the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

8 participants