public static string DownloadText(this IStorageBlob blob)
        {
            if (blob == null)
            {
                throw new ArgumentNullException("blob");
            }

            using (Stream stream = blob.OpenReadAsync(CancellationToken.None).GetAwaiter().GetResult())
                using (TextReader reader = new StreamReader(stream, StrictEncodings.Utf8))
                {
                    return(reader.ReadToEnd());
                }
        }
 private static async Task<string> ReadBlobAsync(IStorageBlob blob, CancellationToken cancellationToken)
 {
     // Beware! Blob.DownloadText does not strip the BOM!
     try
     {
         using (var stream = await blob.OpenReadAsync(cancellationToken))
         using (StreamReader sr = new StreamReader(stream, detectEncodingFromByteOrderMarks: true))
         {
             cancellationToken.ThrowIfCancellationRequested();
             string data = await sr.ReadToEndAsync();
             return data;
         }
     }
     catch
     {
         return null;
     }
 }
Exemple #3
0
        /// <summary>
        /// Given a log file (as a blob), parses it and return a collection of log entries matching version 1.0
        /// of the Storage Analytics Log Format.
        /// </summary>
        /// <param name="blob">Object representing a cloud blob with Storage Analytics log content.</param>
        /// <param name="cancellationToken">A token to monitor for cancellation request.</param>
        /// <returns>Collection of successfully parsed log entries.</returns>
        /// <exception cref="FormatException">If unable to parse a line in given log.</exception>
        /// <seealso cref="StorageAnalyticsLogEntry"/>
        /// <remarks>
        /// The method scans log file lines one at a time.
        /// First it attempts to detect a line format version and throws an exception if failed to do so.
        /// It skips all lines with version different than supported one, i.e. 1.0.
        /// Then it calls TryParseLogEntry to create a log entry out of every line of supported version and throws
        /// an exception if the parse method returns null.
        /// </remarks>
        public async Task <IEnumerable <StorageAnalyticsLogEntry> > ParseLogAsync(IStorageBlob blob,
                                                                                  CancellationToken cancellationToken)
        {
            List <StorageAnalyticsLogEntry> entries = new List <StorageAnalyticsLogEntry>();

            using (TextReader tr = new StreamReader(await blob.OpenReadAsync(cancellationToken)))
            {
                for (int lineNumber = 1; ; lineNumber++)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    string line = await tr.ReadLineAsync();

                    if (line == null)
                    {
                        break;
                    }

                    Version version = TryParseVersion(line);
                    if (version == null)
                    {
                        string message = String.Format(CultureInfo.CurrentCulture,
                                                       "Unable to detect a version of log entry on line {1} of Storage Analytics log file '{0}'.",
                                                       blob.Name, lineNumber);
                        throw new FormatException(message);
                    }

                    if (version == SupportedVersion)
                    {
                        StorageAnalyticsLogEntry entry = TryParseLogEntry(line);
                        if (entry == null)
                        {
                            string message = String.Format(CultureInfo.CurrentCulture,
                                                           "Unable to parse the log entry on line {1} of Storage Analytics log file '{0}'.",
                                                           blob.Name, lineNumber);
                            throw new FormatException(message);
                        }

                        entries.Add(entry);
                    }
                }
            }

            return(entries);
        }
        private static async Task <string> ReadBlobAsync(IStorageBlob blob, CancellationToken cancellationToken)
        {
            try
            {
                // Beware! Blob.DownloadText does not strip the BOM!
                using (var stream = await blob.OpenReadAsync(cancellationToken))
                    using (StreamReader sr = new StreamReader(stream, detectEncodingFromByteOrderMarks: true))
                    {
                        cancellationToken.ThrowIfCancellationRequested();
                        string data = await sr.ReadToEndAsync();

                        return(data);
                    }
            }
            catch
            {
                return(null);
            }
        }
        /// <summary>
        /// Given a log file (as a blob), parses it and return a collection of log entries matching version 1.0
        /// of the Storage Analytics Log Format.
        /// </summary>
        /// <param name="blob">Object representing a cloud blob with Storage Analytics log content.</param>
        /// <param name="cancellationToken">A token to monitor for cancellation request.</param>
        /// <returns>Collection of successfully parsed log entries.</returns>
        /// <exception cref="FormatException">If unable to parse a line in given log.</exception>
        /// <seealso cref="StorageAnalyticsLogEntry"/>
        /// <remarks>
        /// The method scans log file lines one at a time. 
        /// First it attempts to detect a line format version and throws an exception if failed to do so.
        /// It skips all lines with version different than supported one, i.e. 1.0.
        /// Then it calls TryParseLogEntry to create a log entry out of every line of supported version and throws 
        /// an exception if the parse method returns null.
        /// </remarks>
        public async Task<IEnumerable<StorageAnalyticsLogEntry>> ParseLogAsync(IStorageBlob blob,
            CancellationToken cancellationToken)
        {
            List<StorageAnalyticsLogEntry> entries = new List<StorageAnalyticsLogEntry>();

            using (TextReader tr = new StreamReader(await blob.OpenReadAsync(cancellationToken)))
            {
                for (int lineNumber = 1;; lineNumber++)
                {
                    cancellationToken.ThrowIfCancellationRequested();
                    string line = await tr.ReadLineAsync();
                    if (line == null)
                    {
                        break;
                    }

                    Version version = TryParseVersion(line);
                    if (version == null)
                    {
                        string message = String.Format(CultureInfo.CurrentCulture,
                            "Unable to detect a version of log entry on line {1} of Storage Analytics log file '{0}'.",
                            blob.Name, lineNumber);
                        throw new FormatException(message);
                    }

                    if (version == supportedVersion)
                    {
                        StorageAnalyticsLogEntry entry = TryParseLogEntry(line);
                        if (entry == null)
                        {
                            string message = String.Format(CultureInfo.CurrentCulture,
                                "Unable to parse the log entry on line {1} of Storage Analytics log file '{0}'.",
                                blob.Name, lineNumber);
                            throw new FormatException(message);
                        }

                        entries.Add(entry);
                    }
                }
            }

            return entries;
        }
        public static async Task<WatchableReadStream> TryBindStreamAsync(IStorageBlob blob, ValueBindingContext context)
        {
            Stream rawStream;
            try
            {
                rawStream = await blob.OpenReadAsync(context.CancellationToken);
            }
            catch (StorageException exception)
            {
                // Testing generic error case since specific error codes are not available for FetchAttributes 
                // (HEAD request), including OpenRead. 
                if (!exception.IsNotFound())
                {
                    throw;
                }

                return null;
            }
            
            return new WatchableReadStream(rawStream);
        }
        public static async Task <WatchableReadStream> TryBindStreamAsync(IStorageBlob blob, ValueBindingContext context)
        {
            Stream rawStream;

            try
            {
                rawStream = await blob.OpenReadAsync(context.CancellationToken);
            }
            catch (StorageException exception)
            {
                // Testing generic error case since specific error codes are not available for FetchAttributes
                // (HEAD request), including OpenRead.
                if (!exception.IsNotFound())
                {
                    throw;
                }

                return(null);
            }

            return(new WatchableReadStream(rawStream));
        }