asp.net mvc - Download and display a private Azure Blob using ASP MVC -


i'm using asp mvc 5 razor microsoft azure blob storage. can upload documents , images blob storage using mvc struggling find mvc examples how download , display files.

it quite straightforward if blobs stored public files, need them private.

can give me examples or guidance how achieve this?

i've got code below seems retrieve blob, i'm not sure in mvc display in browser.

var fullfilename = "file1.pdf"; var containername = "default";  // retrieve storage account connection string. cloudstorageaccount storageaccount = cloudstorageaccount.parse(configurationmanager.connectionstrings["attachmentstorageconnection"].connectionstring);  // create blob client. cloudblobclient blobclient = storageaccount.createcloudblobclient();  // retrieve reference created container. cloudblobcontainer container = blobclient.getcontainerreference(containername);  // retrieve reference blob ie "picture.jpg". cloudblockblob blockblob = container.getblockblobreference(fullfilename); 

i'm making assumption based on comment

it quite straightforward if blobs stored public files, need them private

that because blobs private attempting return byte array client via mvc controller.

however, alternate method use sharedaccesssignature provide client temporary access blob can access public url. period url valid can specified in controller. has advantage of taking load away controller client download file directly storage.

// view model public class myviewmodel {     string fileurl {get; set;} }   // controller public actionresult mycontrolleraction {     var readpolicy = new sharedaccessblobpolicy()     {         permissions = sharedaccessblobpermissions.read,         sharedaccessexpirytime = datetime.utcnow + timespan.fromminutes(5)     };     // code ------     // retrieve storage account connection string.     cloudstorageaccount storageaccount = cloudstorageaccount.parse(configurationmanager.connectionstrings   ["attachmentstorageconnection"].connectionstring);      // create blob client.     cloudblobclient blobclient = storageaccount.createcloudblobclient();      // retrieve reference created container.     cloudblobcontainer container = blobclient.getcontainerreference(containername);      // retrieve reference blob ie "picture.jpg".     cloudblockblob blockblob = container.getblockblobreference(fullfilename);     //------      var newuri = new uri(blockblob.uri.absoluteuri + blockblob.getsharedaccesssignature(readpolicy));      var viewmodel = new myviewmodel()     {         fileurl = newuri.tostring()     };      return view("myviewname", viewmodel); } 

then in view can use view model value

//image <img src="@model.fileurl" /> //in new tab <a href="@model.fileurl" target="_blank">`open in new window`</a> 

Comments