在 iOS 上創建 Cloud Storage 參考

您的文件存儲在雲存儲桶。此存儲分區中的文件以分層結構呈現,就像本地硬盤上的文件系統或 Firebase 實時數據庫中的數據一樣。通過創建對文件的引用,您的應用程序可以訪問它。然後可以使用這些引用來上傳或下載數據、獲取或更新元數據或刪除文件。引用可以指向特定文件或層次結構中的更高級別節點。

如果您已經使用了火力地堡實時數據庫,這些路徑應該在你看來很熟悉。然而,你的文件數據存儲在雲存儲,而不是在實時數據庫。

創建參考

創建引用以上傳、下載或刪除文件,或者獲取或更新其元數據。可以將引用視為指向雲中文件的指針。引用是輕量級的,因此您可以根據需要創建任意數量的引用。它們也可重複用於多項操作。

參考使用創建的FirebaseStorage服務,並調用它的reference方法。

迅速

// Get a reference to the storage service using the default Firebase App
let storage = Storage.storage()

// Create a storage reference from our storage service
let storageRef = storage.reference()
    

目標-C

// Get a reference to the storage service using the default Firebase App
FIRStorage *storage = [FIRStorage storage];

// Create a storage reference from our storage service
FIRStorageReference *storageRef = [storage reference];
    

您可以在樹中創建一個位置的參考下,說'images/space.jpg'通過child的方法上存在的參考。

迅速

// Create a child reference
// imagesRef now points to "images"
let imagesRef = storageRef.child("images")

// Child references can also take paths delimited by '/'
// spaceRef now points to "images/space.jpg"
// imagesRef still points to "images"
var spaceRef = storageRef.child("images/space.jpg")

// This is equivalent to creating the full reference
let storagePath = "\(your_firebase_storage_bucket)/images/space.jpg"
spaceRef = storage.reference(forURL: storagePath)
    

目標-C

// Create a child reference
// imagesRef now points to "images"
FIRStorageReference *imagesRef = [storageRef child:@"images"];

// Child references can also take paths delimited by '/'
// spaceRef now points to "images/space.jpg"
// imagesRef still points to "images"
FIRStorageReference *spaceRef = [storageRef child:@"images/space.jpg"];

// This is equivalent to creating the full reference
spaceRef = [storage referenceForURL:@"gs://<your-firebase-storage-bucket>/images/space.jpg"];
     

您也可以使用parentroot方法,我們的文件層次結構進行導航了。 parent向上導航一個級別,而root瀏覽所有的方式達到頂峰。

迅速

// Parent allows us to move to the parent of a reference
// imagesRef now points to 'images'
let imagesRef = spaceRef.parent()

// Root allows us to move all the way back to the top of our bucket
// rootRef now points to the root
let rootRef = spaceRef.root()
    

目標-C

// Parent allows us to move to the parent of a reference
// imagesRef now points to 'images'
imagesRef = [spaceRef parent];

// Root allows us to move all the way back to the top of our bucket
// rootRef now points to the root
FIRStorageReference *rootRef = [spaceRef root];
    

childparent ,和root可以多次被鏈接在一起,因為每個返回的參考。唯一的例外是parentroot ,這是nil

迅速

// References can be chained together multiple times
// earthRef points to "images/earth.jpg"
let earthRef = spaceRef.parent()?.child("earth.jpg")

// nilRef is nil, since the parent of root is nil
let nilRef = spaceRef.root().parent()
    

目標-C

// References can be chained together multiple times
// earthRef points to "images/earth.jpg"
FIRStorageReference *earthRef = [[spaceRef parent] child:@"earth.jpg"];

// nilRef is nil, since the parent of root is nil
FIRStorageReference *nilRef = [[spaceRef root] parent];
    

參考屬性

您可以檢查引用,以更好地它們指向的文件,了解使用fullPathnamebucket性。這些屬性獲取文件的完整路徑、名稱和存儲桶。

迅速

// Reference's path is: "images/space.jpg"
// This is analogous to a file path on disk
spaceRef.fullPath

// Reference's name is the last segment of the full path: "space.jpg"
// This is analogous to the file name
spaceRef.name

// Reference's bucket is the name of the storage bucket where files are stored
spaceRef.bucket
    

目標-C

// Reference's path is: "images/space.jpg"
// This is analogous to a file path on disk
spaceRef.fullPath;

// Reference's name is the last segment of the full path: "space.jpg"
// This is analogous to the file name
spaceRef.name;

// Reference's bucket is the name of the storage bucket where files are stored
spaceRef.bucket;
    

參考文獻的限制

引用路徑和名稱可以包含任何有效 Unicode 字符序列,但會施加某些限制,包括:

  1. UTF-8 編碼時,reference.fullPath 的總長度必須在 1 到 1024 字節之間。
  2. 沒有回車符或換行符。
  3. 避免使用#[]*? ,因為這些不與其他工具,如很好地工作火力地堡實時數據庫gsutil會

完整示例

迅速

// Points to the root reference
let storageRef = Storage.storage().reference()

// Points to "images"
let imagesRef = storageRef.child("images")

// Points to "images/space.jpg"
// Note that you can use variables to create child values
let fileName = "space.jpg"
let spaceRef = imagesRef.child(fileName)

// File path is "images/space.jpg"
let path = spaceRef.fullPath;

// File name is "space.jpg"
let name = spaceRef.name;

// Points to "images"
let images = spaceRef.parent()
    

目標-C

// Points to the root reference
FIRStorageReference *storageRef = [[FIRStorage storage] reference];

// Points to "images"
FIRStorageReference *imagesRef = [storageRef child:@"images"];

// Points to "images/space.jpg"
// Note that you can use variables to create child values
NSString *fileName = @"space.jpg";
FIRStorageReference *spaceRef = [imagesRef child:fileName];

// File path is "images/space.jpg"
NSString *path = spaceRef.fullPath;

// File name is "space.jpg"
NSString *name = spaceRef.name;

// Points to "images"
imagesRef = [spaceRef parent];
    

接下來,讓我們學習如何上傳文件到雲存儲。