云计算存储Windows Azure Storage 随笔
2011-07-12 12:55
507 查看
进入 Windows Azure Storage, 创建honggensave帐户,结果如下图所示
存入Blob,主要代码如下。
利用Azure Storage Explorer查看云上的存储情况,如下图:
需要注意的就是配置文件, 在本地调试的时候,使用:
<Setting name="DataConnectionString" value="UseDevelopmentStorage=true" />
<Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="UseDevelopmentStorage=true" />
部署到Azure上时,请使用正确的AccountName和AccountKey。此处配置为Primary Access Key。
<Setting name="DataConnectionString" value="DefaultEndpointsProtocol=https;AccountName=honggensave;AccountKey=C75CoTCNurlkmOmF0YLqgkVYrCgP8EecdlA8zLpx8ds8vF/9i+Lw+vS5fWPgk/4JW+edFzFsiTYPwOYFfbNl8w==" />
<Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="DefaultEndpointsProtocol=https;AccountName=honggensave;AccountKey=C75CoTCNurlkmOmF0YLqgkVYrCgP8EecdlA8zLpx8ds8vF/9i+Lw+vS5fWPgk/4JW+edFzFsiTYPwOYFfbNXXXX" />
查看Table数据存储,可以看到PartitionKey, RowKey。
存储一个JPG图片,可以用下列URL访问到:
https://honggensave.blob.core.windows.net/guestbookpics/image_2d1a11b1-8c37-43e9-857c-937bf37f32ef.jpg
存入Blob,主要代码如下。
using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.ServiceRuntime; using Microsoft.WindowsAzure.StorageClient; private static bool storageInitialized = false; private static object gate = new Object(); private static CloudBlobClient blobStorage; private static CloudQueueClient queueStorage; protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { Timer1.Enabled = true; } } protected void SignButton_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { InitializeStorage(); // upload the image to blob storage CloudBlobContainer container = blobStorage.GetContainerReference("guestbookpics"); string uniqueBlobName = string.Format("image_{0}.jpg", Guid.NewGuid().ToString()); CloudBlockBlob blob = container.GetBlockBlobReference(uniqueBlobName); blob.Properties.ContentType = FileUpload1.PostedFile.ContentType; blob.UploadFromStream(FileUpload1.FileContent); System.Diagnostics.Trace.TraceInformation("Uploaded image '{0}' to blob storage as '{1}'", FileUpload1.FileName, uniqueBlobName); // create a new entry in table storage GuestBookEntry entry = new GuestBookEntry() { GuestName = NameTextBox.Text, Message = MessageTextBox.Text, PhotoUrl = blob.Uri.ToString(), ThumbnailUrl = blob.Uri.ToString() }; GuestBookEntryDataSource ds = new GuestBookEntryDataSource(); ds.AddGuestBookEntry(entry); System.Diagnostics.Trace.TraceInformation("Added entry {0}-{1} in table storage for guest '{2}'", entry.PartitionKey, entry.RowKey, entry.GuestName); // queue a message to process the image var queue = queueStorage.GetQueueReference("guestthumbs"); var message = new CloudQueueMessage(String.Format("{0},{1},{2}", uniqueBlobName, entry.PartitionKey, entry.RowKey)); queue.AddMessage(message); System.Diagnostics.Trace.TraceInformation("Queued message to process blob '{0}'", uniqueBlobName); } NameTextBox.Text = ""; MessageTextBox.Text = ""; DataList1.DataBind(); } protected void Timer1_Tick(object sender, EventArgs e) { DataList1.DataBind(); } private void InitializeStorage() { if (storageInitialized) { return; } lock (gate) { if (storageInitialized) { return; } try { // read account configuration settings var storageAccount = CloudStorageAccount.FromConfigurationSetting("DataConnectionString"); // create blob container for images blobStorage = storageAccount.CreateCloudBlobClient(); CloudBlobContainer container = blobStorage.GetContainerReference("guestbookpics"); container.CreateIfNotExist(); // configure container for public access var permissions = container.GetPermissions(); permissions.PublicAccess = BlobContainerPublicAccessType.Container; container.SetPermissions(permissions); // create queue to communicate with worker role queueStorage = storageAccount.CreateCloudQueueClient(); CloudQueue queue = queueStorage.GetQueueReference("guestthumbs"); queue.CreateIfNotExist(); } catch (WebException) { throw new WebException("Storage services initialization failure. " + "Check your storage account configuration settings. If running locally, " + "ensure that the Development Storage service is running."); } storageInitialized = true; } }
利用Azure Storage Explorer查看云上的存储情况,如下图:
需要注意的就是配置文件, 在本地调试的时候,使用:
<Setting name="DataConnectionString" value="UseDevelopmentStorage=true" />
<Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="UseDevelopmentStorage=true" />
部署到Azure上时,请使用正确的AccountName和AccountKey。此处配置为Primary Access Key。
<Setting name="DataConnectionString" value="DefaultEndpointsProtocol=https;AccountName=honggensave;AccountKey=C75CoTCNurlkmOmF0YLqgkVYrCgP8EecdlA8zLpx8ds8vF/9i+Lw+vS5fWPgk/4JW+edFzFsiTYPwOYFfbNl8w==" />
<Setting name="Microsoft.WindowsAzure.Plugins.Diagnostics.ConnectionString" value="DefaultEndpointsProtocol=https;AccountName=honggensave;AccountKey=C75CoTCNurlkmOmF0YLqgkVYrCgP8EecdlA8zLpx8ds8vF/9i+Lw+vS5fWPgk/4JW+edFzFsiTYPwOYFfbNXXXX" />
查看Table数据存储,可以看到PartitionKey, RowKey。
存储一个JPG图片,可以用下列URL访问到:
https://honggensave.blob.core.windows.net/guestbookpics/image_2d1a11b1-8c37-43e9-857c-937bf37f32ef.jpg
相关文章推荐
- Windows Azure Storage (11) 计算你存储的Blob的大小
- Windows Azure Platform (九) Windows Azure Storage Service存储服务
- [New Portal]Windows Azure Storage (12) 本地冗余存储 vs 地理冗余存储 (上)
- Windows Azure Storage (2) Windows Azure Storage Service存储服务之Blob详解(上)
- Windows Azure Storage (3) Windows Azure Storage Service存储服务之Blob详解(中)
- Windows Azure Storage (4) Windows Azure Storage Service存储服务之Blob Share Access Signature
- Windows Azure Storage (23) 计算Azure VHD实际使用容量
- [New Portal]Windows Azure Storage (13) 本地冗余存储 vs 地理冗余存储 (下)
- Windows Azure Storage (1) Windows Azure Storage Service存储服务
- Windows Azure Storage (19) 再谈Azure Block Blob和Page Blob
- Windows Azure Storage (25) Azure Append Blob
- How Do I: Use Windows Azure Storage with my Application?
- Windows Azure Storage 客户端Java版概览
- Windows Azure Storage新的价格体系公布
- 使用Windows Azure Storage Analytics新特性
- 通过构建Storage Explorer 应用学习Windows Azure Storage APIs
- [Windows Azure] How to use the Windows Azure Blob Storage Service in .NET
- 关于Azure的存储(Storage)服务类型
- Windows Azure Storage (9) Windows Azure 上的托管服务CDN (中) Blob Service
- Windows 8学习笔记(四)--Storage数据存储