你好,游客 登录 注册 搜索
背景:
阅读新闻

在Windows Azure平台上管理文件

[日期:2012-07-27] 来源:Linux社区  作者:aa466564931 [字体:大 中 小]

Context 类

  1. public class FileContext : TableServiceContext  
  2. {  
  3.     public FileContext(string baseAddress, StorageCredentials credentials)  
  4.         : base(baseAddress, credentials)  
  5.     {  
  6.   
  7.     }  
  8.   
  9.     /// <summary>   
  10.     /// Get all entities of table storage "files".   
  11.     /// </summary>   
  12.     public IEnumerable<FileEntity> GetEntities  
  13.     {  
  14.         get  
  15.         {  
  16.             var list = this.CreateQuery<FileEntity>("files");  
  17.             return list;  
  18.         }  
  19.     }  
  20. }  

DataSource 类

  1. public class FileDataSource  
  2. {  
  3.     private static CloudStorageAccount account;  
  4.     private FileContext context;  
  5.   
  6.     public FileDataSource()  
  7.     {  
  8.         // Create table storage client via cloud account.   
  9.         account = CloudStorageAccount.FromConfigurationSetting("StorageConnections");  
  10.         CloudTableClient client = account.CreateCloudTableClient();  
  11.         client.CreateTableIfNotExist("files");  
  12.   
  13.         // Table context properties.   
  14.         context = new FileContext(account.TableEndpoint.AbsoluteUri, account.Credentials);  
  15.         context.RetryPolicy = RetryPolicies.Retry(3, TimeSpan.FromSeconds(1));  
  16.         context.IgnoreResourceNotFoundException = true;  
  17.         context.IgnoreMissingProperties = true;  
  18.           
  19.     }  
  20.   
  21.     /// <summary>   
  22.     /// Get all entities method.   
  23.     /// </summary>   
  24.     /// <returns></returns>   
  25.     public IEnumerable<FileEntity> GetAllEntities()  
  26.     {  
  27.         var list = from m in this.context.GetEntities  
  28.                    select m;  
  29.         return list;  
  30.     }  
  31.   
  32.     /// <summary>   
  33.     /// Get table rows by partitionKey.   
  34.     /// </summary>   
  35.     /// <param name="partitionKey"></param>   
  36.     /// <returns></returns>   
  37.     public IEnumerable<FileEntity> GetEntities(string partitionKey)  
  38.     {  
  39.         var list = from m in this.context.GetEntities  
  40.                    where m.PartitionKey == partitionKey  
  41.                    select m;  
  42.         return list;  
  43.     }  
  44.   
  45.     /// <summary>   
  46.     /// Get specify entity.   
  47.     /// </summary>   
  48.     /// <param name="partitionKey"></param>   
  49.     /// <param name="fileName"></param>   
  50.     /// <returns></returns>   
  51.     public FileEntity GetEntitiesByName(string partitionKey, string fileName)  
  52.     {  
  53.         var list = from m in this.context.GetEntities  
  54.                    where m.PartitionKey == partitionKey && m.FileName == fileName  
  55.                    select m;  
  56.         if (list.Count() > 0)  
  57.             return list.First<FileEntity>();  
  58.         else  
  59.             return null;  
  60.     }  
  61.   
  62.     /// <summary>   
  63.     /// Add an entity.   
  64.     /// </summary>   
  65.     /// <param name="entity"></param>   
  66.     public void AddFile(FileEntity entity)  
  67.     {  
  68.         this.context.AddObject("files", entity);  
  69.         this.context.SaveChanges();  
  70.     }  
  71.   
  72.     /// <summary>   
  73.     /// Make a judgment to check if file is exists.   
  74.     /// </summary>   
  75.     /// <param name="filename"></param>   
  76.     /// <param name="partitionKey"></param>   
  77.     /// <returns></returns>   
  78.     public bool FileExists(string filename, string partitionKey)  
  79.     {  
  80.         IEnumerable<FileEntity> list = from m in this.context.GetEntities  
  81.                    where m.FileName == filename && m.PartitionKey == partitionKey  
  82.                    select m;  
  83.         if (list.Count()>0)  
  84.         {  
  85.             return true;  
  86.         }  
  87.         else  
  88.         {  
  89.             return false;  
  90.         }  
  91.     }  
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款