-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
anonymousAuth.ts
48 lines (38 loc) · 1.37 KB
/
anonymousAuth.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
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
/**
* @summary authenticate anonymously using a SAS-encoded URL
*/
import { ShareServiceClient, AnonymousCredential } 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 SAS
const account = process.env.ACCOUNT_NAME || "";
const accountSas = process.env.ACCOUNT_SAS || "";
// Use AnonymousCredential when url already includes a SAS signature
const anonymousCredential = new AnonymousCredential();
// List shares
const serviceClient = new ShareServiceClient(
// When using AnonymousCredential, following url should include a valid SAS
`https://${account}.file.core.windows.net${accountSas}`,
anonymousCredential
);
console.log("Shares:");
for await (const share of serviceClient.listShares()) {
console.log(`- ${share.name}`);
}
// Create a share
const shareName = `newshare${new Date().getTime()}`;
const shareClient = serviceClient.getShareClient(shareName);
await shareClient.create();
console.log(`Created share ${shareClient.name} successfully.`);
// Delete share
await shareClient.delete();
console.log(`Deleted share ${shareClient.name}.`);
}
main().catch((error) => {
console.error(error);
process.exit(1);
});