title: How to upload file to s3 through nodejs express from Angularjs
body: <p>I am facing problem to upload file to s3 by nodejs express, and angularjs.</p>
  
  <p>I am using angular directive to send file to node express and from node to s3.</p>
  
  <p>Angular directive :</p>
  
  <pre><code> (function() {
    'use strict';
    angular.module('app').directive('ngFileModel', ['$parse', function ($parse) {
        return {
            restrict: 'A',
            link: function (scope, element, attrs) {
                var model = $parse(attrs.ngFileModel);
                var isMultiple = attrs.multiple;
                var modelSetter = model.assign;
                element.bind('change', function () {
                    var values = [];
                    angular.forEach(element[0].files, function (item) {
                        var value = {
                           // File Name 
                            name: item.name,
                            //File Size 
                            size: item.size,
                            //File URL to view 
                            url: URL.createObjectURL(item),
                            // File Input Value 
                            _file: item
                        };
                        values.push(value);
                    });
                    scope.$apply(function () {
                        if (isMultiple) {
                            modelSetter(scope, values);
                        } else {
                            modelSetter(scope, values[0]);
                        }
                    });
                });
            }
        };
    }]);
  
  
  })();
  </code></pre>
  
  <p>Html code</p>
  
  <pre><code>&lt;input type="file" id="upme" ng-file-model="files" multiple style="display:none;" /&gt;
           &lt;div ng-if="files.length&gt;0" ng-init="vm.uploadFile()"&gt;&lt;/div&gt;
  </code></pre>
  
  <p>Server side:</p>
  
  <pre><code>exports.upload = function(req, res){
    var images = req.body.images;
  
      //res.send(images);
     // console.dir(images)
  
     images.forEach(function(file){
  
  
     // console.dir(file);
      S3.upFile('testbucket',file.name, file.url, function(err, data){
  
        if(err){
          console.log(err)
        }else{
          console.dir(data);
        }
  
      });
  
     });
  </code></pre>
  
  <p>Problem,
  The upload function works and I get something has been uploaded on s3 bucket, the file name appears in bucket; but it seems that is not actual size of the file and I can not open. when I click on the file url it say to download the file, after I download the file, it does not open. I think there may have any problem to parse the file in the node server before uploadin to s3. But I can't identify which solution should be there.</p>
  
  <p>I also get one error in the console.</p>
  
  <pre><code>TypeError: path must be a string or Buffer
      at TypeError (native)
      at Object.fs.open (fs.js:625:11)
      at ReadStream.open (fs.js:1708:6)
      at new ReadStream (fs.js:1695:10)
      at Object.fs.createReadStream (fs.js:1643:10)
  </code></pre>
  
  <p>I have made s3 file upload function as module in separate file. Here is module function of file upload</p>
  
  <pre><code>// uploading file or object into bucket
  exports.upFile = function(bucket_name, key, file, next){
  
    var params = {Bucket: bucket_name, Key: key, Body: fs.createReadStream(file), ACL:"public-read"};
    s3.upload(params, function(err, data) {
      next(err, data);
    });
  
  };
  </code></pre>
  
  <p>I appreciate any help from experts.</p>