本頁面由 Cloud Translation API 翻譯而成。
Switch to English

在Android上創建存儲參考

您的文件存儲在Google Cloud Storage存儲桶中。此存儲桶中的文件以分層結構顯示,就像本地硬盤上的文件系統或Firebase Realtime數據庫中的數據一樣。通過創建對文件的引用,您的應用程序可以訪問該文件。這些引用隨後可用於上載或下載數據,獲取或更新元數據或刪除文件。引用可以指向特定文件,也可以指向層次結構中的更高級別的節點。

如果您使用過Firebase Realtime數據庫 ,則這些路徑對您來說應該非常熟悉。但是,您的文件數據存儲在Google Cloud Storage中, 而不是實時數據庫中。

創建參考

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

使用FirebaseStorage單例實例並調用其getReference()方法來創建引用。

爪哇

// Create a storage reference from our app
StorageReference storageRef = storage.getReference();

Kotlin + KTX

// Create a storage reference from our app
var storageRef = storage.reference

您可以通過在現有參考上使用child()方法來創建對樹中較低位置的參考,例如'images/space.jpg'

爪哇

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

// Child references can also take paths
// spaceRef now points to "images/space.jpg
// imagesRef still points to "images"
StorageReference spaceRef = storageRef.child("images/space.jpg");

Kotlin + KTX

// Create a child reference
// imagesRef now points to "images"
var imagesRef: StorageReference? = storageRef.child("images")

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

您還可以使用getParent()getRoot()方法在我們的文件層次結構中向上導航。 getParent()向上導航,而getRoot()導航到頂部。

爪哇

// getParent allows us to move our reference to a parent node
// imagesRef now points to 'images'
imagesRef = spaceRef.getParent();

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

Kotlin + KTX

// parent allows us to move our reference to a parent node
// 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
val rootRef = spaceRef.root

child()getParent()getRoot()可以多次鏈接在一起,因為每個返回一個引用。但是調用getRoot().getParent()返回null

爪哇

// References can be chained together multiple times
// earthRef points to 'images/earth.jpg'
StorageReference earthRef = spaceRef.getParent().child("earth.jpg");

// nullRef is null, since the parent of root is null
StorageReference nullRef = spaceRef.getRoot().getParent();

Kotlin + KTX

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

// nullRef is null, since the parent of root is null
val nullRef = spaceRef.root.parent

參考屬性

您可以使用getPath()getName()getBucket()方法檢查引用,以更好地理解它們指向的文件。這些方法獲取文件的完整路徑,名稱和存儲桶。

爪哇

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

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

// Reference's bucket is the name of the storage bucket that the files are stored in
spaceRef.getBucket();

Kotlin + KTX

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

// 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 that the files are stored in
spaceRef.bucket

參考文獻的局限性

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

  1. 使用UTF-8編碼時,reference.fullPath的總長度必須在1到1024個字節之間。
  2. 沒有回車符或換行符。
  3. 避免使用#[]*? ,因為它們不能與Firebase Realtime Databasegsutil等其他工具很好地配合使用。

完整的例子

爪哇

// Points to the root reference
storageRef = storage.getReference();

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

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

// File path is "images/space.jpg"
String path = spaceRef.getPath();

// File name is "space.jpg"
String name = spaceRef.getName();

// Points to "images"
imagesRef = spaceRef.getParent();

Kotlin + KTX

// Points to the root reference
storageRef = storage.reference

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

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

// File path is "images/space.jpg"
val path = spaceRef.path

// File name is "space.jpg"
val name = spaceRef.name

// Points to "images"
imagesRef = spaceRef.parent

接下來,讓我們學習如何將文件上傳到Cloud Storage。