windows azure 实例

时间:2014-07-10 13:19:36   收藏:0   阅读:333
bubuko.com,布布扣
 1    public class Album : TableServiceEntity
 2     {       
 3     }
 4     public class PhotoAlbumDataContext : TableServiceContext
 5     {
 6       public PhotoAlbumDataContext()
 7             : this(CloudStorageAccount.FromConfigurationSetting("DataConnectionString"))
 8         {
 9         }
10 
11         public PhotoAlbumDataContext(Microsoft.WindowsAzure.CloudStorageAccount account)
12             : base(account.TableEndpoint.ToString(), account.Credentials)
13         {
14             if (!initialized)
15             {
16                 lock (initializationLock)
17                 {
18                     if (!initialized)
19                     {
20                         this.CreateTables();
21                         initialized = true;
22                     }
23                 }
24             }
25     
26      }
27       //getlist
28       var context = new PhotoAlbumDataContext();
29       var list=context.Albums.AsTableServiceQuery().AsEnumerable()
30       
31       
32       //add
33       context.AddObject("Alblum_TableName", new Album());
34       //context.SaveChanges();
35       context.SaveChanges(SaveChangesOptions.ContinueOnError);
36       
37       
38        //get delete
39         var album = context.Albums
40                .Where(a => a.AlbumId == albumName && a.PartitionKey == owner.ToLowerInvariant()).AsTableServiceQuery()
41                .Single();
42 
43          context.DeleteObject(album);
44          context.SaveChanges();
45         
46         //delete
47          context.AttachTo("TableName", photoRow, "*");
48          context.DeleteObject(photoRow);
49          context.SaveChanges();
50              
51          
52          
53          //update
54           var albumRow = new Album(album);
55          // attach and update the photo row
56          context.AttachTo("TableName", albumRow, "*");
57          context.UpdateObject(albumRow);
58          context.SaveChanges();
59          
60          
61          
62          
Table Service
bubuko.com,布布扣
 1        public class Subscriber : TableEntity
 2       {
 3         [Required]
 4         [Display(Name = "List Name")]
 5         public string ListName
 6         {
 7             get
 8             {
 9                 return this.PartitionKey; //分区键
10             }
11             set
12             {
13                 this.PartitionKey = value;
14             }
15         }
16 
17         [Required]
18         [Display(Name = "Email Address")]
19         public string EmailAddress
20         {
21             get
22             {
23                 return this.RowKey;
24             }
25             set
26             {
27                 this.RowKey = value;
28             }
29         }
30 
31         public string SubscriberGUID { get; set; }
32 
33         public bool? Verified { get; set; }
34     }
35 }
36 
37 
38    
39    
40    var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
41    var tableClient = storageAccount.CreateCloudTableClient();
42    var mailingListTable = tableClient.GetTableReference("mailinglist");
43        mailingListTable.CreateIfNotExists();
44        
45    
46    //获取Get
47    var retrieveOperation = TableOperation.Retrieve<T>(_partitionKey, _rowKey);
48    var retrievedResult = mailingListTable.Execute(retrieveOperation);
49    return retrievedResult.Result as T;
50    
51    //GetList
52      string filter = TableQuery.CombineFilters(
53                 TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, _partitionKey),
54                 TableOperators.And,
55                 TableQuery.GenerateFilterCondition("SubscriberGUID", QueryComparisons.Equal, subscriberGUID));
56             var query = new TableQuery<Subscriber>().Where(filter);
57             var subscribers = mailingListTable.ExecuteQuery(query).ToList();
58             
59             
60    //添加 Add
61     var insertOperation = TableOperation.Insert(newSubscriber);
62     mailingListTable.Execute(insertOperation);
63     
64     //Update Or Insert
65     var upsertOperation = TableOperation.InsertOrReplace(emailRowInTable);
66     mailingListTable.Execute(upsertOperation);
67     
68     
69     //Update
70      replaceOperation = TableOperation.Replace(emailRowInTable);
71      mailingListTable.Execute(replaceOperation);
72         
73     //Delete
74       var deleteOperation = TableOperation.Delete(emailRowToDelete);
75       mailingListTable.Execute(deleteOperation);
Sotrage.Tables
bubuko.com,布布扣
 1   var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
 2   var queueClient = storageAccount.CreateCloudQueueClient();
 3   foreach(var q in queueClient.ListQueues()){//所有队列
 4        var msg = q.GetMessage();
 5        var id = msg.Id;
 6        var name=q.Name; //队列名字:azuremailsubscribequeue
 7   }
 8   
 9   
