Search code examples
file-storageazure-service-fabric

How to provide a file share for a stateless service running in Azure Service Fabric cluster?


I am deploying a stateless API that has a file upload method. The API consumer uploads a file and the file is stored in the file system (network share) and the metadata is stored in the database.

However, when it is deployed to the Azure environment, I don't really know how to configure the service to be able to access Azure Files which supports SMB. The Service Fabric Mesh seems to support File volume driver but I am not using service mesh. Just good old Service Fabric.

So can you please recommend a way of not having to rewrite my file i/o so that it works in Azure with the File storage.

Thanks


Solution

  • You can script a mount to the file share. Use a service principal to access the storage credentials, or put them in configuration. Run the script as a setup entry point of the service. Make sure the script runs idempotent.

    $resourceGroupName = "<your-resource-group-name>"
    $storageAccountName = "<your-storage-account-name>"
    
    # These commands require you to be logged into your Azure account, run Login-AzAccount if you haven't
    # already logged in.
    $storageAccount = Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName
    $storageAccountKeys = Get-AzStorageAccountKey -ResourceGroupName $resourceGroupName -Name $storageAccountName
    
    # The cmdkey utility is a command-line (rather than PowerShell) tool. We use Invoke-Expression to allow us to 
    # consume the appropriate values from the storage account variables. The value given to the add parameter of the
    # cmdkey utility is the host address for the storage account, <storage-account>.file.core.windows.net for Azure 
    # Public Regions. $storageAccount.Context.FileEndpoint is used because non-Public Azure regions, such as sovereign 
    # clouds or Azure Stack deployments, will have different hosts for Azure file shares (and other storage resources).
    Invoke-Expression -Command ("cmdkey /add:$([System.Uri]::new($storageAccount.Context.FileEndPoint).Host) " + `
        "/user:AZURE\$($storageAccount.StorageAccountName) /pass:$($storageAccountKeys[0].Value)")