Module azure.storage.files

ballerinax/azure.storage.files Ballerina library

1.0.1

Overview

Azure Files offers fully managed file shares in the cloud, accessible via the industry-standard SMB and NFS protocols and a REST API.

The Azure Files connector offers APIs to connect to Azure Files and manage shares and the directories and files within them, covering uploads, downloads, copies, renames, byte ranges, snapshots, and SAS token generation. It also provides a polling Listener that turns files arriving on a share into service events.

Key Features

  • Share-scoped Client for directory and file operations, transfers, copies, and byte ranges
  • Account-level AdminClient for creating, listing, deleting, and restoring shares
  • Polling Listener that routes files arriving on a watched path to raw, typed, or streaming content handlers, with an optional onError error handler
  • Share snapshots
  • Authentication with shared key, SAS tokens, connection strings, and Microsoft Entra ID
  • GraalVM compatible for native image builds

Setup guide

To use the Azure Files connector, you must have an Azure subscription and an Azure storage account. If you do not have an Azure account, you can sign up for one here.

Step 1: Create a storage account

  1. Sign in to the Azure portal, search for Storage accounts, and open it.

  2. Click + Create.

    Create a storage account

  3. On the Basics tab, provide the following:

    InputValue
    Subscription and Resource groupThe subscription and group the account bills to.
    Storage account nameA globally unique name.
    RegionThe region closest to your workload.
    PerformanceStandard is sufficient for SMB file shares; choose Premium with the File shares account type only for provisioned performance or NFS.

    Storage account basics

  4. Click Review + create, then Create, and wait for the deployment to complete. For the full set of options, see the Azure documentation.

Step 2: Create a file share

  1. Open the deployed storage account and navigate to Data storage > File shares.

  2. Click + File share, provide a name, and click Create. The share name is what you pass to the connector's Client at initialization.

    Create a file share

Step 3: Obtain the credentials

  1. In the storage account, navigate to Security + networking > Access keys.

  2. Click Show next to key1 and copy the following values:

    ValueUsed as
    Storage account nameaccountName
    key1 KeyaccountKey

    Copy the access key

  3. Optionally, use one of the other credentials the connector accepts: a SAS token or SAS URL (generated under Security + networking > Shared access signature), a connection string (shown alongside each access key), or Microsoft Entra ID credentials.

    Generate a SAS token

Quickstart

To use the azure.storage.files connector in your Ballerina application, modify the .bal file as follows:

Step 1: Import the module

Copy
import ballerinax/azure.storage.files;

Step 2: Instantiate a new connector

A Client is bound to a single file share. Provide the credentials through configurable variables:

Copy
configurable string accountName = ?;
configurable string accountKey = ?;

files:Client fileClient = check new ("reports", auth = {accountName, accountKey});

Step 3: Invoke the connector operation

Now, utilize the available connector operations.

Create the share

The client is bound to a share, so create it first if it does not exist yet.

Copy
files:AdminClient admin = check new (auth = {accountName, accountKey});
check admin->createShare("reports");

Upload a file

Paths are relative to the bound share, so /q1.pdf is at the share root. Azure does not create parent directories, so create a directory before writing into one.

Copy
check fileClient->uploadFromFile("./local/q1.pdf", "/q1.pdf");

Get the properties of a file

Copy
files:FileProperties props = check fileClient->getFileProperties("/q1.pdf");

Step 4: Run the Ballerina application

Save the changes and run the Ballerina application using the following command.

Copy
bal run

Examples

The azure.storage.files connector provides practical examples illustrating usage in various scenarios. Explore these examples, covering use cases like backing up a folder to a share, handing out a time-limited file link, and processing files dropped into a share folder.

  1. File backup - Back up a local folder to a file share and restore a file from it.
  2. Share handout - Upload a report and generate a time-limited, read-only SAS URL to share with a third party.
  3. Drop folder processor - Watch a folder on a share with the listener and process each dropped file, deleting JSON files and moving the rest into a processed folder.
  4. Change tracker - Derive created, modified, and deleted events from a watched share with an application-kept eTag snapshot.

Import

import ballerinax/azure.storage.files;Copy

Other versions

Metadata

Released date: 3 days ago

Version: 1.0.1

License: Apache-2.0


Compatibility

Platform: java21

Ballerina version: 2201.12.0

GraalVM compatible: Yes


Pull count

Total: 9

Current verison: 0


Weekly downloads


Source repository


Keywords

Type/Connector

Area/Storage & File Management

Cost/Paid

Vendor/Microsoft


Contributors