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

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

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

【问题讨论】:

标签: firebase google-cloud-storage firebase-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,了解如何执行此操作的实际示例。

【讨论】:

    猜你喜欢
    • 2021-04-07
    • 1970-01-01
    • 2021-03-14
    • 2016-11-20
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 2019-03-24
    • 2021-02-27
    相关资源
    最近更新 更多