Firebase Storage: An unknown error occurred, please check the error payload for server response
Asked Answered
H

8

17

I am trying to create a Vue Composable that uploads a file to Firebase Storage.

To do this I am using the modular Firebase 9 version.

But my current code does not upload anything, and instead returns this error: FirebaseError: Firebase Storage: An unknown error occurred, please check the error payload for server response. (storage/unknown)

Since this error is already coming from my console.log("ERROR", err); I'm not sure where else to look for a solution.

My code is implemented using TypeScript, incase that matters.

import { projectStorage } from "@/firebase/config";
import { ref, watchEffect } from "vue";
import {
  ref as storageRef,
  uploadBytesResumable,
  UploadTaskSnapshot,
  UploadTask,
  getDownloadURL,
  StorageError,
} from "firebase/storage";

const useStorage: any = (file: File) => {
  const error = ref<StorageError | null>(null);
  const url = ref<string | null>(null);
  const progress = ref<number | null>(null);
  watchEffect(() => {
    // references
    const storageReference = storageRef(projectStorage, "images/" + file.name);
    // upload file
    const uploadTask: UploadTask = uploadBytesResumable(storageReference, file);
    // update progess bar as file uploads
    uploadTask.on(
      "state_changed",
      (snapshot: UploadTaskSnapshot) => {
        console.log("SNAPSHOT", snapshot);
      },
      (err) => {
        error.value = err;
        console.log("ERROR", err);
      },
      async () => {
        // get download URL & make firestore doc
        const downloadUrl = await getDownloadURL(storageReference);
        url.value = downloadUrl;
        console.log("DOWNLOADURL", downloadUrl);
      }
    );
  });
  return { progress, url, error };
};
export default useStorage;
Heiney answered 21/11, 2021 at 6:54 Comment(2)
firebaser here If this was a project that you created in the past few days, you may have been affected by a bug in our project creation. If so, that problem has been fixed so that new projects won't be affected anymore. To fix your existing project, have a look at the steps here : stackoverflow.com/a/70060240Arthritis
Also posted on reddit.com/r/Firebase/comments/qz6u84/…Arthritis
H
14

The console error is not sufficent. It does not give enough information.

When viewing the console error you need to click the other red POST 400 error shown in the console. This will take you to the Network tab. From there scroll down and click the offending red error. This should finally show you a more helpful error message that reads something like this:

Permission denied. Please enable Firebase Storage for your bucket by visiting the Storage tab in the Firebase Console and ensure that you have sufficient permission to properly provision resources.

This may lead you to think that it's your Firebase Storage rules to blame. And you should double check those rules before continuing, but the more likely problem is that you are missing an esoteric [email protected] permission inside the Google Cloud Console.

To fix that take these steps:

  1. Go to https://console.cloud.google.com
  2. Select your project in the top blue bar (you will probably need to switch to the "all" tab to see your Firebase projects)
  3. Scroll down the left menu and select "Cloud Storage"
  4. Select all your buckets then click "PERMISSIONS" in the top right hand corner
  5. click "ADD PRINCIPAL"
  6. Add "[email protected]" to the New Principle box and give it the role of "Storage Admin" and save it

That should fix it!

Heiney answered 22/11, 2021 at 1:8 Comment(0)
O
38

First go to Storage, Rules tab and change allow read, write: if false; to true; as shown bellow.

rules_version = '2';
service firebase.storage {
    match /b/{bucket}/o {
        match /{allPaths=**} {
            allow read, write: if true;
    }
  }
}
Olsen answered 19/2, 2022 at 12:5 Comment(2)
It's about the storage security, you can read more on : firebase.google.com/docs/rules/get-started?authuser=0&hl=enOlsen
If you got error code : 404, so check if you imported its module (firebase/storage).Olsen
H
14

The console error is not sufficent. It does not give enough information.

When viewing the console error you need to click the other red POST 400 error shown in the console. This will take you to the Network tab. From there scroll down and click the offending red error. This should finally show you a more helpful error message that reads something like this:

Permission denied. Please enable Firebase Storage for your bucket by visiting the Storage tab in the Firebase Console and ensure that you have sufficient permission to properly provision resources.

This may lead you to think that it's your Firebase Storage rules to blame. And you should double check those rules before continuing, but the more likely problem is that you are missing an esoteric [email protected] permission inside the Google Cloud Console.

To fix that take these steps:

  1. Go to https://console.cloud.google.com
  2. Select your project in the top blue bar (you will probably need to switch to the "all" tab to see your Firebase projects)
  3. Scroll down the left menu and select "Cloud Storage"
  4. Select all your buckets then click "PERMISSIONS" in the top right hand corner
  5. click "ADD PRINCIPAL"
  6. Add "[email protected]" to the New Principle box and give it the role of "Storage Admin" and save it

That should fix it!

Heiney answered 22/11, 2021 at 1:8 Comment(0)
C
2

Go to your firebase project console, go to Storage, then go to Rules, copy & paste this:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write: if true;
    }
    match /users/{userId}/{allPaths=**} {
      allow read: if true;
      allow write: if request.auth.uid == userId;
    }
  }
}
Canada answered 26/6, 2022 at 21:58 Comment(1)
Does the extra lines you added compared to the other answer posted above have any value to the question, or can you explain why you chose to add those in?Poll
E
2

I face the same issue. The problem from my side is that my image size is large. I got no errors after experimenting with small-sized images.

Egbert answered 26/12, 2023 at 18:24 Comment(0)
B
1

The error code 400 shows in the debug console with a message that says you have to update your rules_version = '2'. Doing this solves the problem.

Broadnax answered 9/7, 2022 at 13:9 Comment(0)
J
1

Erase the device content and settings, then build. This worked for me. It's due to the simulator.

Clear Settings Option Location

Ja answered 27/9, 2023 at 13:41 Comment(1)
I had this error in iPhone 15 iOS 17.4 when using Firebase Storage through React Native. Erasing Device Content and Settings fixes the issue.Almonry
C
1

Interesting but I got this error many times when using fb storage on ios simulator. Only one time I was able to upload a file successfully. So I thought this must be related to something with simulator and the storage library.

I immediately tested it with a real device and never got the error again.

I hope this experience will help someone else.

I'm thinking to open an issue on firebase github repo about this problem regards to ios simulators.

Chitkara answered 17/2, 2024 at 16:41 Comment(0)
M
0

If you only want to allow logged in users to write to Firestore you can use this:

rules_version = '2';
service cloud.firestore {
  match /databases/{database}/documents {
    match /{document=**} {
      allow read, write: if request.auth != null;
    }
  }
}
Maryalice answered 11/9, 2022 at 7:45 Comment(1)
You said Firebase Storage, code is for firestore thoughAcetylene

© 2022 - 2025 — McMap. All rights reserved.