Showing posts with label Blob. Show all posts
Showing posts with label Blob. Show all posts

Saturday, 26 October 2024

Copy Blob Between Azure Storage Accounts with C# Using DefaultAzureCredential

 Copying a blob between Azure storage account without downloading the blob locally is realy simple with azcopy command. Now using Azure.Storage.Blobs we can copy blobs between Azure sotrages without downloading the blob locally, with C# code as well. Using DefaultAzureCredential for the copy operation is useful, when we use apps with managed identities, such as workload identity in AKS. Let's explore steps necessary to copy a blob from one storage account to another with C# console app.

How it works

When the example simple console app is executed it copies blob from sourse storage account to target storage account as shown below.


Saturday, 28 September 2024

Remove 409 Conflict when Creating an Azure Storage Blob Container with .NET

 Limiting unnecerry app insights .NET dependecy logs as dicussed in the post "Reducing Log Analytics Cost for App Insights by Removing Successful Dependency Logs Ingestion from .NET Applications" is useful to reduce the costs. However, there are some .NET SDK methods, which are generating unnecessary execptions when used, filling up app indsights with exceptions. Once such method usage is BlobContainerClient.CreateIfNotExists, where it will always throw a 409 conflict, if the blob container is already exist. 


Friday, 19 July 2024

Resolve "System.ArgumentNullException: Value cannot be null. (Parameter 'sharedKeyCredential')" in Generaiting SaS Uri for Azure Storage Blob while using DefaultAzureCredential

 To share an Azure blob for  downloading or editing requires sharing a link with a shared access signature (SaS) with required permissions. The BlobContainerClient.GenerateSasUri Method helps to generate a Uri to share. The  BlobContainerClient.GenerateSasUri Method works only if the BlobServiceClient is created using storage access signature as shown below.

string connectionString = "DefaultEndpointsProtocol=https;AccountName=cheuw001assetssthot;AccountKey=xxxxxxxxxxxxxxxxxxxxxxx==;EndpointSuffix=core.windows.net";
BlobServiceClient blobServiceClient = new(connectionString);

The usage of paswordless authentication using managed identities is the recommended approach to use Azure resources.  If the DefaultAzureCredential is used with managed identity (user assigned or system assigned) to create BlobServiceClient as shown below,  BlobContainerClient.GenerateSasUri Method  failes with error "System.ArgumentNullException: Value cannot be null. (Parameter 'sharedKeyCredential')".

BlobServiceClient blobServiceClient = new(
    new Uri("https://cheuw001assetssthot.blob.core.windows.net/"),
    new DefaultAzureCredential());

Sunday, 1 May 2016

ASP.NET Core 1.0–Store Images in Azure Blob When Deployed as Azure Web Site


Data and images are not meant to be stored in Azure Web Site web root directory. It could cause problems doing so, few ideas discussed in below links.
http://stackoverflow.com/questions/12964129/can-i-write-to-file-system-on-azure-web-site
https://social.msdn.microsoft.com/Forums/vstudio/en-US/a8dec55e-c74b-482b-bc65-1c580e4672f4/i-cant-upload-image-on-azure-website?forum=windowsazurewebsitespreview
http://stackoverflow.com/questions/14323548/upload-picture-to-windows-azure-web-site
To store a image or other files in Azure blob container, the below class can be used, with ASP.NET Core 1.0. This class creates the blob container if it does not exist, with required access level (Blob) to allow store and retrieve images to the web site. (Class and interface can be downloaded from here)

Class

using System.Threading.Tasks;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.AspNet.Http;

namespace BookMyEvents.Services
{
    public class AzureImageHandlerService : IAzureImageHandlerService
    {
        public async Task<string> UploadFileToBlob(IFormFile file, string storageConnectionString, string blobContainerName, string fileName)
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);

            // Create a blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Get a reference to a container 
            CloudBlobContainer container = blobClient.GetContainerReference(blobContainerName);

            // If container doesn’t exist, create it.
            await container.CreateIfNotExistsAsync(BlobContainerPublicAccessType.Blob, null, null);

            // Get a reference to a blob 
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);

            // Create or overwrite the blob with the contents of a local file
            using (var fileStream = file.OpenReadStream())
            {
                await blockBlob.UploadFromStreamAsync(fileStream);
            }

            return blockBlob.Uri.AbsoluteUri;
        }

        public async Task<bool> RemoveFileFromBlob(string storageConnectionString, string blobContainerName, string fileName)
        {
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(storageConnectionString);

            // Create a blob client.
            CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

            // Get a reference to a container 
            CloudBlobContainer container = blobClient.GetContainerReference(blobContainerName);

            // If container doesn’t exist, create it.
            await container.CreateIfNotExistsAsync(BlobContainerPublicAccessType.Blob, null, null);

            // Get a reference to a blob 
            CloudBlockBlob blockBlob = container.GetBlockBlobReference(fileName);

            // Delete the blob if it is existing
            return await blockBlob.DeleteIfExistsAsync();            
        }
    }
}

Interface

using Microsoft.AspNet.Http;
using System.Threading.Tasks;

namespace BookMyEvents.Services
{
    public interface IAzureImageHandlerService
    {
        Task<string> UploadFileToBlob(IFormFile file, string storageConnectionString, string blobContainerName, string fileName);
        Task<bool> RemoveFileFromBlob(string storageConnectionString, string blobContainerName, string fileName);
    }
}

The above class can be used in an ASP.NET Core 1.0 web application as shown below.image
In startup class Startup.cs of ASP.NET Core 1.0 web application, add the class to SerivcesCollection as shown below to make it available for MVC 6.0 controllers.
services.AddScoped<IAzureImageHandlerService, AzureImageHandlerService>();image
Then in a controller, this can be used. MVC 6.0 will auto inject it to controller.image
Upload File usage sampleimage
Remove File usage sampleimage

Popular Posts