使用網頁版 Cloud Storage 列出檔案

本頁面說明如何使用 Cloud Storage for Firebase SDK 列出 Cloud Storage bucket 的內容。SDK 會傳回目前 Cloud Storage 參照下的項目和物件前置字元。

使用 List API 的專案需要Cloud Storage for Firebase規則第 2 版。如果您已有 Firebase 專案,請按照安全性規則指南中的步驟操作。

list() 使用 Google Cloud Storage List API。Cloud Storage for Firebase 使用 / 做為分隔符號,可讓您模擬檔案系統語意。為了有效率地遍歷大型階層式 Cloud Storage bucket,List API 會分別傳回前置字串和項目。舉例來說,如果您上傳一個檔案 /images/uid/file1,

  • root.child('images').listAll() 會傳回 /images/uid 做為前置字串。
  • root.child('images/uid').listAll() 會將檔案以項目形式傳回。

Cloud Storage for Firebase SDK 不會傳回包含兩個連續 / 或結尾為 /. 的物件路徑。舉例來說,假設某個 bucket 含有下列物件:

  • correctPrefix/happyItem
  • wrongPrefix//sadItem
  • lonelyItem/

對這個 bucket 中的項目執行清單作業,會產生下列結果:

  • 根目錄的清單作業會將 correctPrefix、wrongPrefix 和 lonelyItem 的參照傳回為 prefixes。
  • correctPrefix/ 的清單作業會將 correctPrefix/happyItem 的參照傳回為 items。
  • wrongPrefix/ 的清單作業不會傳回任何參照,因為 wrongPrefix//sadItem 包含兩個連續的 /。
  • lonelyItem/ 的清單作業不會傳回任何參照,因為物件 lonelyItem/ 的結尾是 /。

列出所有檔案

您可以使用 listAll 擷取目錄的所有結果。由於所有結果都會緩衝至記憶體,因此最適合用於小型目錄。如果在作業期間新增或移除物件,也可能無法傳回一致的快照。

如果清單很大,請使用分頁的 list() 方法,因為 listAll() 會在記憶體中緩衝所有結果。

以下範例示範 listAll。

Web

import { getStorage, ref, listAll } from "firebase/storage";

const storage = getStorage();

// Create a reference under which you want to list
const listRef = ref(storage, 'files/uid');

// Find all the prefixes and items.
listAll(listRef)
  .then((res) => {
    res.prefixes.forEach((folderRef) => {
      // All the prefixes under listRef.
      // You may call listAll() recursively on them.
    });
    res.items.forEach((itemRef) => {
      // All the items under listRef.
    });
  }).catch((error) => {
    // Uh-oh, an error occurred!
  });

Web

// Create a reference under which you want to list
var listRef = storageRef.child('files/uid');

// Find all the prefixes and items.
listRef.listAll()
  .then((res) => {
    res.prefixes.forEach((folderRef) => {
      // All the prefixes under listRef.
      // You may call listAll() recursively on them.
    });
    res.items.forEach((itemRef) => {
      // All the items under listRef.
    });
  }).catch((error) => {
    // Uh-oh, an error occurred!
  });

將清單結果分頁

list() API 會限制傳回的結果數量。list() 提供一致的網頁瀏覽,並公開 pageToken,可控制何時擷取其他結果。

pageToken 會對先前結果中傳回的最後一個項目的路徑和版本進行編碼。在後續使用 pageToken 的要求中,系統會顯示 pageToken 之後的項目。

以下範例示範如何使用 async/await 將結果分頁。

Web

import { getStorage, ref, list } from "firebase/storage";

async function pageTokenExample(){
  // Create a reference under which you want to list
  const storage = getStorage();
  const listRef = ref(storage, 'files/uid');

  // Fetch the first page of 100.
  const firstPage = await list(listRef, { maxResults: 100 });

  // Use the result.
  // processItems(firstPage.items)
  // processPrefixes(firstPage.prefixes)

  // Fetch the second page if there are more elements.
  if (firstPage.nextPageToken) {
    const secondPage = await list(listRef, {
      maxResults: 100,
      pageToken: firstPage.nextPageToken,
    });
    // processItems(secondPage.items)
    // processPrefixes(secondPage.prefixes)
  }
}

Web

async function pageTokenExample(){
  // Create a reference under which you want to list
  var listRef = storageRef.child('files/uid');

  // Fetch the first page of 100.
  var firstPage = await listRef.list({ maxResults: 100});

  // Use the result.
  // processItems(firstPage.items)
  // processPrefixes(firstPage.prefixes)

  // Fetch the second page if there are more elements.
  if (firstPage.nextPageToken) {
    var secondPage = await listRef.list({
      maxResults: 100,
      pageToken: firstPage.nextPageToken,
    });
    // processItems(secondPage.items)
    // processPrefixes(secondPage.prefixes)
  }
}

錯誤處理

如果您尚未將安全性規則升級至第 2 版,list() 和 listAll() 會傳回遭拒的 Promise。如果看到這則錯誤訊息,請升級安全性規則:

Listing objects in a bucket is disallowed for rules_version = "1".
Please update storage security rules to rules_version = "2" to use list.

其他可能發生的錯誤,代表使用者沒有適當權限。如要進一步瞭解錯誤,請參閱「處理錯誤」。