title: node multer AWS single file multiple upload
body: <p>Issue trying to upload images Openshift node/express/multer to AWS. The console tells me that there are multiple uploads of the same image and the resulting file on AWS is truncated and broken. I took all the client side ajax out and am just using a standard form upload. Express 3.2.5. Thanks for any ideas.</p>
  
  <p>The console output:</p>
  
  <pre><code>in FileUploadData
  in FileUploadData
  in s3.put
  Successfully uploaded data to myBucket/myKey atest.jpg
  in s3.put
  Successfully uploaded data to myBucket/myKey atest.jpg
  in FileUploadData
  in FileUploadData
  in s3.put
  Successfully uploaded data to myBucket/myKey atest.jpg
  in FileUploadData
  in s3.put
  Successfully uploaded data to myBucket/myKey atest.jpg
  in FileUploadData
  in no req.files
  in s3.put
  Successfully uploaded data to myBucket/myKey atest.jpg
  in s3.put
  Successfully uploaded data to myBucket/myKey atest.jpg
  </code></pre>
  
  <p>This is not consistent but always multiple triggers of FileUploadData.
  The node code cut-and-pasted from a stackoverflow thread.</p>
  
  <pre><code>app.use(multer({
      limits : { fileSize:100000 },
      rename: function (fieldname, filename) {
          return filename.replace(/\W+/g, '-').toLowerCase();
      },
      onFileUploadData: function (file, data, req, res) {
      // file : { fieldname, originalname, name, encoding, mimetype, path, extension, size, truncated, buffer }
          console.log("in FileUploadData");
          var params = {
              Bucket: 'your20images',
              Key: file.name,
              Body: data,
              ContentType: 'image/jpeg'
  
          };
          s3.putObject(params, function (perr, pres) {
              console.log("in s3.put");
              if (perr) {
                  console.log("Error uploading data: ", perr);
              } else {
                  console.log("Successfully uploaded data to myBucket/myKey "+file.name);
              }
          });
      }
  }));
  </code></pre>
  
  <p>The client side form</p>
  
  <pre><code>&lt;form method="post" enctype="multipart/form-data" action="/upload" id="upform"&gt;
      &lt;input type="file" name="imgupload"&gt;&lt;br /&gt;&lt;br /&gt;&lt;input type="submit"&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>--------------------- edit ----------------------------</p>
  
  <p>Multiple console logs explained in accepted answer bellow. The tentative solution I've found is send to AWS in onFileUploadComplete. Suspect there's a better way but if anyone gets this far bellow worked for me.</p>
  
  <pre><code>onFileUploadComplete: function (file, data, req, res) {
          console.log("complete");
          fs.readFile(data.files.imgupload[0].path, function(err, data){
          if(err) { console.log("image rename error: "+err) }
          var params = {
              Bucket: 'myBucket',
              Key: file.name,
              Body: data,
              ContentType: 'image/jpeg'
          };
          s3.putObject(params, function (err, res) {
              if (err) {
                  console.log("Error uploading data: ", err);
              } else {
                  console.log("Successfully uploaded data to myBucket/myKey");
              }
          });
      });
  }
  </code></pre>