Пример #1
2
        public async Task<Stream> Download(string containerName, string path)
        {
            CloudBlobContainer container = _blobClient.GetContainerReference(containerName);

            container.CreateIfNotExists();
            container.SetPermissions(new BlobContainerPermissions
            {
                PublicAccess = BlobContainerPublicAccessType.Blob
            });

            _blockBlob = container.GetBlockBlobReference(path);

            var ms = new MemoryStream();
            await _blockBlob.DownloadToStreamAsync(ms);
            return ms;


        }
        private async static Task<List<DeviceTelemetrySummaryModel>> LoadBlobTelemetrySummaryModelsAsync(
            CloudBlockBlob blob)
        {
            Debug.Assert(blob != null, "blob is a null reference.");

            var models = new List<DeviceTelemetrySummaryModel>();

            TextReader reader = null;
            MemoryStream stream = null;
            try
            {
                stream = new MemoryStream();
                await blob.DownloadToStreamAsync(stream);
                stream.Position = 0;
                reader = new StreamReader(stream);

                IEnumerable<StrDict> strdicts = ParsingHelper.ParseCsv(reader).ToDictionaries();
                DeviceTelemetrySummaryModel model;
                double number;
                string str;
                foreach (StrDict strdict in strdicts)
                {
                    model = new DeviceTelemetrySummaryModel();

                    if (strdict.TryGetValue("deviceid", out str))
                    {
                        model.DeviceId = str;
                    }

                    if (strdict.TryGetValue("averagehumidity", out str) &&
                       double.TryParse(
                            str,
                            NumberStyles.Float,
                            CultureInfo.InvariantCulture,
                            out number))
                    {
                        model.AverageHumidity = number;
                    }

                    if (strdict.TryGetValue("maxhumidity", out str) &&
                       double.TryParse(
                            str,
                            NumberStyles.Float,
                            CultureInfo.InvariantCulture,
                            out number))
                    {
                        model.MaximumHumidity = number;
                    }

                    if (strdict.TryGetValue("minimumhumidity", out str) &&
                       double.TryParse(
                            str,
                            NumberStyles.Float,
                            CultureInfo.InvariantCulture,
                            out number))
                    {
                        model.MinimumHumidity = number;
                    }

                    if (strdict.TryGetValue("timeframeminutes", out str) &&
                       double.TryParse(
                            str,
                            NumberStyles.Float,
                            CultureInfo.InvariantCulture,
                            out number))
                    {
                        model.TimeFrameMinutes = number;
                    }

                    // Translate LastModified to local time zone.  DateTimeOffsets 
                    // don't do this automatically.  This is for equivalent behavior 
                    // with parsed DateTimes.
                    if ((blob.Properties != null) &&
                        blob.Properties.LastModified.HasValue)
                    {
                        model.Timestamp = blob.Properties.LastModified.Value.LocalDateTime;
                    }

                    models.Add(model);
                }
            }
            finally
            {
                IDisposable disp;
                if ((disp = stream) != null)
                {
                    disp.Dispose();
                }

                if ((disp = reader) != null)
                {
                    disp.Dispose();
                }
            }

            return models;
        }
