public async Task <StatisticsReport> Load(string name)
        {
            //  In NuGet we always use lowercase names for all blobs in Azure Storage
            name = name.ToLowerInvariant();

            string connectionString = _connectionString;

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
            CloudBlobClient     blobClient     = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer  container      = blobClient.GetContainerReference("stats");
            CloudBlockBlob      blob           = container.GetBlockBlobReference("popularity/" + name);

            MemoryStream stream = new MemoryStream();

            await Task.Factory.FromAsync(blob.BeginFetchAttributes(null, null), blob.EndFetchAttributes);

            await Task.Factory.FromAsync(blob.BeginDownloadToStream(stream, null, null), blob.EndDownloadToStream);

            stream.Seek(0, SeekOrigin.Begin);

            string content;

            using (TextReader reader = new StreamReader(stream))
            {
                content = reader.ReadToEnd();
            }

            return(new StatisticsReport(content, (blob.Properties.LastModified == null ? (DateTime?)null : blob.Properties.LastModified.Value.UtcDateTime)));
        }
        public async Task <string> Load(string name)
        {
            //  In NuGet we always use lowercase names for all blobs in Azure Storage
            name = name.ToLowerInvariant();

            string connectionString = _connectionString;

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(connectionString);
            CloudBlobClient     blobClient     = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer  container      = blobClient.GetContainerReference("stats");
            CloudBlockBlob      blob           = container.GetBlockBlobReference("popularity/" + name);

            MemoryStream stream = new MemoryStream();

            await Task.Factory.FromAsync(blob.BeginDownloadToStream(stream, null, null), blob.EndDownloadToStream);

            stream.Seek(0, SeekOrigin.Begin);

            string content;

            using (TextReader reader = new StreamReader(stream))
            {
                content = reader.ReadToEnd();
            }

            return(content);
        }
Beispiel #3
0
 /// <summary>
 /// Download Stream from BlobStore with a given name
 /// </summary>
 /// <param name="stream">
 /// Stream to download to
 /// </param>
 /// <param name="blobName">
 /// Name of the blob to download
 /// </param>
 /// <returns>
 /// Async Task Wrapper
 /// </returns>
 /// <remarks>
 /// Internal for Testing. Should not be called directly.
 /// </remarks>
 public async Task DownloadBlobAsync(Stream stream, string blobName)
 {
     CloudBlockBlob blockBlob = Container.GetBlockBlobReference(blobName);
     await Task.Factory.FromAsync(
         blockBlob.BeginDownloadToStream(stream, null, null),
         blockBlob.EndDownloadToStream).ConfigureAwait(false);
 }
Beispiel #4
0
        public static async Task <T> DownloadToStreamAsync <T>(this CloudBlockBlob blob, T stream)
            where T : Stream
        {
            var tcs = new TaskCompletionSource <T>();

            blob.BeginDownloadToStream(stream, iar =>
            {
                try { blob.EndDownloadToStream(iar); tcs.TrySetResult(stream); }
                catch (OperationCanceledException) { tcs.TrySetCanceled(); }
                catch (Exception ex) { tcs.TrySetException(ex); }
            }, null);
            return(await tcs.Task);
        }
Beispiel #5
0
        private static async Task <string> DownloadTextAsync(CloudBlockBlob blob)
        {
            using (Stream memoryStream = new MemoryStream())
            {
                IAsyncResult asyncResult = blob.BeginDownloadToStream(memoryStream, null, null);
                await Task.Factory.FromAsync(asyncResult, (r) => { blob.EndDownloadToStream(r); });

                memoryStream.Position = 0;
                byte[] biteArray = new byte[memoryStream.Length];
                memoryStream.Read(biteArray, 0, (int)memoryStream.Length);

                return(System.Convert.ToBase64String(biteArray));
            }
        }