Azure File Share SDK for JavaScript
This documentation covers the Azure File Share SDK for JavaScript, enabling you to interact with Azure Files programmatically from your JavaScript applications.
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.
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): ShareServiceClientShareServiceClient 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): ShareClientParameters
| Name | Type | Description |
|---|---|---|
shareName |
string |
The name of the file share. |
listShares(options?: ListSharesOptions): PagedAsyncIterableIterator<ShareItem>ShareClient
Represents a single file share.
create(options?: ShareCreateOptions): Promise<ShareCreateResponse>delete(options?: ShareDeleteOptions): Promise<ShareDeleteResponse>getDirectoryClient(directoryName: string): ShareDirectoryClientlistFiles(options?: ListFilesOptions): PagedAsyncIterableIterator<FileItem>ShareDirectoryClient
Represents a directory within a file share.
create(options?: DirectoryCreateOptions): Promise<DirectoryCreateResponse>delete(options?: DirectoryDeleteOptions): Promise<DirectoryDeleteResponse>getFileClient(fileName: string): ShareFileClientlistFiles(options?: ListFilesOptions): PagedAsyncIterableIterator<FileItem>ShareFileClient
Represents a file within a file share.
upload(content: string | Uint8Array | ReadableStream, contentLength: number, options?: FileUploadOptions): Promise<FileUploadResponse>download(options?: FileDownloadOptions): Promise<FileDownloadResponse>{
content: Uint8Array | ReadableStream;
// ... other properties
}
delete(options?: FileDeleteOptions): Promise<FileDeleteResponse>