-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
listShares.ts
81 lines (65 loc) · 2.65 KB
/
listShares.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/**
* @summary list shares in an account, showing options for paging, resuming paging, etc.
*/
import { ShareServiceClient, StorageSharedKeyCredential } from "@azure/storage-file-share";
// Load the .env file if it exists
import * as dotenv from "dotenv";
dotenv.config();
export 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);
const serviceClient = new ShareServiceClient(
`https://${account}.file.core.windows.net`,
sharedKeyCredential
);
console.log("Shares:");
for await (const share of serviceClient.listShares()) {
console.log(`- ${share.name}`);
}
// The iterator also supports iteration by page with a configurable (and optional) `maxPageSize`. Here, we use a small
// `maxPageSize` so that the effect of paging is noticeable with a small number of shares
console.log("Shares (by page):");
const maxPageSize = 5;
let pageNumber = 1;
for await (const page of serviceClient.listShares().byPage({ maxPageSize })) {
console.log(`- Page ${pageNumber++}:`);
if (page.shareItems) {
for (const share of page.shareItems) {
console.log(` - ${share.name}`);
}
}
}
// The paged iterator also supports resuming from a continuation token. In the following example, we use the
// continuation token from the first iteration to resume iteration at the second page.
console.log("Shares (by page, starting from the second page of results):");
const iter = serviceClient.listShares().byPage({ maxPageSize });
const result = await iter.next();
if (result.done) {
throw new Error("Expected at least one page of results.");
}
// The continuation token is an optional property of the page.
const continuationToken = result.value.continuationToken;
if (!continuationToken) {
throw new Error("Expected a continuation token from the service, but one was not returned.");
}
const resumed = serviceClient.listShares().byPage({ maxPageSize, continuationToken });
pageNumber = 2;
for await (const page of resumed) {
console.log(`- Page ${pageNumber++}:`);
if (page.shareItems) {
for (const share of page.shareItems) {
console.log(` - ${share.name}`);
}
}
}
}
main().catch((error) => {
console.error(error);
process.exit(1);
});