将多个图像存储到firebase并获取URL

时间:2016-08-23 00:22:00

标签: firebase google-cloud-storage firebase-storage

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

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

1 个答案:

答案 0 :(得分:2)

我强烈建议您同时使用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,了解如何执行此操作的实际示例。