windows azure 实例

时间:2023-01-01 22:06:04
    public class Album : TableServiceEntity
{
}
public class PhotoAlbumDataContext : TableServiceContext
{
public PhotoAlbumDataContext()
: this(CloudStorageAccount.FromConfigurationSetting("DataConnectionString"))
{
} public PhotoAlbumDataContext(Microsoft.WindowsAzure.CloudStorageAccount account)
: base(account.TableEndpoint.ToString(), account.Credentials)
{
if (!initialized)
{
lock (initializationLock)
{
if (!initialized)
{
this.CreateTables();
initialized = true;
}
}
} }
//getlist
var context = new PhotoAlbumDataContext();
var list=context.Albums.AsTableServiceQuery().AsEnumerable() //add
context.AddObject("Alblum_TableName", new Album());
//context.SaveChanges();
context.SaveChanges(SaveChangesOptions.ContinueOnError); //get delete
var album = context.Albums
.Where(a => a.AlbumId == albumName && a.PartitionKey == owner.ToLowerInvariant()).AsTableServiceQuery()
.Single(); context.DeleteObject(album);
context.SaveChanges(); //delete
context.AttachTo("TableName", photoRow, "*");
context.DeleteObject(photoRow);
context.SaveChanges(); //update
var albumRow = new Album(album);
// attach and update the photo row
context.AttachTo("TableName", albumRow, "*");
context.UpdateObject(albumRow);
context.SaveChanges();

Table Service

        public class Subscriber : TableEntity
{
[Required]
[Display(Name = "List Name")]
public string ListName
{
get
{
return this.PartitionKey; //分区键
}
set
{
this.PartitionKey = value;
}
} [Required]
[Display(Name = "Email Address")]
public string EmailAddress
{
get
{
return this.RowKey;
}
set
{
this.RowKey = value;
}
} public string SubscriberGUID { get; set; } public bool? Verified { get; set; }
}
} var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
var tableClient = storageAccount.CreateCloudTableClient();
var mailingListTable = tableClient.GetTableReference("mailinglist");
mailingListTable.CreateIfNotExists(); //获取Get
var retrieveOperation = TableOperation.Retrieve<T>(_partitionKey, _rowKey);
var retrievedResult = mailingListTable.Execute(retrieveOperation);
return retrievedResult.Result as T; //GetList
string filter = TableQuery.CombineFilters(
TableQuery.GenerateFilterCondition("PartitionKey", QueryComparisons.Equal, _partitionKey),
TableOperators.And,
TableQuery.GenerateFilterCondition("SubscriberGUID", QueryComparisons.Equal, subscriberGUID));
var query = new TableQuery<Subscriber>().Where(filter);
var subscribers = mailingListTable.ExecuteQuery(query).ToList(); //添加 Add
var insertOperation = TableOperation.Insert(newSubscriber);
mailingListTable.Execute(insertOperation); //Update Or Insert
var upsertOperation = TableOperation.InsertOrReplace(emailRowInTable);
mailingListTable.Execute(upsertOperation); //Update
replaceOperation = TableOperation.Replace(emailRowInTable);
mailingListTable.Execute(replaceOperation); //Delete
var deleteOperation = TableOperation.Delete(emailRowToDelete);
mailingListTable.Execute(deleteOperation);

Sotrage.Tables

   var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);
var queueClient = storageAccount.CreateCloudQueueClient();
foreach(var q in queueClient.ListQueues()){//所有队列
var msg = q.GetMessage();
var id = msg.Id;
var name=q.Name; //队列名字:azuremailsubscribequeue
} subscribeQueue = queueClient.GetQueueReference("azuremailsubscribequeue");
subscribeQueue.CreateIfNotExists(); subscribeQueue.AddMessage(new CloudQueueMessage(newSubscriber.SubscriberGUID + "," + newSubscriber.ListName)); msg = subscribeQueue.GetMessage(); subscribeQueue.DeleteMessage(msg); public class WorkerRole : RoleEntryPoint
{ }

CloudQueue

 using System.Net;
using System.Net.Mail;
using Microsoft.WindowsAzure.Diagnostics;
using Microsoft.WindowsAzure.ServiceRuntime;
using Microsoft.WindowsAzure.Storage.Blob;
using Microsoft.WindowsAzure.Storage.Queue;
using Microsoft.WindowsAzure.Storage.Table;
using SendGridMail;
using SendGridMail.Transport; var storageAccount=CloudStorageAccount.FromConfigurationSetting("DataConnectionString"); private static void SendSubscribeEmail(string subscriberGUID, Subscriber subscriber, MailingList mailingList)
{
var email = SendGrid.GenerateInstance();
email.From = new MailAddress(mailingList.FromEmailAddress);
email.AddTo(subscriber.EmailAddress);
string subscribeURL = RoleEnvironment.GetConfigurationSettingValue("AzureMailServiceURL") +
"/subscribe?id=" + subscriberGUID + "&listName=" + subscriber.ListName;
email.Html = String.Format("<p>Click the link below to subscribe to {0}. " +
"If you don't confirm your subscription, you won't be subscribed to the list.</p>" +
"<a href=\"{1}\">Confirm Subscription</a>", mailingList.Description, subscribeURL);
email.Text = String.Format("Copy and paste the following URL into your browser in order to subscribe to {0}. " +
"If you don't confirm your subscription, you won't be subscribed to the list.\n" +
"{1}", mailingList.Description, subscribeURL);
email.Subject = "Subscribe to " + mailingList.Description;
var credentials = new NetworkCredential(RoleEnvironment.GetConfigurationSettingValue("SendGridUserName"), RoleEnvironment.GetConfigurationSettingValue("SendGridPassword"));
var transportREST = REST.GetInstance(credentials);
transportREST.Deliver(email);
} private string GetBlobText(string blogRef)
{
var blob = blobContainer.GetBlockBlobReference(blogRef);
blob.FetchAttributes();
var blobSize = blob.Properties.Length;
using (var memoryStream = new MemoryStream((int)blobSize))
{
blob.DownloadToStream(memoryStream);
return System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
}
} //upload image (stream)
var blob = container.GetBlobReference(file);
blob.Properties.ContentType = mimeType;
blob.UploadFromStream(binary); //down image
var client = this.storageAccount.CreateCloudBlobClient();
var container = client.GetContainerReference(owner); using (var ms = new MemoryStream())
{
container.GetBlobReference(file).DownloadToStream(ms);
var image = Image.FromStream(ms);
} // save it off to blob storage
using (var thumbStream = new MemoryStream())
{
thumb.Save(
thumbStream,
System.Drawing.Imaging.ImageFormat.Jpeg); thumbStream.Position = ; // reset; var thumbBlob = container.GetBlobReference(Path.Combine("thumb", file));
thumbBlob.Properties.ContentType = "image/jpeg";
thumbBlob.UploadFromStream(thumbStream);
} //use image
var blobUri = client.GetContainerReference(owner).GetBlobReference(file).Uri.ToString();
var thumbUri = client.GetContainerReference(owner).GetBlobReference(Path.Combine("thumb", file)).Uri.ToString(); //delete var blobGone = container.GetBlobReference(filename).DeleteIfExists();
var thumbGone = container.GetBlobReference(thumbname).DeleteIfExists();

Storage.Blob