将多个图像存储到 Firebase 并获取 url

Posted

技术标签:

【中文标题】将多个图像存储到 Firebase 并获取 url【英文标题】:Storing multiple images into firebase and getting urls 【发布时间】:2016-08-23 00:21:59 【问题描述】:

我有 100 张图片要存储在 Firebase 存储中,但我还需要从中提取网址。有自动的方法吗?

如果没有,是否有更好的服务提供商允许上传大量图片并自动提取网址??

【问题讨论】:

Firebase 存储有一个 API,您可以使用该 API 上传图像,然后依次获取每个图像的下载 URL。见firebase.google.com/docs/storage 【参考方案1】:

我强烈建议同时使用 Firebase 存储和 Firebase 实时数据库来完成此任务。下面是一些显示这些部分如何交互的代码(Swift):

共享:

// Firebase services
var database: FIRDatabase!
var storage: FIRStorage!
...
// Initialize Database, Auth, Storage
database = FIRDatabase.database()
storage = FIRStorage.storage()

上传:

let fileData = NSData() // get data...
let storageRef = storage.reference().child("myFiles/myFile")
storageRef.putData(fileData).observeStatus(.Success)  (snapshot) in
  // When the image has successfully uploaded, we get it's download URL
  // This "extracts" the URL, which you can then save to the RT DB
  let downloadURL = snapshot.metadata?.downloadURL()?.absoluteString
  // Write the download URL to the Realtime Database
  let dbRef = database.reference().child("myFiles/myFile")
  dbRef.setValue(downloadURL)

下载:

let dbRef = database.reference().child("myFiles")
dbRef.observeEventType(.ChildAdded, withBlock:  (snapshot) in
  // Get download URL from snapshot
  let downloadURL = snapshot.value() as! String
  // Create a storage reference from the URL
  let storageRef = storage.referenceFromURL(downloadURL)
  // Download the data, assuming a max size of 1MB (you can change this as necessary)
  storageRef.dataWithMaxSize(1 * 1024 * 1024)  (data, error) -> Void in
    // Do something with downloaded data...
  )
)

有关详细信息,请参阅Zero to App: Develop with Firebase,它是associated source code,了解如何执行此操作的实际示例。

【讨论】:

以上是关于将多个图像存储到 Firebase 并获取 url的主要内容,如果未能解决你的问题,请参考以下文章

如何将多个图像上传到Firebase存储并返回多个downloadURL

将多个图像的异步图像上传到firebase存储

成功将图像上传到 Firebase 存储后,Firebase 获取下载 URL

Firebase存储将图像成功上传到Firebase存储后获得下载URL

如何按选择的顺序将多个文件上传到 Firebase 存储?

从多个文件上传 Firebase 存储中获取下载 url