3

我有一个lightswitch应用程序,当前存储SQL Azure中的所有数据 - 包括图像。但是我想将图像分别保存在Azure Blob存储中,并保留所有非二进制数据。使用Lightswitch在Azure Blob存储中保存图像2012

因此,在lightswitch应用程序中保存实体的结果应如下所示:插入/更新数据SQL Azure并将图像插入/更新到blob存储。

任何有关问题的最佳方法的建议,将不胜感激:)。

+0

您是否找到解决方案?寻找同样的事情。 – 2014-02-24 09:40:54

+0

@NielsBosma看到我的答案在下面,希望有所帮助。 – krzysztofkarolczak 2014-03-05 20:31:18

回答

0

我已经添加了一个短代码到LightSwitch应用程序的服务器端,用于将图像保存到Azure Blob上以添加和更新相关数据条目。

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) 
      { 
       // ... 
      } 
     } 
    } 
} 

它做什么它随时发送文件到blob任何时候相应的实体改变。方括号中的值需要根据您的Azure帐户进行更改。

获得政策签名的方法是在官方文档中详细描述的共享访问签名:Part 1 & Part 2