您首先需要创建一个 Cloud Storage 引用。有多种方法:
带有初始文件路径和名称
var storage = firebase.storage();
var pathReference = storage.ref('images/stars.jpg');
或来自 Google Cloud Storage URI
var gsReference = storage.refFromURL('gs://bucket/images/stars.jpg')
或来自 HTTPS URL
var httpsReference = storage.refFromURL('https://firebasestorage.googleapis.com/b/bucket/o/images%20stars.jpg');
获得引用后,您可以通过在存储引用上调用 getDownloadURL() 方法来获取文件的下载 URL。
storageRef.child('images/stars.jpg').getDownloadURL().then(function(url) {
// `url` is the download URL for 'images/stars.jpg'
// This can be downloaded directly:
var xhr = new XMLHttpRequest();
xhr.responseType = 'blob';
xhr.onload = function(event) {
var blob = xhr.response;
};
xhr.open('GET', url);
xhr.send();
// Or inserted into an <img> element:
var img = document.getElementById('myimg');
img.src = url;
}).catch(function(error) {
// Handle any errors
});
更多详情here