Skip to content

Commit 5f09bbf

Browse files
Edits
1 parent f61dc68 commit 5f09bbf

File tree

1 file changed

+41
-121
lines changed

1 file changed

+41
-121
lines changed

articles/storage/blobs/storage-blobs-list-javascript.md

Lines changed: 41 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ ms.author: pauljewell
88

99
ms.service: azure-blob-storage
1010
ms.topic: how-to
11-
ms.date: 08/05/2024
11+
ms.date: 10/24/2024
1212

1313
ms.devlang: javascript
1414
ms.custom: devx-track-js, devguide-js
@@ -30,42 +30,25 @@ This article shows how to list blobs using the [Azure Storage client library for
3030

3131
When you list blobs from your code, you can specify a number of options to manage how results are returned from Azure Storage. You can specify the number of results to return in each set of results, and then retrieve the subsequent sets. You can specify a prefix to return blobs whose names begin with that character or string. And you can list blobs in a flat listing structure, or hierarchically. A hierarchical listing returns blobs as though they were organized into folders.
3232

33-
To list the blobs in a storage account, create a [ContainerClient](storage-blob-javascript-get-started.md#create-a-containerclient-object) then call one of these methods:
33+
To list the blobs in a container using a flat listing, call the following method:
3434

35-
- ContainerClient.[listBlobsByHierarcy](/javascript/api/@azure/storage-blob/containerclient#@azure-storage-blob-containerclient-listblobsbyhierarchy)
36-
- ContainerClient.[listBlobsFlat](/javascript/api/@azure/storage-blob/containerclient#@azure-storage-blob-containerclient-listblobsflat)
35+
- [ContainerClient.listBlobsFlat](/javascript/api/@azure/storage-blob/containerclient#@azure-storage-blob-containerclient-listblobsflat)
3736

38-
Related functionality can be found in the following methods:
37+
To list the blobs in a container using a hierarchical listing, call the following method:
3938

40-
- BlobServiceClient.[findBlobsByTag](/javascript/api/@azure/storage-blob/blobserviceclient#@azure-storage-blob-blobserviceclient-findblobsbytags)
41-
- ContainerClient.[findBlobsByTag](/javascript/api/@azure/storage-blob/containerclient#@azure-storage-blob-containerclient-findblobsbytags)
39+
- ContainerClient.[listBlobsByHierarchy](/javascript/api/@azure/storage-blob/containerclient#@azure-storage-blob-containerclient-listblobsbyhierarchy)
4240

4341
### Manage how many results are returned
4442

4543
By default, a listing operation returns up to 5000 results at a time, but you can specify the number of results that you want each listing operation to return. The examples presented in this article show you how to return results in pages. To learn more about pagination concepts, see [Pagination with the Azure SDK for JavaScript](/azure/developer/javascript/core/use-azure-sdk#asynchronous-paging-of-results).
4644

4745
### Filter results with a prefix
4846

49-
To filter the list of blobs, specify a string for the `prefix` property in [ContainerListBlobsOptions](/javascript/api/@azure/storage-blob/containerlistblobsoptions). The prefix string can include one or more characters. Azure Storage then returns only the blobs whose names start with that prefix.
50-
51-
```javascript
52-
const listOptions = {
53-
includeCopy: false, // include metadata from previous copies
54-
includeDeleted: false, // include deleted blobs
55-
includeDeletedWithVersions: false, // include deleted blobs with versions
56-
includeLegalHold: false, // include legal hold
57-
includeMetadata: true, // include custom metadata
58-
includeSnapshots: true, // include snapshots
59-
includeTags: true, // include indexable tags
60-
includeUncommitedBlobs: false, // include uncommitted blobs
61-
includeVersions: false, // include all blob version
62-
prefix: '' // filter by blob name prefix
63-
};
64-
```
47+
To filter the list of blobs, specify a string for the `prefix` property in [ContainerListBlobsOptions](/javascript/api/@azure/storage-blob/containerlistblobsoptions). The prefix string can include one or more characters. Azure Storage then returns only the blobs whose names start with that prefix. For example, passing the prefix string `sample-` returns only blobs whose names start with `sample-`.
6548

66-
### Return metadata
49+
### Include blob metadata or other information
6750

68-
You can return blob metadata with the results by specifying the `includeMetadata` property in the [list options](/javascript/api/@azure/storage-blob/containerlistblobsoptions).
51+
To include blob metadata with the results, set the `includeMetadata` property to `true` as part of [ContainerListBlobsOptions](/javascript/api/@azure/storage-blob/containerlistblobsoptions). You can also include snapshots, tags, or versions in the results by setting the appropriate property to `true`.
6952

7053
### Flat listing versus hierarchical listing
7154

@@ -79,60 +62,31 @@ If you name your blobs using a delimiter, then you can choose to list blobs hier
7962

8063
By default, a listing operation returns blobs in a flat listing. In a flat listing, blobs are not organized by virtual directory.
8164

82-
The following example lists the blobs in the specified container using a flat listing.
83-
84-
```javascript
85-
async function listBlobsFlatWithPageMarker(containerClient) {
86-
87-
// page size - artificially low as example
88-
const maxPageSize = 2;
89-
90-
let i = 1;
91-
let marker;
92-
93-
// some options for filtering list
94-
const listOptions = {
95-
includeMetadata: false,
96-
includeSnapshots: false,
97-
includeTags: false,
98-
includeVersions: false,
99-
prefix: ''
100-
};
101-
102-
let iterator = containerClient.listBlobsFlat(listOptions).byPage({ maxPageSize });
103-
let response = (await iterator.next()).value;
104-
105-
// Prints blob names
106-
for (const blob of response.segment.blobItems) {
107-
console.log(`Flat listing: ${i++}: ${blob.name}`);
108-
}
109-
110-
// Gets next marker
111-
marker = response.continuationToken;
112-
113-
// Passing next marker as continuationToken
114-
iterator = containerClient.listBlobsFlat().byPage({
115-
continuationToken: marker,
116-
maxPageSize: maxPageSize * 2
117-
});
118-
response = (await iterator.next()).value;
119-
120-
// Prints next blob names
121-
for (const blob of response.segment.blobItems) {
122-
console.log(`Flat listing: ${i++}: ${blob.name}`);
123-
}
124-
}
125-
```
65+
The following example lists the blobs in the specified container using a flat listing. This example includes blob snapshots and blob metadata, if they exist:
66+
67+
### [JavaScript](#tab/javascript)
68+
69+
:::code language="javascript" source="~/azure-storage-snippets/blobs/howto/JavaScript/NodeJS-v12/dev-guide/list-blobs.js" id="snippet_listBlobsFlatWithPageMarker":::
70+
71+
### [TypeScript](#tab/typescript)
72+
73+
:::code language="typescript" source="~/azure-storage-snippets/blobs/howto/TypeScript/NodeJS-v12/dev-guide/src/blobs-list.ts" id="snippet_listBlobsFlatWithPageMarker" :::
74+
75+
---
12676

12777
The sample output is similar to:
12878

12979
```console
130-
Flat listing: 1: a1
131-
Flat listing: 2: a2
132-
Flat listing: 3: folder1/b1
133-
Flat listing: 4: folder1/b2
134-
Flat listing: 5: folder2/sub1/c
135-
Flat listing: 6: folder2/sub1/d
80+
Blobs flat list (by page):
81+
- Page:
82+
- a1
83+
- a2
84+
- Page:
85+
- folder1/b1
86+
- folder1/b2
87+
- Page:
88+
- folder2/sub1/c
89+
- folder2/sub1/d
13690
```
13791

13892
> [!NOTE]
@@ -142,55 +96,21 @@ Flat listing: 6: folder2/sub1/d
14296

14397
When you call a listing operation hierarchically, Azure Storage returns the virtual directories and blobs at the first level of the hierarchy.
14498

145-
To list blobs hierarchically, call the [BlobContainerClient.listBlobsByHierarchy](/javascript/api/@azure/storage-blob/containerclient#@azure-storage-blob-containerclient-listblobsbyhierarchy) method.
146-
147-
The following example lists the blobs in the specified container using a hierarchical listing, with an optional segment size specified, and writes the blob name to the console window.
148-
149-
```javascript
150-
// Recursively list virtual folders and blobs
151-
// Pass an empty string for prefixStr to list everything in the container
152-
async function listBlobHierarchical(containerClient, prefixStr) {
99+
To list blobs hierarchically, use the following method:
153100

154-
// page size - artificially low as example
155-
const maxPageSize = 2;
101+
- [BlobContainerClient.listBlobsByHierarchy](/javascript/api/@azure/storage-blob/containerclient#@azure-storage-blob-containerclient-listblobsbyhierarchy)
156102

157-
// some options for filtering list
158-
const listOptions = {
159-
includeMetadata: false,
160-
includeSnapshots: false,
161-
includeTags: false,
162-
includeVersions: false,
163-
prefix: prefixStr
164-
};
103+
The following example lists the blobs in the specified container using a hierarchical listing. In this example, the prefix parameter is initially set to an empty string to list all blobs in the container. The example then calls the listing operation recursively to traverse the virtual directory hierarchy and list blobs.
165104

166-
let delimiter = '/';
167-
let i = 1;
168-
console.log(`Folder ${delimiter}${prefixStr}`);
105+
### [JavaScript](#tab/javascript)
169106

170-
for await (const response of containerClient
171-
.listBlobsByHierarchy(delimiter, listOptions)
172-
.byPage({ maxPageSize })) {
107+
:::code language="javascript" source="~/azure-storage-snippets/blobs/howto/JavaScript/NodeJS-v12/dev-guide/list-blobs.js" id="snippet_listBlobsHierarchicalWithPageMarker":::
173108

174-
console.log(` Page ${i++}`);
175-
const segment = response.segment;
109+
### [TypeScript](#tab/typescript)
176110

177-
if (segment.blobPrefixes) {
111+
:::code language="typescript" source="~/azure-storage-snippets/blobs/howto/TypeScript/NodeJS-v12/dev-guide/src/blobs-list.ts" id="snippet_listBlobsHierarchicalWithPageMarker" :::
178112

179-
// Do something with each virtual folder
180-
for await (const prefix of segment.blobPrefixes) {
181-
// build new prefix from current virtual folder
182-
await listBlobHierarchical(containerClient, prefix.name);
183-
}
184-
}
185-
186-
for (const blob of response.segment.blobItems) {
187-
188-
// Do something with each blob
189-
console.log(`\tBlobItem: name - ${blob.name}`);
190-
}
191-
}
192-
}
193-
```
113+
---
194114

195115
The sample output is similar to:
196116

@@ -221,16 +141,16 @@ Folder /folder2/sub1/
221141

222142
To learn more about how to list blobs using the Azure Blob Storage client library for JavaScript, see the following resources.
223143

144+
### Code samples
145+
146+
- View [JavaScript](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/JavaScript/NodeJS-v12/dev-guide/list-blobs.js) and [TypeScript](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/TypeScript/NodeJS-v12/dev-guide/src/blobs-list.ts) code samples from this article (GitHub)
147+
224148
### REST API operations
225149

226150
The Azure SDK for JavaScript contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar JavaScript paradigms. The client library methods for listing blobs use the following REST API operation:
227151

228152
- [List Blobs](/rest/api/storageservices/list-blobs) (REST API)
229153

230-
### Code samples
231-
232-
- [View code samples from this article (GitHub)](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/JavaScript/NodeJS-v12/dev-guide/list-blobs.js)
233-
234154
[!INCLUDE [storage-dev-guide-resources-javascript](../../../includes/storage-dev-guides/storage-dev-guide-resources-javascript.md)]
235155

236156
### See also

0 commit comments

Comments
 (0)