azuremodel-view-controllerkendo-uiazure-blob-storage

How do I upload multiple files to Azure blob storage using an ID parameter as a container?


I've created a storage account on Azure for uploading files to. I am using KenoUI Async upload widget which allows multiple file uploads. What I'd like to do is have the system create a container based on the passing in Id parameter if it doesn't already exist.

I'm struggling with the actual upload part however, I'm not sure how to pass the enumerable files to the storage account. Here is my code so far:

public ActionResult Async_Save(IEnumerable<HttpPostedFileBase> files, string id)
    {
        //Connect to Azure
        CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("my_AzureStorageConnectionString"));

        //Create Blob Client
        CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

        //Retrieve a reference to a container
        CloudBlobContainer blobContainer = blobClient.GetContainerReference("vehicle_" + id);

        try
        {
            // Create the container if it doesn't already exist
            blobContainer.CreateIfNotExists();
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            Console.WriteLine(ex.InnerException);

        }
        foreach (var file in files)
        {
            //This doesn't seem right to me and it's where I'm struggling
            var fileName = Path.GetFileName(file.FileName);
            var physicalPath = Path.Combine(blobContainer, fileName);
            file.SaveAs(physicalPath);
        }

        // Return an empty string to signify success
        return Content("");
    }

What I've attempted to do is create a method that connects to my Azure storage account and:

  1. Check for the existence of a container with the same ID as the the parameter that's passed in, if it doesn't exist, create it.
  2. Upload the files to the existing or newly created directory and give them a prefix of the ID i.e. "_".

Solution

  • As Gaurav Mantri said above, we will need to use Azure Storage SDK.

    For better performance, we can do as below:

    Parallel.ForEach(files, file =>
    
    {
    
        CloudBlockBlob blob = blobContainer.GetBlockBlobReference(file.FileName); 
    
        blob.UploadFromStream(file.InputStream);
    
        file.InputStream.Close();
    
    
    });
    

    Your code will be better as below:

    public ActionResult Async_Save(List<HttpPostedFileBase> files, string id)
    
    {
    
    // Connect to Azure
    
    CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("my_AzureStorageConnectionString"));
    
    try
    
    {
    
       //Create Blob Client
    
       CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
    
       //Retrieve a reference to a container
    
       CloudBlobContainer blobContainer = blobClient.GetContainerReference("vehicle_" + id);
    
       // Create the container if it doesn't already exist
    
       blobContainer.CreateIfNotExists();
    
       Parallel.ForEach(files, file =>
    
       {
    
           CloudBlockBlob blob = blobContainer.GetBlockBlobReference(file.FileName);
    
           blob.UploadFromStream(file.InputStream);
    
           file.InputStream.Close();
    
       });
    
    }
    
    catch (Exception ex)
    
    {
    
        Console.WriteLine(ex.Message);
    
        Console.WriteLine(ex.InnerException);
    
    } 
    
    // Return an empty string to signify success
    
        return new ContentResult();
    
    }
    

    More information about how to use Azure Storage SDK, we can refer to:

    Get Quick Start with Storage SDK