Azure Storage JavaScript Client Library Sample for File Operations

In this sample, we will demonstrate common scenarios for Azure File Storage that includes creating, listing and deleting file shares, directories and files.

Azure File storage is a service for storing large amounts of unstructured object data, such as text or binary data, that can be accessed from anywhere in the world via HTTP or HTTPS. You can use file storage to expose data publicly to the world, or to store application data privately.

With Azure File storage, you can migrate legacy applications that rely on file shares to Azure quickly and without costly rewrites. Applications running in Azure virtual machines or cloud services or from on-premises clients can mount a file share in the cloud, just as a desktop application mounts a typical SMB share. Any number of application components can then mount and access the File storage share simultaneously. In this sample, you are able to create a file service with storage account and SAS Token. Based on the file service, you could create a file share, list files, upload files and delete files.

Note: You may need set up a HTTP server to host this sample for IE11 and latest Chrome.


Step 1: Preparing an Azure Storage account with CORS rules set

Cross-origin resource sharing, or CORS, must be configured on the Azure Storage account to be accessed directly from JavaScript in the browser. You are able to set the CORS rules for specific Azure Storage account on the Azure Portal. The "Allowed origins" could be set to "*" to allow all the origins in this sample. For more information about CORS, see Cross-Origin Resource Sharing (CORS).

Step 2: Importing Azure Storage JavaScript Files

Importing azure-storage.file.js in your HTML file for file operations.

<script src="azure-storage.file.js"></script>

Step 3: Creating an Azure Storage File Service Object

The FileService object lets you work with files and directories. Following code creates a FileService object with storage account and SAS Token.

var fileUri = 'https://' + 'STORAGE_ACCOUNT' + '';
var fileService = AzureStorage.File.createFileServiceWithSas(fileUri, 'SAS_TOKEN');

You can load Azure Storage JavaScript Client Library in a CommonJS or AMD environment by JavaScript module loaders. If no module system is found, global variable AzureStorage.File will be set, which is the start point where we can create service objects for file and access to the storage utilities.

How to get full detailed API definitions? Currently, the JavaScript Client Library shares almost the same API definitions with Node.js SDK, besides Node.js runtime specific APIs. Please check API details on Azure Storage API reference documents. The JavaScript global variable AzureStorage.File is just like the object require('azure-storage') returns in Node.js, but limits to File related interfaces. Go to FileService to view possible methods provided by FileService class.
Warning: Azure Storage JavaScript Client Library also supports creating FileService based on Storage Account Key for authentication besides SAS Token. However, for security concerns, we recommend use of a limited time SAS Token, generated by a backend web server using a Stored Access Policy.

Step 4: File Share Operations

Share: A File storage share is an SMB file share in Azure. All directories and files must be created in a parent share. An account can contain an unlimited number of shares, and a share can store an unlimited number of files, up to the 5 TB total capacity of the file share.

List File Shares

FileService provides listSharesSegmented and listSharesSegmentedWithPrefix for listing file shares under a storage account.

fileService.listSharesSegmented(null, function(error, result) {
    if(error) {
        // List shares error
    } else {
        for (var i = 0, share; share = results.entries[i]; i++) {
            // Deal with share object

Create File Share

FileService provides createShare and createShareIfNotExists for creating a file share under a storage account.

fileService.createShareIfNotExists('myshare', function(error, result) {
    if(error) {
        // Create share error
    } else {
        // Create share successfully

Delete File Share

FileService provides deleteShare and deleteShareIfExists for deleting a file share under a storage account.

fileService.deleteShareIfExists('myshare', function(error, result) {
    if(error) {
        // Delete share error
    } else {
        // Delete share successfully

Executable Example

The sample will try to create an Azure Storage file service object based on SAS Token authorization. Enter your Azure Storage account name and SAS Token here. Make sure you have set the CORS rules for the Azure Storage file service, and the SAS Token is in valid period.

Azure Storage file service provides plenty of interfaces for file operations. In following example, you can try to list all the file shares under your storage account, and try to create or delete one file share from your account.

Step 5: Directory and File Operations

Directory in storage is an optional hierarchy of directories.

File: A file in the share. A file may be up to 1 TB in size.

List Files and Directories

FileService provides listFilesAndDirectoriesSegmented for listing directories and files under a file share.

fileService.listFilesAndDirectoriesSegmented('myfileshare', '', null, function(error, result, response) {
    if(error) {
        // List table entity error
    } else {
        for (var i = 0, dir; dir = results.entries.directories[i]; i++) {
            // Deal with directory object
        for (var i = 0, file; file = results.entries.files[i]; i++) {
            // Deal with file object

Create Directory

FileService provides createDirectory and createDirectoryIfNotExists for creating directories under a file share.

fileService.createDirectoryIfNotExists('myfileshare', 'mydirectory', function(error, result, response) {
    if(error) {
        // Create directory error
    } else {
        // Create directory successfully

Delete Directory

FileService provides deleteDirectory and deleteDirectoryIfExists for deleting directories under a file share.

fileService.deleteDirectoryIfExists('myfileshare', 'mydirectory', function(error, result, response) {
    if(error) {
        // Delete directory error
    } else {
        // Delete directory successfully

Upload File

FileService provides createFileFromBrowserFile for uploading a file from an HTML file in browsers.

Uploading file from stream. You can set up the file name as well as the size of this uploading session.

// If one file has been selected in the HTML file input element
var files = document.getElementById('fileinput').files;
var file = files[0];

var finishedOrError = false;
var speedSummary = fileService.createFileFromBrowserFile('myfileshare', 'mydirectory',, file, {}, function(error, result, response) {
    finishedOrError = true;
    if (error) {
        // Upload file failed
    } else {
        // Upload successfully

Checking the upload progress with speedSummary object.

speedSummary.on('progress', function () {
    var process = speedSummary.getCompletePercent();

Download File

FileService provides interfaces for downloading a file into browser memory directly. Because of browser's sandbox limitation, we cannot save the downloaded data trunks into disk until we get all the data trunks of a file into browser memory. The browser's memory size is also limited especially for downloading huge files, so it's recommended to download a file in browser with SAS Token authorized link directly.

Shared access signatures (SAS) are a secure way to provide granular access to files and directories without providing your storage account name or keys. Shared access signatures are often used to provide limited access to your data, such as allowing a mobile app to access files. The following code example generates a new shared access policy that allows the shared access signatures holder to perform read operations on the myfile file, and expires 100 minutes after the time it is created.

Note: You can choose to use the SAS Token in browser side, or generate a temporary SAS Token dynamically in your server side with Azure Storage C# or Node.js SDKs etc. according to your security requirements.
var downloadLink = fileService.getUrl('myshare', 'mydirectory', 'myfile', 'SAS_TOKEN');

Delete File

FileService provides deleteFile and deleteFileIfExists for deleting files under a file share.

fileService.deleteFileIfExists('myfileshare', 'mydirectory', 'myfile', function(error, result, response) {
    if(error) {
        // Delete file error
    } else {
        // Delete file successfully

Executable Example

After clicked the "Select" button on the file share list, you are able to operate with the directories and files under the selected file share.

Current Path:
Uploaded Bytes:

Step 6: Creating your JavaScript Application based on Azure Storage JavaScript Client Library

  1. Setting CORS rules for your selected Azure-Storage account file service.
  2. Including functional file(s) needed, such as "azure-storage.file.js" for file operation.
  3. Using keyword "AzureStorage.File" to access to Azure storage JavaScript APIs for files.
  4. Referring to API documents for detailed API definitions.

You can view the source code of this sample for detailed reference.