This documentation covers the Azure File Share SDK for JavaScript, enabling you to interact with Azure Files programmatically from your JavaScript applications.

Introduction
Installation
Getting Started
API Reference

Introduction

Azure Files offers fully managed cloud file shares that are accessible via the industry-standard Server Message Block (SMB) protocol. This means you can mount your Azure file shares simultaneously on-premises or in the cloud. The Azure File Share SDK for JavaScript provides a robust and convenient way to manage these shares, including creating/deleting shares, managing file shares, uploading/downloading files, and setting permissions.

Note: This SDK is designed for Node.js environments and browser-based applications that can securely handle credentials.

Installation

You can install the Azure File Share SDK using npm or yarn:


npm install @azure/storage-file-share
# or
yarn add @azure/storage-file-share
                

Getting Started

To use the SDK, you'll need an Azure Storage account. You can create one through the Azure portal, Azure CLI, or Azure PowerShell. You'll also need a connection string or a Shared Access Signature (SAS) token for authentication.

1. Authentication

There are several ways to authenticate:

  • Connection String: The most straightforward method for development.
  • SAS Token: Provides granular permissions for a limited time.
  • Azure Identity Library: Recommended for production environments, using managed identities or service principals.

2. Creating a Share Client

Here's how to create a ShareServiceClient using a connection string:


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

// Replace with your actual storage account connection string
const connectionString = "YOUR_AZURE_STORAGE_CONNECTION_STRING";

async function main() {
    const shareServiceClient = ShareServiceClient.fromConnectionString(connectionString);

    console.log("ShareServiceClient created successfully.");

    // Example: List shares
    console.log("Listing shares:");
    for await (const item of shareServiceClient.listShares()) {
        console.log(`- ${item.name}`);
    }
}

main().catch((error) => {
    console.error("Error: ", error);
});
                

3. Creating a File Share

Once you have a ShareServiceClient, you can create a file share:


const shareName = "mysampleshare"; // Choose a name for your share

async function createShare() {
    const shareClient = shareServiceClient.getShareClient(shareName);
    await shareClient.create();
    console.log(`Share "${shareName}" created.`);
}

// Call this after creating the shareServiceClient
// createShare().catch((error) => console.error("Error creating share:", error));
                

4. Uploading a File

To upload a file, you'll need a ShareFileClient:


const directoryName = "my-directory"; // Optional: create a directory first
const fileName = "my-file.txt";
const fileContent = "Hello, Azure Files!";

async function uploadFile() {
    // Create a directory if it doesn't exist
    const shareClient = shareServiceClient.getShareClient(shareName);
    const directoryClient = shareClient.getDirectoryClient(directoryName);
    try {
        await directoryClient.create();
        console.log(`Directory "${directoryName}" created.`);
    } catch (error) {
        if (error.statusCode !== 409) { // Ignore if directory already exists
            throw error;
        }
        console.log(`Directory "${directoryName}" already exists.`);
    }

    // Get a client for the file
    const fileClient = directoryClient.getFileClient(fileName);

    // Upload the file content
    await fileClient.upload(fileContent, fileContent.length);
    console.log(`File "${fileName}" uploaded to "${directoryName}".`);
}

// Call this after creating the shareServiceClient
// uploadFile().catch((error) => console.error("Error uploading file:", error));
                

API Reference

ShareServiceClient

The main client for interacting with Azure File Share service.

static fromConnectionString(connectionString: string, options?: ShareServiceClientOptions): ShareServiceClient
Creates a ShareServiceClient from a storage account connection string.

Parameters

Name Type Description
connectionString string The connection string of the storage account.
options ShareServiceClientOptions Optional configuration options.
getShareClient(shareName: string): ShareClient
Gets a client to interact with a specific file share.

Parameters

Name Type Description
shareName string The name of the file share.
listShares(options?: ListSharesOptions): PagedAsyncIterableIterator<ShareItem>
Lists all file shares in the storage account.

ShareClient

Represents a single file share.

create(options?: ShareCreateOptions): Promise<ShareCreateResponse>
Creates a new file share.
delete(options?: ShareDeleteOptions): Promise<ShareDeleteResponse>
Deletes a file share.
getDirectoryClient(directoryName: string): ShareDirectoryClient
Gets a client to interact with a specific directory within the share.
listFiles(options?: ListFilesOptions): PagedAsyncIterableIterator<FileItem>
Lists files and directories within the share's root directory.

ShareDirectoryClient

Represents a directory within a file share.

create(options?: DirectoryCreateOptions): Promise<DirectoryCreateResponse>
Creates a new directory.
delete(options?: DirectoryDeleteOptions): Promise<DirectoryDeleteResponse>
Deletes a directory.
getFileClient(fileName: string): ShareFileClient
Gets a client to interact with a specific file within this directory.
listFiles(options?: ListFilesOptions): PagedAsyncIterableIterator<FileItem>
Lists files and directories within this directory.

ShareFileClient

Represents a file within a file share.

upload(content: string | Uint8Array | ReadableStream, contentLength: number, options?: FileUploadOptions): Promise<FileUploadResponse>
Uploads content to a file.
download(options?: FileDownloadOptions): Promise<FileDownloadResponse>
Downloads the content of a file.
Returns: A promise that resolves with the file content and metadata.
{
    content: Uint8Array | ReadableStream;
    // ... other properties
}
delete(options?: FileDeleteOptions): Promise<FileDeleteResponse>
Deletes a file.