Skip to content

Firestorage has different behavior when hardcode

I don’t know what happened with my code. But everytime i used this code is working

const downloadUrlThumb = async(urlPathThumbs) => {
        const gsRef = projectStorage.refFromURL('gs://myapps.appspot.com/courses/m9APF8TEUnfS6IQew591sl4kphH2/thumbs/business-3560917_640_200x200.jpg') // hardcode mode
        //const gsRef = projectStorage.refFromURL(urlPathThumbs)
        await gsRef.getDownloadURL().then(urlDownload => {
            
            console.log('URL THUMB ADALAH: ', urlDownload);
            url.value = urlDownload

        }).catch(err => {
            console.log(err.message);
        })
    }

But when i switched to urlPathThumbs it will become error 404. Error said like so

Firebase Storage: Object ‘courses/m9APF8TEUnfS6IQew591sl4kphH2/thumbs/Rectangle 68 (1)_200x200.png’ does not exist. (storage/object-not-found)

however they both are same pattern. This is how variable urlPathThumbs created in previous snippet code

var filename = file.name.replace(/(.[wd_-]+)$/i, '_200x200$1')
            console.log('filename ', filename);
            let location = 'gs://myapps.appspot.com/courses/'+user.value.uid+'/thumbs/'+filename
            console.log('full location: ', location); // --> This will print full location:  gs://myapps.appspot.com/courses/m9APF8TEUnfS6IQew591sl4kphH2/thumbs/Rectangle 68 (1)_200x200.png
            await downloadUrlThumb(location)

this file is successful uploaded and i can see it in firebase storage location: firebase storage

Please can somebody help me?

Answer

since refFromURL is expecting a URL string, spaces should not be allow. You will need to urlencode the spaces.

e.g.

console.log(encodeURI("gs://myapps.appspot.com/courses/m9APF8TEUnfS6IQew591sl4kphH2/thumbs/Rectangle 68 (1)_200x200.png"))

refFromURL refFromURL ( url : string ) : Reference Returns a reference for the given absolute URL.

Parameters url: string A URL in the form: 1) a gs:// URL, for example gs://bucket/files/image.png 2) a download URL taken from object metadata. @see firebase.storage.FullMetadata.downloadURLs