Azure Storage SDK for JavaScript - File Services

This documentation provides comprehensive guidance on using the Azure Storage SDK for JavaScript to interact with Azure File Shares. The SDK simplifies common operations like uploading, downloading, listing, and managing files and directories within your file shares.

Getting Started

To begin, ensure you have Node.js installed. Then, install the necessary SDK package:

npm install @azure/storage-file-share

Authentication

You can authenticate with Azure File Shares using a connection string or with Azure Identity credentials.

Using a Connection String

A connection string contains all the information needed to connect to your storage account.

Example: Creating a Share Client with Connection String


const { ShareServiceClient } = require("@azure/storage-file-share");

const connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";
const shareServiceClient = ShareServiceClient.fromConnectionString(connectionString);
            

Using Azure Identity

For more secure scenarios, especially in production environments, use Azure Identity. Ensure you have authenticated your environment (e.g., via Azure CLI login).

Example: Creating a Share Client with Default Azure Credential


const { ShareServiceClient } = require("@azure/storage-file-share");
const { DefaultAzureCredential } = require("@azure/identity");

const accountName = "YOUR_STORAGE_ACCOUNT_NAME";
const credential = new DefaultAzureCredential();
const shareServiceClient = new ShareServiceClient(`https://${accountName}.file.core.windows.net`, credential);
            

Key Concepts

Common Operations

Creating a File Share

Example: Create a new file share


async function createShare(shareServiceClient, shareName) {
    try {
        const createShareResponse = await shareServiceClient.createShare(shareName);
        console.log(`Share '${shareName}' created successfully.`);
    } catch (error) {
        console.error("Error creating share:", error.message);
    }
}
            

Listing File Shares

Example: List all file shares in the account


async function listShares(shareServiceClient) {
    console.log("Listing shares:");
    for await (const share of shareServiceClient.listShares()) {
        console.log(`- ${share.name}`);
    }
}
            

Working with Directories

You can create, list, and manage directories within a share.

Example: Create a directory and list its contents


async function manageDirectory(shareClient) {
    const directoryName = "my-directory";
    const directoryClient = shareClient.getDirectoryClient(directoryName);

    try {
        await directoryClient.create();
        console.log(`Directory '${directoryName}' created.`);

        console.log(`Listing contents of '${directoryName}':`);
        for await (const item of directoryClient.listHandles()) {
            console.log(`- ${item.name}`);
        }
    } catch (error) {
        console.error("Error managing directory:", error.message);
    }
}
            

Working with Files

Upload, download, and manage files.

Example: Upload and download a file


const { BlobServiceClient } = require("@azure/storage-blob"); // Note: File operations often use Blob SDK concepts/methods

async function manageFile(shareClient) {
    const directoryClient = shareClient.getDirectoryClient("my-directory");
    const fileName = "sample.txt";
    const fileClient = directoryClient.getFileClient(fileName);
    const fileContent = "Hello Azure Files!";

    try {
        // Upload file
        const uploadResponse = await fileClient.upload(fileContent, fileContent.length);
        console.log(`File '${fileName}' uploaded. Upload ID: ${uploadResponse.etag}`);

        // Download file
        const downloadResponse = await fileClient.download();
        const downloadedContent = await downloadResponse.text();
        console.log(`Downloaded content of '${fileName}': "${downloadedContent}"`);

    } catch (error) {
        console.error("Error managing file:", error.message);
    }
}
            
Note: While the file service is distinct, some underlying SDK mechanisms might share similarities or build upon concepts from the Blob service SDK. Always refer to the specific package documentation for the most accurate details.
Important: Secure your connection strings and manage credentials carefully, especially in production environments. Consider using Azure Key Vault for storing secrets.
View on GitHub View on npm