Question

I want to be upload a file to Azure Blob storage and save the associated metadata relating to the file to table storage. What would be the best way to accomplish this? The blob storage URI needs to be saved in the table storage model data so I will first need to upload the file to blob storage before I can save metadata.

Would it be good practice to do this on the one form? Has anyone got any examples or tutorials that they could point me towards

Was it helpful?

Solution 2

I achieved this by posting the file to blob storage and taking the associated metadata from a formcollection on the same page. In my POST I use IO steam to read the file and the associated metadata as shown below:

[HttpPost]
public ActionResult NewContent(HttpPostedFileBase postedFile,FormCollection form)
{
    if (postedFile != null)
    {
        HttpPostedFileBase postedFileCopy = postedFile;
        postedFileCopy.InputStream.Position = 0;
        Stream stream = postedFile.InputStream;

        string[] name = form.GetValues("name");
        string[] author = form.GetValues("author");
        string[] description = form.GetValues("description");
        DateTime uploaddate = form.GetValues("uploaddate");
        DateTime expirydate = form.GetValues("expirydate");
        string[] participationpoints = form.GetValues("participationpoints");
        string[] contenttypeid = form.GetValues("contenttypeid");

        try
        {
            avm.AddContent(postedFile, stream, name, author, description, uploaddate, expirydate, participationpoints, contenttypeid);
        }
        catch (Exception ex)
        {
            return RedirectToAction("Index", "Admin");
        }
    }
}

In my AdminViewModel I then called a method saved the associated metadata through my dbcontext and so the changes were saved to SQL storage and while the file was also written to blob storage.

OTHER TIPS

First of all its technically impossible to achieve this as the URIs are different and so are the APIs dealing with Azure blobs and table storage.

You do have a few options though. First the blob API allows associating metadata with the blob. The Put Blob REST API call has the option x-ms-meta name:value. This way you could do a single HTTP request to store both blob contents and metadata. If you want to strictly store the metadata to the table storage AND do it in one-shot, then you can route your HTTP request through your web-server, post the content and metadata to the server-side app, which then handles storing to the blob and table storage the relevant content.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top