nukulb / s3-angular-file-upload

Example of S3 file upload using ng-file-upload, angular, nodejs
MIT License
135 stars 34 forks source link

onSelectMethod Does not work with newer versions of ng-file-upload #18

Closed Jaicob closed 8 years ago

Jaicob commented 9 years ago

If you try to use the latest version of ng-file-upload you will receive this error

InvalidArgumentBucket POST must contain a field named 'key'. If it is specified, please check the order of the fields.

The solution is to change data to fields in the upload portion of the onfileSelcect method. The new method will look like so

        $scope.onFileSelect = function ($files) {
            $scope.files = $files;
            $scope.upload = [];
            for (var i = 0; i < $files.length; i++) {
                var file = $files[i];
                file.progress = parseInt(0);
                (function (file, i) {
                    $http.get('/api/s3Policy?mimeType='+ file.type).success(function(response) {
                        var s3Params = response;
                        $scope.upload[i] = $upload.upload({
                            url: 'https://' + $rootScope.config.awsConfig.bucket + '.s3.amazonaws.com/',
                            method: 'POST',
                            transformRequest: function (data, headersGetter) {
                                //Headers change here
                                var headers = headersGetter();
                                delete headers['Authorization'];
                                return data;
                            },
                            fields: {
                                'key' : 's3UploadExample/'+ Math.round(Math.random()*10000) + '$$' + file.name,
                                'acl' : 'public-read',
                                'Content-Type' : file.type,
                                'AWSAccessKeyId': s3Params.AWSAccessKeyId,
                                'success_action_status' : '201',
                                'Policy' : s3Params.s3Policy,
                                'Signature' : s3Params.s3Signature
                            },
                            file: file,
                        });
                        $scope.upload[i]
                        .then(function(response) {
                            file.progress = parseInt(100);
                            if (response.status === 201) {
                                var data = xml2json.parser(response.data),
                                parsedData;
                                parsedData = {
                                    location: data.postresponse.location,
                                    bucket: data.postresponse.bucket,
                                    key: data.postresponse.key,
                                    etag: data.postresponse.etag
                                };
                                $scope.imageUploads.push(parsedData);

                            } else {
                                alert('Upload Failed');
                            }
                        }, null, function(evt) {
                            file.progress =  parseInt(100.0 * evt.loaded / evt.total);
                        });
                    });
                }(file, i));
            }
        };
ninjasort commented 9 years ago

Set the file in the data object, not fields:

data: {
  'key' : 's3UploadExample/'+ Math.round(Math.random()*10000) + '$$' + file.name,
  'acl' : 'public-read',
  'Content-Type' : file.type,
  'AWSAccessKeyId': s3Params.AWSAccessKeyId,
  'success_action_status' : '201',
  'Policy' : s3Params.s3Policy,
  'Signature' : s3Params.s3Signature,
  file: file
}