Пример #3
0
        /// <summary>
        /// Runs the mapper task.
        /// </summary>
        public async Task RunAsync()
        {
            CloudBlockBlob blob = new CloudBlockBlob(new Uri(this.blobSas));
            Console.WriteLine("Matches in blob: {0}/{1}", blob.Container.Name, blob.Name);

            using (Stream memoryStream = new MemoryStream())
            {
                //Download the blob.
                await blob.DownloadToStreamAsync(memoryStream);
                memoryStream.Seek(0, SeekOrigin.Begin);

                using (StreamReader streamReader = new StreamReader(memoryStream))
                {
                    Regex regex = new Regex(this.configurationSettings.RegularExpression);

                    int lineCount = 0;

                    //Read the file content.
                    while (!streamReader.EndOfStream)
                    {
                        ++lineCount;
                        string textLine = await streamReader.ReadLineAsync();

                        //If the line matches the search parameters, then print it out.
                        if (textLine.Length > 0)
                        {
                            if (regex.Match(textLine).Success)
                            {
                                Console.WriteLine("Match: \"{0}\" -- line: {1}", textLine, lineCount);
                            }
                        }
                    }
                }
            }
        }
		public async Task<Stream> LoadBlobAsync (Uri location)
		{
			// The container is setup for public read access
			// do not need to generate credentials
			var cloudBlob = new CloudBlockBlob(location);

			MemoryStream imageStream = new MemoryStream ();

			await cloudBlob.DownloadToStreamAsync (imageStream);

			return imageStream;
		}
        private async Task CloudBlockBlobUploadFromStreamAsync(bool seekableSourceStream, int startOffset)
        {
            byte[] buffer = GetRandomBuffer(1 * 1024 * 1024);

            CryptographicHash hasher = HashAlgorithmProvider.OpenAlgorithm("MD5").CreateHash();

            hasher.Append(buffer.AsBuffer(startOffset, buffer.Length - startOffset));
            string md5 = CryptographicBuffer.EncodeToBase64String(hasher.GetValueAndReset());

            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                await container.CreateAsync();

                CloudBlockBlob blob = container.GetBlockBlobReference("blob1");

                using (MemoryStream originalBlob = new MemoryStream())
                {
                    originalBlob.Write(buffer, startOffset, buffer.Length - startOffset);

                    Stream sourceStream;
                    if (seekableSourceStream)
                    {
                        MemoryStream stream = new MemoryStream(buffer);
                        stream.Seek(startOffset, SeekOrigin.Begin);
                        sourceStream = stream;
                    }
                    else
                    {
                        NonSeekableMemoryStream stream = new NonSeekableMemoryStream(buffer);
                        stream.ForceSeek(startOffset, SeekOrigin.Begin);
                        sourceStream = stream;
                    }

                    using (sourceStream)
                    {
                        BlobRequestOptions options = new BlobRequestOptions()
                        {
                            StoreBlobContentMD5 = true,
                        };
                        await blob.UploadFromStreamAsync(sourceStream.AsInputStream(), null, options, null);
                    }

                    await blob.FetchAttributesAsync();

                    Assert.AreEqual(md5, blob.Properties.ContentMD5);

                    using (MemoryStream downloadedBlob = new MemoryStream())
                    {
                        await blob.DownloadToStreamAsync(downloadedBlob.AsOutputStream());

                        TestHelper.AssertStreamsAreEqual(originalBlob, downloadedBlob);
                    }
                }
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
Пример #6
0
        public async Task DisableContentMD5ValidationTestAsync()
        {
            byte[] buffer = new byte[1024];
            Random random = new Random();

            random.NextBytes(buffer);

            BlobRequestOptions optionsWithNoMD5 = new BlobRequestOptions()
            {
                DisableContentMD5Validation = true,
                StoreBlobContentMD5         = true,
            };
            BlobRequestOptions optionsWithMD5 = new BlobRequestOptions()
            {
                DisableContentMD5Validation = false,
                StoreBlobContentMD5         = true,
            };

            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                await container.CreateAsync();

                CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob1");
                using (Stream stream = new NonSeekableMemoryStream(buffer))
                {
                    await blockBlob.UploadFromStreamAsync(stream.AsInputStream(), null, optionsWithMD5, null);
                }

                using (Stream stream = new MemoryStream())
                {
                    await blockBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithMD5, null);

                    await blockBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithNoMD5, null);

                    using (var blobStream = await blockBlob.OpenReadAsync(null, optionsWithMD5, null))
                    {
                        Stream blobStreamForRead = blobStream.AsStreamForRead();
                        int    read;
                        do
                        {
                            read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }

                    using (var blobStream = await blockBlob.OpenReadAsync(null, optionsWithNoMD5, null))
                    {
                        Stream blobStreamForRead = blobStream.AsStreamForRead();
                        int    read;
                        do
                        {
                            read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }

                    blockBlob.Properties.ContentMD5 = "MDAwMDAwMDA=";
                    await blockBlob.SetPropertiesAsync();

                    OperationContext opContext = new OperationContext();
                    await TestHelper.ExpectedExceptionAsync(
                        async() => await blockBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithMD5, opContext),
                        opContext,
                        "Downloading a blob with invalid MD5 should fail",
                        HttpStatusCode.OK);

                    await blockBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithNoMD5, null);

                    using (var blobStream = await blockBlob.OpenReadAsync(null, optionsWithMD5, null))
                    {
                        Stream blobStreamForRead = blobStream.AsStreamForRead();
                        TestHelper.ExpectedException <IOException>(
                            () =>
                        {
                            int read;
                            do
                            {
                                read = blobStreamForRead.Read(buffer, 0, buffer.Length);
                            }while (read > 0);
                        },
                            "Downloading a blob with invalid MD5 should fail");
                    }

                    using (var blobStream = await blockBlob.OpenReadAsync(null, optionsWithNoMD5, null))
                    {
                        Stream blobStreamForRead = blobStream.AsStreamForRead();
                        int    read;
                        do
                        {
                            read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }
                }

                CloudPageBlob pageBlob = container.GetPageBlobReference("blob2");
                using (Stream stream = new MemoryStream(buffer))
                {
                    await pageBlob.UploadFromStreamAsync(stream.AsInputStream(), null, optionsWithMD5, null);
                }

                using (Stream stream = new MemoryStream())
                {
                    await pageBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithMD5, null);

                    await pageBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithNoMD5, null);

                    using (var blobStream = await pageBlob.OpenReadAsync(null, optionsWithMD5, null))
                    {
                        Stream blobStreamForRead = blobStream.AsStreamForRead();
                        int    read;
                        do
                        {
                            read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }

                    using (var blobStream = await pageBlob.OpenReadAsync(null, optionsWithNoMD5, null))
                    {
                        Stream blobStreamForRead = blobStream.AsStreamForRead();
                        int    read;
                        do
                        {
                            read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }

                    pageBlob.Properties.ContentMD5 = "MDAwMDAwMDA=";
                    await pageBlob.SetPropertiesAsync();

                    OperationContext opContext = new OperationContext();
                    await TestHelper.ExpectedExceptionAsync(
                        async() => await pageBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithMD5, opContext),
                        opContext,
                        "Downloading a blob with invalid MD5 should fail",
                        HttpStatusCode.OK);

                    await pageBlob.DownloadToStreamAsync(stream.AsOutputStream(), null, optionsWithNoMD5, null);

                    using (var blobStream = await pageBlob.OpenReadAsync(null, optionsWithMD5, null))
                    {
                        Stream blobStreamForRead = blobStream.AsStreamForRead();
                        TestHelper.ExpectedException <IOException>(
                            () =>
                        {
                            int read;
                            do
                            {
                                read = blobStreamForRead.Read(buffer, 0, buffer.Length);
                            }while (read > 0);
                        },
                            "Downloading a blob with invalid MD5 should fail");
                    }

                    using (var blobStream = await pageBlob.OpenReadAsync(null, optionsWithNoMD5, null))
                    {
                        Stream blobStreamForRead = blobStream.AsStreamForRead();
                        int    read;
                        do
                        {
                            read = await blobStreamForRead.ReadAsync(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }
                }
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
        public static async Task<Stream> LoadFile(string location, string storagePrimary)
        {
            CloudStorageAccount account = CloudStorageAccount.Parse(storagePrimary);

            CloudBlockBlob blob = new CloudBlockBlob(new Uri(location), account.Credentials);
            MemoryStream stream = new MemoryStream();
            await blob.DownloadToStreamAsync(stream);
            stream.Seek(0, SeekOrigin.Begin);

            return stream;
        }
Пример #8
0
        public async Task BlockBlobWriteStreamBasicTestAsync()
        {
            byte[] buffer = GetRandomBuffer(3 * 1024 * 1024);
#if ASPNET_K
            MD5 hasher = MD5.Create();
#else
            CryptographicHash hasher = HashAlgorithmProvider.OpenAlgorithm("MD5").CreateHash();
#endif
            CloudBlobClient blobClient = GenerateCloudBlobClient();
            blobClient.DefaultRequestOptions.ParallelOperationThreadCount = 2;
            string             name      = GetRandomContainerName();
            CloudBlobContainer container = blobClient.GetContainerReference(name);
            try
            {
                await container.CreateAsync();

                CloudBlockBlob blob = container.GetBlockBlobReference("blob1");
                using (MemoryStream wholeBlob = new MemoryStream())
                {
                    BlobRequestOptions options = new BlobRequestOptions()
                    {
                        StoreBlobContentMD5 = true,
                    };
                    using (var writeStream = await blob.OpenWriteAsync(null, options, null))
                    {
                        Stream blobStream = writeStream.AsStreamForWrite();

                        for (int i = 0; i < 3; i++)
                        {
                            await blobStream.WriteAsync(buffer, 0, buffer.Length);

                            await wholeBlob.WriteAsync(buffer, 0, buffer.Length);

                            Assert.AreEqual(wholeBlob.Position, blobStream.Position);
#if !ASPNET_K
                            hasher.Append(buffer.AsBuffer());
#endif
                        }

                        await blobStream.FlushAsync();
                    }

#if ASPNET_K
                    string md5 = Convert.ToBase64String(hasher.ComputeHash(wholeBlob.ToArray()));
#else
                    string md5 = CryptographicBuffer.EncodeToBase64String(hasher.GetValueAndReset());
#endif
                    await blob.FetchAttributesAsync();

                    Assert.AreEqual(md5, blob.Properties.ContentMD5);

                    using (MemoryOutputStream downloadedBlob = new MemoryOutputStream())
                    {
                        await blob.DownloadToStreamAsync(downloadedBlob);

                        TestHelper.AssertStreamsAreEqual(wholeBlob, downloadedBlob.UnderlyingStream);
                    }
                }
            }
            finally
            {
                container.DeleteAsync().AsTask().Wait();
            }
        }
Пример #9
0
		//Source: http://stackoverflow.com/questions/18050836/getting-return-value-from-task-run
		private async Task<MemoryStream> WrapDownloadToStreamAsync(CloudBlockBlob ccb, MemoryStream ms, CancellationToken ct)
		{
			await Task.Run(() => ccb.DownloadToStreamAsync(ms, ct));
			return ms;
		}
        private async Task<Log> FetchLogFromBlob(string blobUri, MetricFilter filter, LocalizableString category)
        {
            var blob = new CloudBlockBlob(new Uri(blobUri));

            using (var memoryStream = new MemoryStream())
            {
                try
                {
                    await blob.DownloadToStreamAsync(memoryStream);
                }
                catch (StorageException ex)
                {
                    if (ex.RequestInformation.HttpStatusCode == 404)
                    {
                        return new Log
                        {
                            Category = new LocalizableString
                            {
                                LocalizedValue = category.LocalizedValue,
                                Value = category.Value
                            },
                            StartTime = filter.StartTime,
                            EndTime = filter.EndTime,
                            Value = new List<LogValue>()
                        };
                    }

                    throw;
                }

                memoryStream.Seek(0, 0);
                using (var streamReader = new StreamReader(memoryStream))
                {
                    string content = await streamReader.ReadToEndAsync();
                    var logBlob = JsonConvert.DeserializeObject<LogBlob>(content);
                    var logValues = logBlob.records.Where(x => x.Time >= filter.StartTime && x.Time < filter.EndTime).ToList();

                    return new Log
                    {
                        Category = category,
                        StartTime = filter.StartTime,
                        EndTime = filter.EndTime,
                        Value = logValues
                    };
                }
            }
        }
        private async static Task<List<AlertHistoryItemModel>> ProduceAlertHistoryItemsAsync(
            CloudBlockBlob blob)
        {
            IDisposable disp;
            IEnumerable<ExpandoObject> expandos;
            AlertHistoryItemModel model;
            List<AlertHistoryItemModel> models;
            TextReader reader;
            MemoryStream stream;

            Debug.Assert(blob != null, "blob is a null reference.");

            models = new List<AlertHistoryItemModel>();

            reader = null;
            stream = null;
            try
            {
                stream = new MemoryStream();
                await blob.DownloadToStreamAsync(stream);
                stream.Position = 0;
                reader = new StreamReader(stream);

                expandos = ParsingHelper.ParseCsv(reader).ToExpandoObjects();
                foreach (ExpandoObject expando in expandos)
                {
                    model =
                        ProduceAlertHistoryItem(
                            expando,
                            "temperatureruleoutput");

                    if (model != null)
                    {
                        models.Add(model);
                    }

                    model =
                        ProduceAlertHistoryItem(expando, "humidityruleoutput");

                    if (model != null)
                    {
                        models.Add(model);
                    }
                }
            }
            finally
            {
                if ((disp = stream) != null)
                {
                    disp.Dispose();
                }

                if ((disp = reader) != null)
                {
                    disp.Dispose();
                }
            }

            return models;
        }
        private async static Task<List<DeviceTelemetrySummaryModel>> LoadBlobTelemetrySummaryModelsAsync(
            CloudBlockBlob blob)
        {
            IDisposable disp;
            IEnumerable<StrDict> strdicts;
            DeviceTelemetrySummaryModel model;
            List<DeviceTelemetrySummaryModel> models;
            double number;
            TextReader reader;
            string str;
            MemoryStream stream;

            Debug.Assert(blob != null, "blob is a null reference.");

            models = new List<DeviceTelemetrySummaryModel>();

            reader = null;
            stream = null;
            try
            {
                stream = new MemoryStream();
                await blob.DownloadToStreamAsync(stream);
                stream.Position = 0;
                reader = new StreamReader(stream);

                strdicts = ParsingHelper.ParseCsv(reader).ToDictionaries();
                foreach (StrDict strdict in strdicts)
                {
                    model = new DeviceTelemetrySummaryModel();

                    if (strdict.TryGetValue("deviceid", out str))
                    {
                        model.DeviceId = str;
                    }

                    if (strdict.TryGetValue("averagehumidity", out str) &&
                       double.TryParse(str, out number))
                    {
                        model.AverageHumidity = number;
                    }

                    if (strdict.TryGetValue("maxhumidity", out str) &&
                       double.TryParse(str, out number))
                    {
                        model.MaximumHumidity = number;
                    }

                    if (strdict.TryGetValue("minimumhumidity", out str) &&
                       double.TryParse(str, out number))
                    {
                        model.MinimumHumidity = number;
                    }

                    if (strdict.TryGetValue("timeframeminutes", out str) &&
                       double.TryParse(str, out number))
                    {
                        model.TimeFrameMinutes = number;
                    }

                    if ((blob.Properties != null) &&
                        blob.Properties.LastModified.HasValue)
                    {
                        model.Timestamp =
                            blob.Properties.LastModified.Value.UtcDateTime;
                    }

                    models.Add(model);
                }
            }
            finally
            {
                if ((disp = stream) != null)
                {
                    disp.Dispose();
                }

                if ((disp = reader) != null)
                {
                    disp.Dispose();
                }
            }

            return models;
        }
        private async static Task<List<DeviceTelemetryModel>> LoadBlobTelemetryModelsAsync(
            CloudBlockBlob blob)
        {
            DateTime date;
            IDisposable disp;
            DeviceTelemetryModel model;
            List<DeviceTelemetryModel> models;
            double number;
            TextReader reader;
            string str;
            IEnumerable<StrDict> strdicts;
            MemoryStream stream;

            Debug.Assert(blob != null, "blob is a null reference.");

            models = new List<DeviceTelemetryModel>();

            reader = null;
            stream = null;
            try
            {
                stream = new MemoryStream();
                await blob.DownloadToStreamAsync(stream);
                stream.Position = 0;
                reader = new StreamReader(stream);

                strdicts = ParsingHelper.ParseCsv(reader).ToDictionaries();
                foreach (StrDict strdict in strdicts)
                {
                    model = new DeviceTelemetryModel();

                    if (strdict.TryGetValue("DeviceId", out str))
                    {
                        model.DeviceId = str;
                    }

                    if (strdict.TryGetValue("ExternalTemperature", out str) &&
                        double.TryParse(str, out number))
                    {
                        model.ExternalTemperature = number;
                    }

                    if (strdict.TryGetValue("Humidity", out str) &&
                        double.TryParse(str, out number))
                    {
                        model.Humidity = number;
                    }

                    if (strdict.TryGetValue("Temperature", out str) &&
                        double.TryParse(str, out number))
                    {
                        model.Temperature = number;
                    }

                    if (strdict.TryGetValue("EventEnqueuedUtcTime", out str) &&
                        DateTime.TryParse(str, out date))
                    {
                        model.Timestamp = date;
                    }

                    models.Add(model);
                }
            }
            finally
            {
                if ((disp = stream) != null)
                {
                    disp.Dispose();
                }

                if ((disp = reader) != null)
                {
                    disp.Dispose();
                }
            }

            return models;
        }
        private async Task<Dictionary<string, List<MetricValueBlob>>> FetchMetricValuesFromBlob(BlobInfo blobInfo, MetricFilter filter)
        {
            if (blobInfo.EndTime < filter.StartTime || blobInfo.StartTime >= filter.EndTime)
            {
                return new Dictionary<string, List<MetricValueBlob>>();
            }

            var blob = new CloudBlockBlob(new Uri(blobInfo.BlobUri));

            using (var memoryStream = new MemoryStream())
            {
                try
                {
                    await blob.DownloadToStreamAsync(memoryStream);
                }
                catch (StorageException ex)
                {
                    if (ex.RequestInformation.HttpStatusCode == 404)
                    {
                        return new Dictionary<string, List<MetricValueBlob>>();
                    }

                    throw;
                }
                
                memoryStream.Seek(0, 0);
                using (var streamReader = new StreamReader(memoryStream))
                {
                    string content = await streamReader.ReadToEndAsync();
                    var metricBlob = JsonConvert.DeserializeObject<MetricBlob>(content);
                    var metricValues = metricBlob.records;

                    var metricsPerName = new Dictionary<string, List<MetricValueBlob>>();
                    foreach (var metric in metricValues)
                    {
                        if (metric.time < filter.StartTime || metric.time >= filter.EndTime)
                        {
                            continue;
                        }

                        List<MetricValueBlob> metrics;
                        if (!metricsPerName.TryGetValue(metric.metricName, out metrics))
                        {
                            metrics = new List<MetricValueBlob>();
                            metricsPerName.Add(metric.metricName, metrics);
                        }

                        metrics.Add(metric);
                    }

                    return metricsPerName;
                }
            }
        }
 public async Task<MemoryStream> DownloadPictureFromCloud(PictureViewModel pictureViewModel)
 {
     try
     {
         var blob = new CloudBlockBlob(new Uri(pictureViewModel.PictureUrl), App.credentials);
         var ims = new MemoryStream();
         await blob.DownloadToStreamAsync(ims);
         return ims;
     }
     catch (Exception)
     {
         throw;
     }
 }
        /// <summary>
        /// Tests a blob SAS to determine which operations it allows.
        /// </summary>
        /// <param name="sasUri">A string containing a URI with a SAS appended.</param>
        /// <param name="blobContent">A string content content to write to the blob.</param>
        /// <returns>A Task object.</returns>
        private static async Task TestBlobSASAsync(string sasUri, string blobContent)
        {
            // Try performing blob operations using the SAS provided.

            // Return a reference to the blob using the SAS URI.
            CloudBlockBlob blob = new CloudBlockBlob(new Uri(sasUri));

            // Create operation: Upload a blob with the specified name to the container.
            // If the blob does not exist, it will be created. If it does exist, it will be overwritten.
            try
            {
                MemoryStream msWrite = new MemoryStream(Encoding.UTF8.GetBytes(blobContent));
                msWrite.Position = 0;
                using (msWrite)
                {
                    await blob.UploadFromStreamAsync(msWrite);
                }

                Console.WriteLine("Create operation succeeded for SAS {0}", sasUri);
                Console.WriteLine();
            }
            catch (StorageException e)
            {
                if (e.RequestInformation.HttpStatusCode == 403)
                {
                    Console.WriteLine("Create operation failed for SAS {0}", sasUri);
                    Console.WriteLine("Additional error information: " + e.Message);
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine(e.Message);
                    Console.ReadLine();
                    throw;
                }
            }

            // Write operation: Add metadata to the blob
            try
            {
                await blob.FetchAttributesAsync();
                string rnd = new Random().Next().ToString();
                string metadataName = "name";
                string metadataValue = "value";
                blob.Metadata.Add(metadataName, metadataValue);
                await blob.SetMetadataAsync();

                Console.WriteLine("Write operation succeeded for SAS {0}", sasUri);
                Console.WriteLine();
            }
            catch (StorageException e)
            {
                if (e.RequestInformation.HttpStatusCode == 403)
                {
                    Console.WriteLine("Write operation failed for SAS {0}", sasUri);
                    Console.WriteLine("Additional error information: " + e.Message);
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine(e.Message);
                    Console.ReadLine();
                    throw;
                }
            }

            // Read operation: Read the contents of the blob.
            try
            {
                MemoryStream msRead = new MemoryStream();
                using (msRead)
                {
                    await blob.DownloadToStreamAsync(msRead);
                    msRead.Position = 0;
                    using (StreamReader reader = new StreamReader(msRead, true))
                    {
                        string line;
                        while ((line = reader.ReadLine()) != null)
                        {
                            Console.WriteLine(line);
                        }
                    }

                    Console.WriteLine();
                }

                Console.WriteLine("Read operation succeeded for SAS {0}", sasUri);
                Console.WriteLine();
            }
            catch (StorageException e)
            {
                if (e.RequestInformation.HttpStatusCode == 403)
                {
                    Console.WriteLine("Read operation failed for SAS {0}", sasUri);
                    Console.WriteLine("Additional error information: " + e.Message);
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine(e.Message);
                    Console.ReadLine();
                    throw;
                }
            }

            // Delete operation: Delete the blob.
            try
            {
                await blob.DeleteAsync();
                Console.WriteLine("Delete operation succeeded for SAS {0}", sasUri);
                Console.WriteLine();
            }
            catch (StorageException e)
            {
                if (e.RequestInformation.HttpStatusCode == 403)
                {
                    Console.WriteLine("Delete operation failed for SAS {0}", sasUri);
                    Console.WriteLine("Additional error information: " + e.Message);
                    Console.WriteLine();
                }
                else
                {
                    Console.WriteLine(e.Message);
                    Console.ReadLine();
                    throw;
                }
            }
        }
        private async static Task<List<AlertHistoryItemModel>> ProduceAlertHistoryItemsAsync(CloudBlockBlob blob)
        {
            Debug.Assert(blob != null, "blob is a null reference.");

            var models = new List<AlertHistoryItemModel>();
            var stream = new MemoryStream();
            TextReader reader = null;
            try
            {
                await blob.DownloadToStreamAsync(stream);
                stream.Position = 0;
                reader = new StreamReader(stream);

                IEnumerable<ExpandoObject> expandos = ParsingHelper.ParseCsv(reader).ToExpandoObjects();
                foreach (ExpandoObject expando in expandos)
                {
                    AlertHistoryItemModel model = ProduceAlertHistoryItem(expando);

                    if (model != null)
                    {
                        models.Add(model);
                    }
                }
            }
            finally
            {
                IDisposable dispStream = stream as IDisposable;
                if (dispStream != null)
                {
                    dispStream.Dispose();
                }

                IDisposable dispReader = reader as IDisposable;
                if (dispReader != null)
                {
                    dispReader.Dispose();
                }
            }

            return models;
        }