Beispiel #1
0
        protected void SignButton_Click(object sender, EventArgs e)
        {
            if (this.FileUpload1.HasFile)
            {
                this.InitializeStorage();

                // upload the image to blob storage
                string uniqueBlobName = string.Format("guestbookpics/image_{0}{1}", Guid.NewGuid().ToString(), Path.GetExtension(this.FileUpload1.FileName));
                CloudBlockBlob blob = blobStorage.GetBlockBlobReference(uniqueBlobName);
                blob.Properties.ContentType = this.FileUpload1.PostedFile.ContentType;
                blob.UploadFromStream(this.FileUpload1.FileContent);
                System.Diagnostics.Trace.TraceInformation("Uploaded image '{0}' to blob storage as '{1}'", this.FileUpload1.FileName, uniqueBlobName);

                // create a new entry in table storage
                GuestBookEntry entry = new GuestBookEntry() { GuestName = this.NameTextBox.Text, Message = this.MessageTextBox.Text, PhotoUrl = blob.Uri.ToString(), ThumbnailUrl = blob.Uri.ToString() };
                GuestBookDataSource ds = new GuestBookDataSource();
                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}", blob.Uri.ToString(), entry.PartitionKey, entry.RowKey));
                queue.AddMessage(message);
                System.Diagnostics.Trace.TraceInformation("Queued message to process blob '{0}'", uniqueBlobName);

            }

            this.NameTextBox.Text = string.Empty;
            this.MessageTextBox.Text = string.Empty;

            this.DataList1.DataBind();
        }
