0

I've watched and read a bunch of videos and tutorials but I'm getting a 403 error.

I'm using Angular 1.

First, I made an Angular directive because ng-model doesn't work with files. I called my directive file-model:

app.directive('fileModel',['$parse', function ($parse){
  return {
    restrict: 'A',
    link: function (scope, element, attrs) {
      element.bind('change', function () {
        $parse(attrs.fileModel)
        .assign(scope, element[0].files[0])
        scope.$apply();
      })
    }
  }
}]);

Then I used my directive in the HTML template:

<form ng-submit="uploadFile(file)">
      <input type="file" accept="txt" file-model="file" class="form-control">
      <button type="submit" class="btn btn-primary">Upload File</button>
</form>

And I made a handler in the controller:

  app.controller('myController', ['$scope', '$firebaseStorage', function($scope, $firebaseStorage) {

  // Create a Firebase Storage reference
  var storage = firebase.storage();
  var storageRef = storage.ref();
  var filesRef = storageRef.child('files');

  $scope.uploadFile = function(file) {
    console.log("Let's upload a file!");
    console.log($scope.file);
    var storageRef = firebase.storage().ref("files");
    $firebaseStorage(filesRef).$put($scope.file);
  };

}]);

Lastly I set the Firebase Storage rules to "public":

service firebase.storage {
  match /b/myFirebaseProject.appspot.com/o {
    match /{allPaths=**} {
      allow read, write: if request.auth != null;
    }
  }
}

Running all this, I can select my test file, click Upload File, and the file loads into the $scope. My directive is working. But then I get an error message:

POST https://firebasestorage.googleapis.com/v0/b/myFirebaseProject.appspot.com/o?name=files 403 ()

I tried creating a files folder in Firebase Storage but I got the same 403 error.

Thanks!

1 Answer 1

1

The rules you posted aren't public, they require authentication. Instead you want to use something like:

service firebase.storage {
  match /b/myFirebaseProject.appspot.com/o {
    match /{allPaths=**} {
      allow read, write;
    }
  }
}

Or use Firebase Authentication in your app.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, it's working now! I copied and pasted the "public" rules that Firebase provides but had some problems cutting and pasting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.