我目前正在使用 React.js 進行我的第一個專案——建立一個在線商店。我正在使用 Firebase Storage 上傳產品圖片。之后我可以上傳它們并通過另一個請求成功獲取它們的網址。
上傳完成后是否可以正確獲取網址?我查看了有關 uploadBytes 函式的檔案,希望它回傳一個可以與影像關聯的 url,但它沒有。
我想上傳圖片并知道它的 url,以便我可以使用它的 url 作為參考將附加資訊上傳到資料庫,例如價格、數量等,然后根據該 url 從存盤和資料庫中獲取資訊.
uj5u.com熱心網友回復:
該uploadBytes()方法回傳一個 Promise,其中包含一個UploadResult具有兩個屬性的 Object:
metadata,其中包含從服務器發回的元資料;ref,產生此上傳的參考。
因此,您可以使用該ref屬性來獲取下載 URL。(或者你可以直接使用你傳遞給uploadBytes()方法的參考,因為它是一樣的!)
例如:
import { getStorage, ref, uploadBytes } from "firebase/storage";
const storage = getStorage();
const storageRef = ref(storage, 'some-child');
const bytes = new Uint8Array([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21]); // Example
uploadBytes(storageRef, bytes)
.then((uploadResult) => {
return getDownloadURL(uploadResult.ref)
})
.then((downloadURL) => {
console.log('File available at', downloadURL);
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/518686.html
標籤:Google Cloud Collective 火力基地firebase-实时数据库上传文件谷歌云存储
