SliderUK
SliderUK

Reputation: 167

C# MVC Web App Service Connect to Azure Storage Blob

I've got a basic web app in C# MVC (i'm new to MVC) which is connected to a database. In that database there is a table with a list of filenames. These files are stored in Azure Storage Blob Container.

I've used Scaffolding (creates a controller and view) to show data from my table of filenames and that works fine.

Now I would like to connect those filenames to the blob storage so that the user can click on and open them. How do I achieve this?

Do I edit the index view? Do I get the user to click on a filename and then connect to Azure storage to open that file? How is this done?

Please note that files on storage are private and is accessed using the storage key. Files cannot be made public.

Thanks for any advice.

[Update]

I've implemented the Shared Access Signature (SAS) using the code below.

public static string GetSASUrl(string containerName)
    {
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
        CloudBlobContainer container = blobClient.GetContainerReference(containerName);
        BlobContainerPermissions containerPermissions = new BlobContainerPermissions();
        containerPermissions.SharedAccessPolicies.Add("twominutepolicy", new SharedAccessBlobPolicy()
        {
            SharedAccessStartTime = DateTime.UtcNow.AddMinutes(-1),
            SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(2),
            Permissions = SharedAccessBlobPermissions.Write | SharedAccessBlobPermissions.Read
        });
        containerPermissions.PublicAccess = BlobContainerPublicAccessType.Off;
        container.SetPermissions(containerPermissions);
        string sas = container.GetSharedAccessSignature(new SharedAccessBlobPolicy(), "twominutepolicy");
        return sas;
    }

    public static string GetSasBlobUrl(string containerName, string fileName, string sas)
    {
        // Create new storage credentials using the SAS token.
        StorageCredentials accountSAS = new StorageCredentials(sas);
        // Use these credentials and the account name to create a Blob service client.
        CloudStorageAccount accountWithSAS = new CloudStorageAccount(accountSAS, [Enter Account Name], endpointSuffix: null, useHttps: true);
        CloudBlobClient blobClientWithSAS = accountWithSAS.CreateCloudBlobClient();

        // Retrieve reference to a previously created container.
        CloudBlobContainer container = blobClientWithSAS.GetContainerReference(containerName);

        // Retrieve reference to a blob named "photo1.jpg".
        CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);

        return blockBlob.Uri.AbsoluteUri + sas;
    }

Upvotes: 0

Views: 958

Answers (1)

Thiago Custodio
Thiago Custodio

Reputation: 18387

In order to access blobs that are not public, you'll need to use Shared Access Signatures, with that, you'll create access tokens valid for a period of time (you'll choose) and you can also restrict by IP address.

More info in here:

https://learn.microsoft.com/en-us/azure/storage/storage-dotnet-shared-access-signature-part-1

As they are not public, you'll need to add an additional step before pass the data to your view, which is concatenate the SAS token to the blob Uri. You can find a very good example in here: http://www.dotnetcurry.com/windows-azure/901/protect-azure-blob-storage-shared-access-signature

Upvotes: 0

Related Questions