skip to Main Content

I have a folder inside my firebase storage named ‘banners’
Using javascript I have successfully uploaded it to the folder.
The problem is that the URL provided by the getDownloadURL() is giving me an error
‘Invalid HTTP method/URL pair’

The link below is the one provided by the getDownloadURL() method, which does not work.
https://firebasestorage.googleapis.com/v0/b/sample.appspot.com/o/banners/imageid123.jpg?alt=media&token=sampletoken

And this is the link that is working
https://firebasestorage.googleapis.com/v0/b/sample.appspot.com/o/banners%2Fimageid123.jpg?alt=media&token=sampletoken

I noticed that it works when there is a %2F in the link, but I don’t know how it will generate a link in that format.

I hope you can help me. Thank you

Btw, this is my code.

    // Initialize Firebase
    firebase.initializeApp(firebaseConfig);
    firebase.analytics();
    
    btn.addEventListener("click", function (e) {
      e.preventDefault()
      const storage = firebase.storage();
      const storageRef = storage.ref('/banners/');
    
    
    
      const productImages = document.getElementsByClassName("drop-zone__input");
    
      if (document.querySelector(".drop-zone__input").value !== "") {
    
        for (let i = 0; i < productImages.length; i++) {
          setImageNameAndMetadata(
            productImages[i].files[0],
            productImages[i].getAttribute("id")
          );
        }
    
        function setImageNameAndMetadata(image, imageId) {
          result = "";
          for (var i = 0; i < 5; i++) {
            result += characters.charAt(
              Math.floor(Math.random() * charactersLength)
            );
          }
          var imageName = result + image.name;
          var imageMetadata = {
            contentType: image.type,
          };
          uploadImages(image, imageName, imageMetadata, imageId);
        }
        function uploadImages(image, imageName, imageMetadata, imageId) {
          var uploadImage = storageRef.child(imageName).put(image, imageMetadata);
          uploadImage
            .then((snapshot) => snapshot.ref.getDownloadURL())
            .then((url) => {
              console.log(url);
    
              document.querySelector(`.image-box #${imageId}_url`).value = url;
            });
        }

  }

2

Answers


  1. Firebase storage stores all files in the root of each bucket.
    Files inside folders are just normal files having prefixes in their names. For example:
    Banners/123.jpg is a single file in the root with that string as its whole name.

    This is the reason that you need to encode all inner slashes with %2F in your download URLs.
    Whether you use the API to get them or generate them manually for public files.

    Login or Signup to reply.
  2. Without looking into your code how are we gonna suggest?

    However, Check the method Here.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search