|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT license. |
| 3 | + |
| 4 | +/** |
| 5 | + * This sample demonstrates how to use the different methods of authentication |
| 6 | + * to make calls to the Azure Tables Service |
| 7 | + * |
| 8 | + * @summary authenticates using different authentication methods |
| 9 | + * @azsdk-weight 40 |
| 10 | + */ |
| 11 | +import { TableServiceClient, TablesSharedKeyCredential } from "@azure/data-tables"; |
| 12 | + |
| 13 | +// Load the .env file if it exists |
| 14 | +import * as dotenv from "dotenv"; |
| 15 | +dotenv.config(); |
| 16 | + |
| 17 | +// URL of the tables endpoint |
| 18 | +const tablesUrl = process.env["TABLES_URL"] || ""; |
| 19 | + |
| 20 | +// You can find your storage account's name, connection strings and keys in the Azure portal. |
| 21 | +// Navigate to Settings > Access keys in your storage account's menu blade to see connection strings for both primary and secondary access keys |
| 22 | +const accountConnectionString = process.env["ACCOUNT_CONNECTION_STRING"] || ""; |
| 23 | +const accountName = process.env["ACCOUNT_NAME"] || ""; |
| 24 | +const accountKey = process.env["ACCOUNT_KEY"] || ""; |
| 25 | + |
| 26 | +// You can generate a SAS connection string and token for your storage account in the Azure Portal |
| 27 | +// Navigate to Settings > "Shared access signature" in your storage account's menu blade select the Allowed services, resource types, permissions and expiry options |
| 28 | +// and generate your SAS and connection string. |
| 29 | +const sasConnectionString = process.env["SAS_CONNECTION_STRING"] || ""; |
| 30 | +const sasToken = process.env["SAS_TOKEN"] || ""; |
| 31 | + |
| 32 | +/** |
| 33 | + * Create a TableServiceCLient using a SAS connection String |
| 34 | + */ |
| 35 | +async function tableServiceClientWithSasConnectionString() { |
| 36 | + const client = TableServiceClient.fromConnectionString(sasConnectionString); |
| 37 | + countTablesWithClient(client); |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Create a TableServiceCLient using a SAS token |
| 42 | + */ |
| 43 | +async function tableServiceClientWithSasToken() { |
| 44 | + const client = new TableServiceClient(`${tablesUrl}${sasToken}`); |
| 45 | + countTablesWithClient(client); |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Create a TableServiceCLient using an Account connection String. |
| 50 | + * Note that this authentication method is only supported in Node, |
| 51 | + * and it is not available for browsers |
| 52 | + */ |
| 53 | +async function tableServiceClientWithAccountConnectionString() { |
| 54 | + const client = TableServiceClient.fromConnectionString(accountConnectionString); |
| 55 | + countTablesWithClient(client); |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Create a TableServiceCLient using account name and account key |
| 60 | + * Note that this authentication method is only supported in Node, |
| 61 | + * and it is not available for browsers |
| 62 | + */ |
| 63 | +async function tableServiceClientWithAccountKey() { |
| 64 | + const creds = new TablesSharedKeyCredential(accountName, accountKey); |
| 65 | + const client = new TableServiceClient(tablesUrl, creds); |
| 66 | + countTablesWithClient(client); |
| 67 | +} |
| 68 | + |
| 69 | +async function countTablesWithClient(client: TableServiceClient) { |
| 70 | + const tablesIterator = client.listTables(); |
| 71 | + let count = 0; |
| 72 | + for await (const _table of tablesIterator) { |
| 73 | + count++; |
| 74 | + } |
| 75 | + |
| 76 | + console.log(`Listed ${count} tables`); |
| 77 | +} |
| 78 | + |
| 79 | +export async function main() { |
| 80 | + console.log("== Client Authentication Methods Sample =="); |
| 81 | + |
| 82 | + await tableServiceClientWithSasConnectionString(); |
| 83 | + await tableServiceClientWithSasToken(); |
| 84 | + |
| 85 | + await tableServiceClientWithAccountConnectionString(); |
| 86 | + await tableServiceClientWithAccountKey(); |
| 87 | +} |
| 88 | + |
| 89 | +main().catch((err) => { |
| 90 | + console.error("The sample encountered an error:", err); |
| 91 | +}); |
0 commit comments