Beispiel #2
0
        protected void SignButton_Click(object sender, EventArgs e)
        {
            if (this.FileUpload1.HasFile)
            {
                this.InitializeStorage();

                // upload the image to blob storage
                string uniqueBlobName = string.Format("guestbookpics/image_{0}{1}", Guid.NewGuid().ToString(), Path.GetExtension(this.FileUpload1.FileName));
                string blobUri = _storage.AddToBucket("guestbookpics1", uniqueBlobName, this.FileUpload1.FileContent);
                System.Diagnostics.Trace.TraceInformation("Uploaded image '{0}' to blob storage as '{1}'", this.FileUpload1.FileName, uniqueBlobName);

                // create a new entry in table storage
                var entry = new GuestBookEntry() { GuestName = this.NameTextBox.Text, Message = this.MessageTextBox.Text, PhotoUrl = blobUri, ThumbnailUrl = blobUri };
                var ds = new GuestBookDataSource();
                ds.AddGuestBookEntry(entry);
                System.Diagnostics.Trace.TraceInformation("Added entry {0} in table storage for guest '{1}'", entry.Id, entry.GuestName);

                // queue a message to process the image
                var queue = _queue.GetQueueById("guestthumbs");
                var message = string.Format("{0},{1}", blobUri, entry.Id);
                _queue.SendMessage(queue, message);
                System.Diagnostics.Trace.TraceInformation("Queued message to process blob '{0}'", uniqueBlobName);
            }

            this.NameTextBox.Text = string.Empty;
            this.MessageTextBox.Text = string.Empty;

            this.DataList1.DataBind();
        }
        public override void Run()
        {
            Trace.TraceInformation("Listening for queue messages...");

            while (true)
            {
                try
                {
                    // retrieve a new message from the queue
                    CloudQueueMessage msg = this.queue.GetMessage();
                    if (msg != null)
                    {
                        // parse message retrieved from queue
                        var messageParts = msg.AsString.Split(new char[] { ',' });
                        var imageBlobUri = messageParts[0];
                        var partitionKey = messageParts[1];
                        var rowkey = messageParts[2];
                        Trace.TraceInformation("Processing image in blob '{0}'.", imageBlobUri);

                        string thumbnailBlobUri = System.Text.RegularExpressions.Regex.Replace(imageBlobUri, "([^\\.]+)(\\.[^\\.]+)?$", "$1-thumb$2");

                        CloudBlob inputBlob = this.container.GetBlobReference(imageBlobUri);
                        CloudBlob outputBlob = this.container.GetBlobReference(thumbnailBlobUri);

                        using (BlobStream input = inputBlob.OpenRead())
                        using (BlobStream output = outputBlob.OpenWrite())
                        {
                            this.ProcessImage(input, output);

                            // commit the blob and set its properties
                            output.Commit();
                            outputBlob.Properties.ContentType = "image/jpeg";
                            outputBlob.SetProperties();

                            // update the entry in table storage to point to the thumbnail
                            GuestBookDataSource ds = new GuestBookDataSource();
                            ds.UpdateImageThumbnail(partitionKey, rowkey, thumbnailBlobUri);

                            // remove message from queue
                            this.queue.DeleteMessage(msg);

                            Trace.TraceInformation("Generated thumbnail in blob '{0}'.", thumbnailBlobUri);
                        }
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(1000);
                    }
                }
                catch (StorageClientException e)
                {
                    Trace.TraceError("Exception when processing queue item. Message: '{0}'", e.Message);
                    System.Threading.Thread.Sleep(5000);
                }
            }
        }
        public override void Run()
        {
            // This is a sample worker implementation. Replace with your logic.
            Trace.TraceInformation("GuestBook_WorkerRole entry point is called", "Information");

            while (true)
            {
                try
                {
                    // TODO: Retrieve a new message from the queue
                    CloudQueueMessage newMessage =  _queueStorage.GetNewMessage();


                    if (newMessage != null)
                    {
                        var newMessageParts = newMessage.AsString.Split(new [] {','});
                        var imageBlobName = newMessageParts[0];
                        var partitionKey = newMessageParts[1];
                        var rowKey = newMessageParts[2];
                        Trace.TraceInformation("Processing image in blob '{0}'", imageBlobName);

                        string thumbnailName = System.Text.RegularExpressions.Regex.Replace(imageBlobName, "([^\\.]+)(\\.[^\\.]+)?$", "$1-thumb$2");
                        Trace.TraceInformation("Generated Thumbnail name = '{0}'", thumbnailName);

                        CloudBlockBlob inputBlob = _blobStorage.GetBlockBlobReference(imageBlobName);
                        CloudBlockBlob outputBlob = _blobStorage.GetBlockBlobReference(thumbnailName);

                        using(Stream inputStream = inputBlob.OpenRead())
                        using (Stream outputStream = outputBlob.OpenWrite())
                        {
                            processImage(inputStream, outputStream);

                            outputBlob.Properties.ContentType = inputBlob.Properties.ContentType;
                            string outputBlobUri = outputBlob.Uri.ToString();

                            // Update related Guestbook entry in the table storage to point to the thumbnail's Uri
                            GuestBookDataSource guestBookDataSource = new GuestBookDataSource();
                            guestBookDataSource.UpdateImageThumbnail(partitionKey, rowKey, outputBlobUri);

                            // Remove the message from queue
                            _queueStorage.DeleteMessage(newMessage);

                            Trace.TraceInformation("Generated thumbnail in blob '{0}'.", outputBlobUri);
                        }
                    }
                }
                catch (Exception exception)
                {
                    Trace.TraceError("Exception when processing queue item. Message: '{0}'", exception.Message);
                    Thread.Sleep(5000);
                }

            }
        }
 private static void createNewEntryInCloudTableStorage(string name, string message, string uploadedFileBlobUri,
                                                       out string newEntryPartitionKey, out string newEntryRowKey)
 {
     var newGuestBookEntry = new GuestBookEntry
         {
             GuestName = name,
             Comment = message,
             PhotoUrl = uploadedFileBlobUri,
             ThumbnailUrl = uploadedFileBlobUri
         };
     var guestBookDataSource = new GuestBookDataSource();
     guestBookDataSource.AddGuestBookEntry(newGuestBookEntry);
     newEntryPartitionKey = newGuestBookEntry.PartitionKey;
     newEntryRowKey = newGuestBookEntry.RowKey;
     Trace.TraceInformation("Added entry {0}-{1} in table storage for guest '{2}'", newEntryPartitionKey,
                            newEntryRowKey, newGuestBookEntry.GuestName);
 }
 private IEnumerable<GuestBookEntryModel> retrieveGuestBookEntries()
 {
     var guestBookDataSource = new GuestBookDataSource();
     return guestBookDataSource.GetCurrentGuestBookEntries().ToMvcModels<GuestBookEntry, GuestBookItemModel>();
 }