Upload a file to Azure Blob Storage
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.
StefanL19
I am a Computer Science and Mathematics student. My favorite programming languages are JavaScript, C++, and C#. I am currently focused on ASP.NET but I also like Ruby on Rails and Meteor.js
Updated on June 05, 2022Comments
-
StefanL19 almost 2 years
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.
In the
BlobHelper
class I save the needed information about the container and then instantiate it, it is astatic
class. Using a collection makes it possible to upload a multiple files as a part of the same stream. -
StefanL19 almost 8 yearsyes, but the problem is that the request does not each the uri part. It just crashes at the moment when ReadAsMultipartAsync is executed.
-
Alex Belotserkovskiy almost 8 years@StefanL19 i created the chat room - if you have a time, let's chat and see if we can resolve the issue