/
listContainers.ts
52 lines (43 loc) · 1.59 KB
/
listContainers.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/**
* @summary list containers in an account, showing options for paging, resuming paging, etc.
*/
import { BlobServiceClient, StorageSharedKeyCredential } from "@azure/storage-blob";
// Load the .env file if it exists
import * as dotenv from "dotenv";
dotenv.config();
async function main() {
// Enter your storage account name and shared key
const account = process.env.ACCOUNT_NAME || "";
const accountKey = process.env.ACCOUNT_KEY || "";
// Use StorageSharedKeyCredential with storage account and account key
// StorageSharedKeyCredential is only available in Node.js runtime, not in browsers
const sharedKeyCredential = new StorageSharedKeyCredential(account, accountKey);
// List containers
const blobServiceClient = new BlobServiceClient(
`https://${account}.blob.core.windows.net`,
sharedKeyCredential
);
// Iterate over all containers in the account
console.log("Containers:");
for await (const container of blobServiceClient.listContainers()) {
console.log(`- ${container.name}`);
}
// The iterator also supports iteration by page with a configurable (and optional) `maxPageSize` setting.
console.log("Containers (by page):");
for await (const response of blobServiceClient.listContainers().byPage({
maxPageSize: 20
})) {
console.log("- Page:");
if (response.containerItems) {
for (const container of response.containerItems) {
console.log(` - ${container.name}`);
}
}
}
}
main().catch((error) => {
console.error(error);
process.exit(1);
});