10   subscribeQueue = queueClient.GetQueueReference("azuremailsubscribequeue");
11   subscribeQueue.CreateIfNotExists();
12   
13   
14   subscribeQueue.AddMessage(new CloudQueueMessage(newSubscriber.SubscriberGUID + "," + newSubscriber.ListName));
15   
16   msg = subscribeQueue.GetMessage();
17   
18   subscribeQueue.DeleteMessage(msg);
19   
20   
21   public class WorkerRole : RoleEntryPoint
22   {
23   
24   }
CloudQueue
bubuko.com,布布扣
 1 using System.Net;
 2 using System.Net.Mail;
 3 using Microsoft.WindowsAzure.Diagnostics;
 4 using Microsoft.WindowsAzure.ServiceRuntime;
 5 using Microsoft.WindowsAzure.Storage.Blob;
 6 using Microsoft.WindowsAzure.Storage.Queue;
 7 using Microsoft.WindowsAzure.Storage.Table; 
 8 using SendGridMail;
 9 using SendGridMail.Transport;
10 
11   var storageAccount=CloudStorageAccount.FromConfigurationSetting("DataConnectionString");
12   
13 
14         private static void SendSubscribeEmail(string subscriberGUID, Subscriber subscriber, MailingList mailingList)
15         {
16             var email = SendGrid.GenerateInstance();
17             email.From = new MailAddress(mailingList.FromEmailAddress);
18             email.AddTo(subscriber.EmailAddress);
19             string subscribeURL = RoleEnvironment.GetConfigurationSettingValue("AzureMailServiceURL") +
20                 "/subscribe?id=" + subscriberGUID + "&listName=" + subscriber.ListName;
21             email.Html = String.Format("<p>Click the link below to subscribe to {0}. " +
22                 "If you don‘t confirm your subscription, you won‘t be subscribed to the list.</p>" +
23                 "<a href=\"{1}\">Confirm Subscription</a>", mailingList.Description, subscribeURL);
24             email.Text = String.Format("Copy and paste the following URL into your browser in order to subscribe to {0}. " +
25                 "If you don‘t confirm your subscription, you won‘t be subscribed to the list.\n" +
26                 "{1}", mailingList.Description, subscribeURL);
27             email.Subject = "Subscribe to " + mailingList.Description;
28             var credentials = new NetworkCredential(RoleEnvironment.GetConfigurationSettingValue("SendGridUserName"), RoleEnvironment.GetConfigurationSettingValue("SendGridPassword"));
29             var transportREST = REST.GetInstance(credentials);
30             transportREST.Deliver(email);
31         }
32         
33         
34         private string GetBlobText(string blogRef)
35         {
36             var blob = blobContainer.GetBlockBlobReference(blogRef);
37             blob.FetchAttributes();
38             var blobSize = blob.Properties.Length;
39             using (var memoryStream = new MemoryStream((int)blobSize))
40             {
41                 blob.DownloadToStream(memoryStream);
42                 return System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
43             }
44         }
45         
46         
47         //upload image (stream)
48         var blob = container.GetBlobReference(file);
49             blob.Properties.ContentType = mimeType;
50             blob.UploadFromStream(binary);
51             
52             
53        //down image
54          var client = this.storageAccount.CreateCloudBlobClient();
55          var container = client.GetContainerReference(owner);
56 
57          using (var ms = new MemoryStream())
58          {
59               container.GetBlobReference(file).DownloadToStream(ms);
60               var image = Image.FromStream(ms); 
61          }
62          
63       
64          // save it off to blob storage
65           using (var thumbStream = new MemoryStream())
66           {
67                  thumb.Save(
68                      thumbStream,
69                      System.Drawing.Imaging.ImageFormat.Jpeg);
70 
71                    thumbStream.Position = 0; // reset;
72 
73                    var thumbBlob = container.GetBlobReference(Path.Combine("thumb", file));
74                    thumbBlob.Properties.ContentType = "image/jpeg";
75                    thumbBlob.UploadFromStream(thumbStream);
76            }
77            
78            
79            //use image
80            var blobUri = client.GetContainerReference(owner).GetBlobReference(file).Uri.ToString();
81            var thumbUri = client.GetContainerReference(owner).GetBlobReference(Path.Combine("thumb", file)).Uri.ToString();
82            
83          
84          
85            //delete 
86             
87            var blobGone = container.GetBlobReference(filename).DeleteIfExists();  
88            var thumbGone = container.GetBlobReference(thumbname).DeleteIfExists();
Storage.Blob

 

windows azure 实例,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!