硬编码时,Firestorage 具有不同的行为

Firestorage has different behavior when hardcode

我不知道我的代码发生了什么。但每次我使用此代码时都有效

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);
        })
    }

但是当我切换到urlPathThumbs时它会变成错误404。错误是这样说的

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

但是它们都是相同的模式。这就是在前面的代码片段

中创建变量 urlPathThumbs 的方式
var filename = file.name.replace(/(\.[\w\d_-]+)$/i, '_200x200')
            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)

此文件已成功上传,我可以在 firebase 存储位置看到它:

有人能帮帮我吗?

因为 refFromURL 需要一个 URL 字符串,所以不允许有空格。您需要对空格进行 urlencode。

例如

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