Search code examples
azure-storagevisual-studio-lightswitchazure-blob-storagelightswitch-2012

Saving images in Azure Blob Storage using Lightswitch 2012


I have a lightswitch application which currently stores all data in SQL Azure - including images. But I would like to keep the images separately in Azure Blob Storage and all non-binary data as it is.

So the outcome of saving an entity in the lightswitch app should be as follows: insert/update data SQL Azure and insert/update images to blob storage.

Any suggestion on the best approach on issue this would be greatly appreciated :).


Solution

  • I've added a short code to the server side of the LightSwitch application to save the images to Azure Blobs on add and update of the related data entry.

    namespace LightSwitchApplication
    {
        public partial class ApplicationDataService
        {
            string storageAccount = [aZURE_STORAGE_NAME_HERE]
            string containerName = [CONTAINTER_NAME_HERE]
            string policyName = [POLICY_NAME_HERE]
            string policySig = [OBTAINED_POLICY_SIG_HERE]
    
            partial void SaveChanges_Executing()
            {
                if (this.DataWorkspace.ApplicationData.Details.HasChanges)
                {
                    EntityChangeSet changeSet = this.DataWorkspace.ApplicationData.Details.GetChanges();
                    foreach (IEntityObject entity in changeSet.ModifiedEntities)
                    {
                        string type = entity.GetType().Name;
                        // ... 
                        // My type of LightSwitch entities are for example "Places"
    
                        UploadFileToBlob((Place)entity, containerName, policyName, policySig);
                    }
                }
            }
    
            private void UploadFileToBlob(Place p, String container, String policyName, String policySig)
            {
                string signature = "?sr=c&si=" + policyName + "&sig=" + policySig;
                string file = p.Id + ".png";
                WebResponse resp = UploadFile(storageAccount, container, file, signature, p.Photo);
            }
    
            static WebResponse UploadFile(string storageAccount, string container, string filename, string signature, byte[] data)
            {
                try
                {
                    var req = (HttpWebRequest)WebRequest.Create(string.Format("http://{0}.blob.core.windows.net/{1}/{2}{3}", storageAccount, container, filename, signature));
                    req.Method = "PUT";
                    req.ContentType = "image/png";
                    req.ContentLength = data.Length;
                    req.Date = DateTime.UtcNow;
                    //req.Headers.Add("x-ms-date", DateTime.UtcNow.ToString());
                    req.Headers.Add("x-ms-version", "2012-02-12");
                    req.Headers.Add("x-ms-blob-type", "BlockBlob");
    
                    using (Stream stream = req.GetRequestStream())
                    {
                        stream.Write(data, 0, data.Length);
                    }
    
                    var a = req.Headers.ToString();
    
                    return req.GetResponse();
                }
                catch (Exception e)
                { 
                    // ...
                }
            }
        }
    }
    

    What it does it sends the file to the blob any-time the corresponding entity is changed. Values in the square brackets need to be change in accordance to your Azure account.

    The method of obtaining policy signature is a Shared Access Signature well described in official documentation: Part 1 & Part 2.