雲端運算儲存Windows Azure Storage 隨筆

來源:互聯網
上載者:User

進入 Windows Azure Storage, 建立honggensave帳戶,結果如所示

存入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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.