Azure Blob Storage - robinrodricks/FluentStorage GitHub Wiki

In order to use Microsoft Azure blob or file storage you need to reference FluentStorage.Azure.Blobs.

Connect to Azure Blob Storage

To create Azure Blob Storage with Shared Key:

IStore store = AzureBlobStorage.FromSharedKey(accountName, accountKey);
IStore store = AzureBlobStorage.FromSharedKey(accountName, accountKey, cloudEnvironment);
IStore store = AzureBlobStorage.FromSharedKey(accountName, accountKey, serviceUri, cloudEnvironment);

To create Azure Blob Storage with Azure AD (optionally with AD Authority endpoint):

IStore store = AzureBlobStorage.FromAzureAd(accountName, tenantId, applicationId, applicationSecret);
IStore store = AzureBlobStorage.FromAzureAd(accountName, tenantId, applicationId, applicationSecret, cloudEnvironment);
IStore store = AzureBlobStorage.FromAzureAd(accountName, tenantId, applicationId, applicationSecret, activeDirectoryAuthEndpoint);
IStore store = AzureBlobStorage.FromAzureAd(accountName, tenantId, applicationId, applicationSecret, activeDirectoryAuthEndpoint, cloudEnvironment);

To create Azure Blob Storage with Token Credentials:

IStore store = AzureBlobStorage.FromTokenCredential(accountName, tokenCredential);
IStore store = AzureBlobStorage.FromTokenCredential(accountName, tokenCredential, cloudEnvironment);

To create Azure Blob Storage with Managed Identity:

IStore store = AzureBlobStorage.FromMsi(accountName);
IStore store = AzureBlobStorage.FromMsi(accountName, cloudEnvironment);
IStore store = AzureBlobStorage.FromMsi(accountName, clientId);
IStore store = AzureBlobStorage.FromMsi(accountName, clientId, cloudEnvironment);

To create an instance of Azure Blob Storage that wraps around the native SDK CloudBlobClient (use the native option with caution)

IStore store = AzureBlobStorage.FromClient(client);

To create an instance of Azure Blob Storage that uses the local development storage emulator:

IStore store = AzureBlobStorage.FromLocalEmulator();

Connect using Connection Strings

To use connection strings, first register the module when your program starts by calling StorageFactory.Modules.UseAzureBlobStorage(); then use the following:

//using account name and key
IStore store = StorageFactory.FromConnectionString("azure.blob://account=account_name;key=secret_value");

//local development emulator
IStore store = StorageFactory.FromConnectionString("azure.blob://development=true");

This storage is working with block blobs only. We are planning to add append blobs support but that requires some architectural changes and as always you're welcome to help.

This package treats the first part of the path as container name. This allows you to have access to all the containers at once. For instance, path root/file.txt creates file file.txt in the root of container called root. root/folder1/file.txt creates file file.txt in folder folder1 under container root and so on. You can check if the folder returned is a container by referring to isContainer custom property (blob.Properties["IsContainer"] == "True").

Perform storage operations

Use our simple polycloud API to perform file and object manipulation operations.

Native operations

Access some Azure-specific operations by casting IStore to IAzureBlobStore.

Call the IStore.GetClient() method to return the native SDK client and perform any native operations.

SAS Tokens

You can obtain a SAS (Shared Access Signature) tokens to the following objects:

Storage Account

Getting SAS token for an account involves granting limited access to entire account. To grant it, for instance, for one hour from now, create a policy first:

var policy = new AccountSasPolicy(DateTimeOffset.?, TimeSpan.FromHours(1));

By default the policy is configured to give only List and Read permissions, meaning that users will be able to list containers and blobs, and also read them. You can customise policy permissions by modifying the Permissions flag property, for instance to also have Write permission you could explicitly assign it:

policy.Permissions =
   AccountSasPermission.List |
   AccountSasPermission.Read |
   AccountSasPermission.Write;

Then get the policy signature:

string sasUrl = await _native.GetStorageSasAsync(policy, true);

The second boolean parameter indicates whether to return full URL to the storage with SAS policy or only the policy itself. Setting it to true is useful if you want to use this URL in say Azure Storage Explorer to attach that account directly. Also, in order to connect to blob storage with SAS, you need the full URL:

To connect to an account using a policy, use the following factory method:

IStore sasInstance = AzureBlobStorage.FromSas(sasUrl);
Container

You can get container's Shared Access Signature in the same way as account's one, by calling to

string sasUrl = await _native.GetContainerSasAsync(containerName, policy, true);

This returns SAS URL that can be used in Azure Storage Explorer, or you can use it to connect in this library itself:

IStore sasInstance = AzureBlobStorage.FromSas(sasUrl);

Note that the method's signature is identical to account's one, actually it's the same method. FluentStorage takes care of figuring out whether SAS URL is for a container or for a storage account automatically. However, in case of a container SAS, the root folder in IStore instance is the container itself.

Blob

In order to get a signature for a specific blob, you can use use GetBlobSasAsync method. Calling it without any parameters for a blob, returns a read-only URL valid for 1 hour from now:

string publicUrl = await _native.GetBlobSasAsync(path);

You can then redistribute this URL amongst other users so they can download the content.

To customise the policy, pass additional parameters. For instance, to grant read/write access for 12 hours you can write the following code:

var policy = new BlobSasPolicy(TimeSpan.FromHours(12))
{
   Permissions = BlobSasPermission.Read | BlobSasPermission.Write
};

string publicUrl = await _native.GetBlobSasAsync(path, policy);

Blob Lease

There is a helper utility method to acquire a block blob lease or a container lease, which is useful for virtual transactions support. For instance:

using(AzureStorageLease lease = await _blobs.AcquireLeaseAsync(id, timeSpan))
{
   // your code
}

Where the first parameter is blob id or container name, and the second is lease duration. The BlobLease returned implements IDisposable pattern so that on exit the lease is returned. Note that if blob doesn't exist, current implementation will create a zero-size file and then acquire a least, just for your convenience. The blob is not deleted automatically though.

AcquireLeaseAsync also has an option to wait for the lease to be returned (third optional argument) which when set to true causes this library to try to acquire a lease every second until it's released, and re-lease it.

It also exposes RenewLeaseAsync() method to renew the lease explicitly.

⚠️ **GitHub.com Fallback** ⚠️