-
Notifications
You must be signed in to change notification settings - Fork 47
/
containers-list.ts
109 lines (92 loc) · 2.93 KB
/
containers-list.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// index.js
import {
BlobServiceClient,
ContainerClient,
ServiceListContainersOptions
} from '@azure/storage-blob';
import * as dotenv from 'dotenv';
dotenv.config();
// Get BlobServiceClient
import { getBlobServiceClientFromDefaultAzureCredential } from './auth-get-client';
const blobServiceClient: BlobServiceClient =
getBlobServiceClientFromDefaultAzureCredential();
// <snippet_listContainers>
// return up to 5000 containers
async function listContainers(
blobServiceClient: BlobServiceClient,
containerNamePrefix: string
): Promise<void> {
const options: ServiceListContainersOptions = {
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: ContainerClient =
blobServiceClient.getContainerClient(containerItem.name);
// ... do something with container
// containerClient.listBlobsFlat({ includeMetadata: true,
// includeSnapshots: false,
// includeTags: true,
// includeVersions: false,
// prefix: ''});
}
}
// </snippet_listContainers>
// <snippet_listContainersWithPagingMarker>
async function listContainersWithPagingMarker(
blobServiceClient: BlobServiceClient
) {
// add prefix to filter list
const containerNamePrefix = '';
// page size
const maxPageSize = 2;
const options: ServiceListContainersOptions = {
includeDeleted: false,
includeMetadata: true,
includeSystem: true,
prefix: containerNamePrefix
};
let i = 1;
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
const 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}`);
}
}
}
// </snippet_listContainersWithPagingMarker>
// assumes containers are already in storage
async function main(blobServiceClient: BlobServiceClient) {
const containerNamePrefix = '';
await listContainers(blobServiceClient, containerNamePrefix);
await listContainersWithPagingMarker(blobServiceClient);
}
main(blobServiceClient)
.then(() => console.log(`success`))
.catch((err: unknown) => {
if (err instanceof Error) {
console.log(err.message);
}
});