3
votes

I want to upload a file to Azure blob storage asynchronously. I have tried the way suggested in the official sdk:

This is how I get the container:

public static class BlobHelper
{
    public static CloudBlobContainer GetBlobContainer()
    {
        // Pull these from config
        var blobStorageConnectionString = ConfigurationManager.AppSettings["BlobStorageConnectionString"];
        var blobStorageContainerName = ConfigurationManager.AppSettings["BlobStorageContainerName"];

        // Create blob client and return reference to the container
        var blobStorageAccount = CloudStorageAccount.Parse(blobStorageConnectionString);
        var blobClient = blobStorageAccount.CreateCloudBlobClient();
        var container = blobClient.GetContainerReference(blobStorageContainerName);
        container.CreateIfNotExists();
        container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob });
        return container;
    }
}

And this is how i try to upload the file:

var documentName = Guid.NewGuid().ToString();

CloudBlobContainer container = BlobHelper.GetBlobContainer();

CloudBlockBlob blockBlob = container.GetBlockBlobReference(documentName);

public class FilesService
{
    public async Task<string> UploadFiles(HttpContent httpContent)
    {

        var documentName = Guid.NewGuid().ToString();

        CloudBlobContainer container = BlobHelper.GetBlobContainer();

        CloudBlockBlob blockBlob = container.GetBlockBlobReference(documentName);

        using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
        {
            await blockBlob.UploadFromStreamAsync(fileStream);
        }

        return blockBlob.Uri.ToString();
    }
}

The problem is that I do not know how to get the path to my file (it is uploaded by the user).

When I try this:

var rootpath =  HttpContext.Current.Server.MapPath("~/App_Data");

var streamProvider = new MultipartFileStreamProvider(rootpath);

await httpContent.ReadAsMultipartAsync(streamProvider);
foreach (var file in streamProvider.FileData)
{
    var localName = file.LocalFileName;
    using (var fileStream = System.IO.File.OpenRead(file.LocalFileName))
    {
        await blockBlob.UploadFromStreamAsync(fileStream);
    }
}

And when I try a post request. The request just crashes and does not return anything (even an exception);

Solution:

The issue was resolved in the following way. I used a service method in order to be able to upload a collection of files.

Service Method to upload a collection of blobs

In the BlobHelper class I save the needed information about the container and then instantiate it, it is a static class. Using a collection makes it possible to upload a multiple files as a part of the same stream.

1
I am trying to upload the file! By obtaining its path.StefanL19
can you please share you working code snippet for the above issue?Thanks.Sampath
I will add it as an extension to the question.StefanL19
still, it is not clear for me.what happened to this section of code snippet?await httpContent.ReadAsMultipartAsync(streamProvider);Sampath
Remove this part, you do not need it at all. The snippet in the service is going to upload the file.StefanL19

1 Answers

3
votes

I think you are trying to get the path to the file that is being uploaded to the Blob Storage using standard ASP.NET methods and local context. Files uploaded to the blob will not be accessible that way.

Seems like you upload your blob properly. Now, if your file uploaded successfully, your method should return blockBlob.Uri.ToString(), which is the link to your file - you may store it somewhere in the database or anywhere else.