使用 JavaScript 列出 Blob 容器
當您從程式碼列出 Azure 儲存體中的容器時,即可指定數個選項來管理從 Azure 儲存體傳回結果的方式。 本文說明如何使用適用於 JavaScript 的 Azure 儲存體用戶端程式庫來列出容器。
必要條件
- 本文中的範例假設您已設定專案,以搭配使用適用於 JavaScript 的 Azure Blob 儲存體用戶端程式庫。 若要了解如何設定專案,包括套件安裝、匯入模組,以及建立授權的用戶端物件來處理資料資源,請參閱開始使用 Azure Blob 儲存體和 JavaScript。
- 授權機制必須具有列出 Blob 容器的權限。 若要深入了解,請參閱下列 REST API 作業的授權指引:
關於容器列出選項
若要列出您的儲存體帳戶中的容器,請建立 BlobServiceClient 物件,然後呼叫下列方法:
- BlobServiceClient.listContainers
列出具有選擇性前置詞的容器
根據預設,清單作業一次最多會傳回 5000 個結果。
BlobServiceClient.listContainers 會傳回 ContainerItem 物件的清單。 使用 containerItem.name 來建立 ContainerClient,以取得更完整的 ContainerProperties 物件。
async function listContainers(blobServiceClient, containerNamePrefix) {
const options = {
includeDeleted: false,
includeMetadata: true,
includeSystem: true,
prefix: containerNamePrefix
}
for await (const containerItem of blobServiceClient.listContainers(options)) {
// ContainerItem
console.log(`For-await list: ${containerItem.name}`);
// ContainerClient
const containerClient = blobServiceClient.getContainerClient(containerItem.name);
// ... do something with container
}
}
列出具有分頁的容器
若要傳回一組較小的結果,請為要傳回的結果頁面大小提供非零的值。
如果您的儲存體帳戶包含超過 5,000 個容器,或如果已指定頁面大小,如此清單作業即會傳回儲存體帳戶中的容器子集,而 Azure 儲存體會傳回接續權杖與容器清單。 接續權杖是不透明的值,其可用來從 Azure 儲存體中擷取下一組結果。
在程式碼中檢查接續權杖的值,以判斷該值是否為空白。 當接續權杖為空白時,結果集就會完成。 如果接續權杖不是空白,則會再次呼叫清單方法、傳遞接續權杖來擷取下一組結果,直到接續權杖成為空白為止。
async function listContainersWithPagingMarker(blobServiceClient) {
// add prefix to filter list
const containerNamePrefix = '';
// page size
const maxPageSize = 2;
const options = {
includeDeleted: false,
includeMetadata: true,
includeSystem: true,
prefix: containerNamePrefix
}
let i = 1;
let marker;
let iterator = blobServiceClient.listContainers(options).byPage({ maxPageSize });
let response = (await iterator.next()).value;
// Prints 2 container names
if (response.containerItems) {
for (const container of response.containerItems) {
console.log(`IteratorPaged: Container ${i++}: ${container.name}`);
}
}
// Gets next marker
marker = response.continuationToken;
// Passing next marker as continuationToken
iterator = blobServiceClient.listContainers().byPage({ continuationToken: marker, maxPageSize: maxPageSize * 2 });
response = (await iterator.next()).value;
// Print next 4 container names
if (response.containerItems) {
for (const container of response.containerItems) {
console.log(`Container ${i++}: ${container.name}`);
}
}
}
在 listContainers 方法中使用 options 參數,以篩選具有前置詞的結果。
使用前置詞篩選結果
若要篩選容器清單,請為 prefix 屬性指定字串。 前置詞字串可包含一或多個字元。 Azure 儲存體接著只會傳回名稱開頭為該前置詞的容器。
async function listContainers(blobServiceClient, containerNamePrefix) {
const options = {
includeDeleted: false,
includeMetadata: true,
includeSystem: true,
// filter with prefix
prefix: containerNamePrefix
}
for await (const containerItem of blobServiceClient.listContainers(options)) {
// do something with containerItem
}
}
在結果中包含中繼資料
若要傳回具有結果的容器中繼資料,請指定 BlobContainerTraits 列舉的中繼資料值。 Azure 儲存體包含中繼資料與每個傳回的容器,因此您不需要以個別作業的形式擷取容器中繼資料。
async function listContainers(blobServiceClient, containerNamePrefix) {
const options = {
includeDeleted: false,
includeSystem: true,
prefix: containerNamePrefix,
// include metadata
includeMetadata: true,
}
for await (const containerItem of blobServiceClient.listContainers(options)) {
// do something with containerItem
}
}
資源
若要深入了解如何使用適用於 JavaScript 的 Azure Blob 儲存體用戶端程式庫來列出容器,請參閱下列資源。
REST API 操作
適用於 JavaScript 的 Azure SDK 包含建置在 Azure REST API 之上的程式庫,可讓您透過熟悉的 JavaScript 範例與 REST API 作業進行互動。 用來列出容器的用戶端程式庫方法會使用下列 REST API 作業:
- 列出容器 (REST API)
程式碼範例
- 檢視本文中的程式碼範例 (GitHub) (英文)
用戶端程式庫資源
另請參閱
意見反應
https://aka.ms/ContentUserFeedback。
即將登場:在 2024 年,我們將逐步淘汰 GitHub 問題作為內容的意見反應機制,並將它取代為新的意見反應系統。 如需詳細資訊,請參閱:提交並檢視相關的意見反應