使用aws-sdk nodejs上传aws s3文件

时间:2015-12-12 22:19:15

标签: javascript angularjs node.js amazon-web-services amazon-s3

我正在尝试从用户{angularjs frontend}将文件上传到aws s3存储桶并将文件解析为nodejs api路由但仍然收到错误:“TypeError:无法读取未定义的属性'文件'”我能够通过发送文件的发送请求从邮递员(铬扩展)上传文件,但我无法弄清楚如何从角度到节点js解析文件。下面是我在做什么:

// NODEJS代码:

router.post('/user/upload', multipartMiddleware, function(req, res){
        var file = req.files.file;
        var body = fs.createReadStream(file.path);
        var filename =  'post7/media.jpg';

        var s3bucket3 = new AWS.S3();
        s3bucket3.createBucket({Bucket: 'artistarc-user-post-media'}, function() {
          var data = {Bucket: 'artistarc-user-post-media', Key: filename, Body: body};
          s3bucket3.putObject(data, function(err, data) {
            if (err) {
              console.log("Error uploading data: ", err);
            } else {
              console.log("Successfully uploaded data to Bucket");
            }
          });
        });
        res.end('Uploaded');
    });

// ANGULARJS控制器:

myApp.controller('galleryController', ['$scope', 'Api', function($scope, Api){

    console.log('galleryController');
    $scope.file = [];
    $scope.uploadFile = function(){
        Api.MediaUpload.save({}, $scope.file, function(){
            console.log('uploaded');
        });
    }
}]);

// EJS温和

<form>
                <button type="button" class="btn btn-default btn-trans btn-sm" ng-click = "uploadFile()">
                  <span class="glyphicon glyphicon-cloud-upload" aria-hidden="true"></span>
                    Upload
                </button>
                <input type = "file" name = "file"  ng-model="file" file/></div>
</form>

****我也试过使用指令,但它也导致了错误。这是我试过的**

//指令'档'

myApp.directive('file', ['$scope', function($scope){
    return {
        restrict: 'E',
        scope: {
          file: '@'
        },
        link: function(scope, el, attrs){
          el.bind('change', function(event){
            var files = event.target.files;
            var file = files[0];
            scope.file = file;
            scope.$parent.file = file;
            scope.$apply();
          });
        }
    };
}]);

0 个答案:

没有答案