public async Task<string> GetETagAsync(IStorageBlob blob, CancellationToken cancellationToken)
        {
            try
            {
                await blob.FetchAttributesAsync(cancellationToken);
            }
            catch (StorageException exception)
            {
                // Note that specific exception codes are not available for FetchAttributes, which makes a HEAD request.

                if (exception.IsNotFound())
                {
                    // Blob does not exist.
                    return null;
                }
                else if (exception.IsOk())
                {
                    // If the blob type is incorrect (block vs. page) a 200 OK is returned but the SDK throws an
                    // exception.
                    return null;
                }
                else
                {
                    throw;
                }
            }

            return blob.Properties.ETag;
        }
Beispiel #2
0
 public AzureAcmeAccountStore(
     IOptions <AzureAcmeAccountStoreOptions> options,
     IStorageBlob <StorageBlobOptions> storage)
 {
     _options = options.Value ?? throw new ArgumentNullException(nameof(options));
     _storage = storage ?? throw new ArgumentNullException(nameof(storage));
 }
Beispiel #3
0
 public ObjectValueBinder(IStorageBlob blob, WatchableCloudBlobStream stream,
                          ICloudBlobStreamBinder <TValue> objectBinder)
 {
     _blob         = blob;
     _stream       = stream;
     _objectBinder = objectBinder;
 }
        private async Task NotifyRegistrationsAsync(IStorageBlob blob, ICollection <IStorageBlob> failedNotifications,
                                                    CancellationToken cancellationToken)
        {
            IStorageBlobContainer container = blob.Container;
            ContainerScanInfo     containerScanInfo;

            // Blob written notifications are host-wide, so filter out things that aren't in the container list.
            if (!_scanInfo.TryGetValue(container, out containerScanInfo))
            {
                return;
            }

            foreach (ITriggerExecutor <IStorageBlob> registration in containerScanInfo.Registrations)
            {
                cancellationToken.ThrowIfCancellationRequested();

                FunctionResult result = await registration.ExecuteAsync(blob, cancellationToken);

                if (!result.Succeeded)
                {
                    // If notification failed, try again on the next iteration.
                    failedNotifications.Add(blob);
                }
            }
        }
Beispiel #5
0
        // Populate the List<> with blob logs for the given prefix.
        // http://blogs.msdn.com/b/windowsazurestorage/archive/2011/08/03/windows-azure-storage-logging-using-logs-to-track-storage-requests.aspx
        private static async Task GetLogsWithPrefixAsync(List <IStorageBlob> selectedLogs, IStorageBlobClient blobClient,
                                                         string prefix, CancellationToken cancellationToken)
        {
            // List the blobs using the prefix
            IEnumerable <IStorageListBlobItem> blobs = await blobClient.ListBlobsAsync(prefix,
                                                                                       useFlatBlobListing : true, blobListingDetails : BlobListingDetails.Metadata,
                                                                                       cancellationToken : cancellationToken);

            // iterate through each blob and figure the start and end times in the metadata
            // Type cast to IStorageBlob is safe due to useFlatBlobListing: true above.
            foreach (IStorageBlob item in blobs)
            {
                IStorageBlob log = item as IStorageBlob;
                if (log != null)
                {
                    // we will exclude the file if the file does not have log entries in the interested time range.
                    string logType   = log.Metadata[LogType];
                    bool   hasWrites = logType.Contains("write");

                    if (hasWrites)
                    {
                        selectedLogs.Add(log);
                    }
                }
            }
        }
        public static async Task <bool> TryFetchAttributesAsync(this IStorageBlob blob,
                                                                CancellationToken cancellationToken)
        {
            if (blob == null)
            {
                throw new ArgumentNullException("blob");
            }

            try
            {
                await blob.FetchAttributesAsync(cancellationToken);

                return(true);
            }
            catch (StorageException exception)
            {
                // Remember specific error codes are not available for Fetch (HEAD request).

                if (exception.IsNotFound())
                {
                    return(false);
                }
                else if (exception.IsOk())
                {
                    // If the blob type is incorrect (block vs. page) a 200 OK is returned but the SDK throws an
                    // exception.
                    return(false);
                }
                else
                {
                    throw;
                }
            }
        }
Beispiel #7
0
            public async Task <IValueProvider> BindAsync(IStorageBlob blob, ValueBindingContext context)
            {
                WatchableCloudBlobStream watchableStream = await WriteBlobArgumentBinding.BindStreamAsync(blob,
                                                                                                          context, _blobWrittenWatcherGetter.Value);

                return(new WriteStreamValueBinder(blob, watchableStream));
            }
Beispiel #8
0
        public void ExecuteAsync_IfLeasedReceiptBecameCompleted_ReleasesLeaseAndReturnsTrue()
        {
            // Arrange
            IStorageBlob    blob       = CreateBlobReference();
            IBlobPathSource input      = CreateBlobPath(blob);
            IBlobETagReader eTagReader = CreateStubETagReader("ETag");

            Mock <IBlobReceiptManager> mock = CreateReceiptManagerReferenceMock();
            int calls = 0;

            mock.Setup(m => m.TryReadAsync(It.IsAny <IStorageBlockBlob>(), It.IsAny <CancellationToken>()))
            .Returns(() =>
            {
                int call = calls++;
                return(Task.FromResult(call == 0 ? BlobReceipt.Incomplete : BlobReceipt.Complete));
            });
            mock.Setup(m => m.TryAcquireLeaseAsync(It.IsAny <IStorageBlockBlob>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult("LeaseId"));
            mock.Setup(m => m.ReleaseLeaseAsync(It.IsAny <IStorageBlockBlob>(), It.IsAny <string>(),
                                                It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(0))
            .Verifiable();
            IBlobReceiptManager receiptManager = mock.Object;

            ITriggerExecutor <IStorageBlob> product = CreateProductUnderTest(input, eTagReader, receiptManager);

            // Act
            Task <bool> task = product.ExecuteAsync(blob, CancellationToken.None);

            // Assert
            task.WaitUntilCompleted();
            mock.Verify();
            Assert.True(task.Result);
        }
Beispiel #9
0
        public void ExecuteAsync_IfIncompleteBlobReceiptExists_TriesToAcquireLease()
        {
            // Arrange
            IStorageBlob    blob       = CreateBlobReference();
            IBlobPathSource input      = CreateBlobPath(blob);
            IBlobETagReader eTagReader = CreateStubETagReader("ETag");

            Mock <IBlobReceiptManager> mock = CreateReceiptManagerReferenceMock();

            mock.Setup(m => m.TryReadAsync(It.IsAny <IStorageBlockBlob>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(BlobReceipt.Incomplete));
            mock.Setup(m => m.TryAcquireLeaseAsync(It.IsAny <IStorageBlockBlob>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult <string>(null))
            .Verifiable();
            IBlobReceiptManager receiptManager = mock.Object;

            ITriggerExecutor <IStorageBlob> product = CreateProductUnderTest(input, eTagReader, receiptManager);

            // Act
            Task <bool> task = product.ExecuteAsync(blob, CancellationToken.None);

            // Assert
            task.GetAwaiter().GetResult();
            mock.Verify();
        }
Beispiel #10
0
        public async Task <string> GetETagAsync(IStorageBlob blob, CancellationToken cancellationToken)
        {
            try
            {
                await blob.FetchAttributesAsync(cancellationToken);
            }
            catch (StorageException exception)
            {
                // Note that specific exception codes are not available for FetchAttributes, which makes a HEAD request.

                if (exception.IsNotFound())
                {
                    // Blob does not exist.
                    return(null);
                }
                else if (exception.IsOk())
                {
                    // If the blob type is incorrect (block vs. page) a 200 OK is returned but the SDK throws an
                    // exception.
                    return(null);
                }
                else
                {
                    throw;
                }
            }

            return(blob.Properties.ETag);
        }
 public AzureCertificateStore(
     IOptions <AzureCertificateStoreOptions> options,
     IStorageBlob <StorageBlobOptions> storage)
 {
     _options = options.Value;
     _storage = storage ?? throw new ArgumentNullException(nameof(storage));
 }
Beispiel #12
0
        public void ExecuteAsync_IfTryAcquireLeaseSucceeds_ReadsLatestReceipt()
        {
            // Arrange
            IStorageBlob    blob       = CreateBlobReference();
            IBlobPathSource input      = CreateBlobPath(blob);
            IBlobETagReader eTagReader = CreateStubETagReader("ETag");

            Mock <IBlobReceiptManager> mock = CreateReceiptManagerReferenceMock();
            int calls = 0;

            mock.Setup(m => m.TryReadAsync(It.IsAny <IStorageBlockBlob>(), It.IsAny <CancellationToken>()))
            .Returns(() =>
            {
                return(Task.FromResult(calls++ == 0 ? BlobReceipt.Incomplete : BlobReceipt.Complete));
            });
            mock.Setup(m => m.TryAcquireLeaseAsync(It.IsAny <IStorageBlockBlob>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult("LeaseId"));
            mock.Setup(m => m.ReleaseLeaseAsync(It.IsAny <IStorageBlockBlob>(), It.IsAny <string>(),
                                                It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(0));
            IBlobReceiptManager receiptManager = mock.Object;

            ITriggerExecutor <IStorageBlob> product = CreateProductUnderTest(input, eTagReader, receiptManager);

            // Act
            Task <bool> task = product.ExecuteAsync(blob, CancellationToken.None);

            // Assert
            task.GetAwaiter().GetResult();
            Assert.Equal(2, calls);
        }
Beispiel #13
0
 public AzureBlobStorageHealthCheck(
     IStorageBlob <StorageBlobOptions> storageBlob,
     IOptionsMonitor <StorageBlobOptions> storageBlobOptionsMonitor,
     ILogger <AzureBlobStorageHealthCheck> logger)
 {
     _storageBlob = storageBlob ?? throw new ArgumentNullException(nameof(storageBlob));
     _storageBlobOptionsMonitor = storageBlobOptionsMonitor ?? throw new ArgumentNullException(nameof(storageBlobOptionsMonitor));
     _logger = logger ?? throw new ArgumentNullException(nameof(logger));
 }
        public static BlobPath ToBlobPath(this IStorageBlob blob)
        {
            if (blob == null)
            {
                throw new ArgumentNullException("blob");
            }

            return(new BlobPath(blob.Container.Name, blob.Name));
        }
        public static bool Exists(this IStorageBlob blob)
        {
            if (blob == null)
            {
                throw new ArgumentNullException("blob");
            }

            return(blob.ExistsAsync(CancellationToken.None).GetAwaiter().GetResult());
        }
            public async Task<IValueProvider> BindAsync(IStorageBlob blob, ValueBindingContext context)
            {
                WatchableCloudBlobStream watchableStream = await WriteBlobArgumentBinding.BindStreamAsync(blob,
                    context, _blobWrittenWatcherGetter.Value);
                const int DefaultBufferSize = 1024;

                TextWriter writer = new StreamWriter(watchableStream, Encoding.UTF8, DefaultBufferSize, leaveOpen: true);

                return new TextWriterValueBinder(blob, watchableStream, writer);
            }
Beispiel #17
0
        public void ExecuteAsync_IfLeasedIncompleteReceipt_EnqueuesMessageMarksCompletedReleasesLeaseAndReturnsTrue()
        {
            // Arrange
            string          expectedFunctionId = "FunctionId";
            string          expectedETag       = "ETag";
            IStorageBlob    blob       = CreateBlobReference("container", "blob");
            IBlobPathSource input      = CreateBlobPath(blob);
            IBlobETagReader eTagReader = CreateStubETagReader(expectedETag);

            Mock <IBlobReceiptManager> managerMock = CreateReceiptManagerReferenceMock();

            managerMock
            .Setup(m => m.TryReadAsync(It.IsAny <IStorageBlockBlob>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(BlobReceipt.Incomplete));
            managerMock
            .Setup(m => m.TryAcquireLeaseAsync(It.IsAny <IStorageBlockBlob>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult("LeaseId"));
            managerMock
            .Setup(m => m.MarkCompletedAsync(It.IsAny <IStorageBlockBlob>(), It.IsAny <string>(),
                                             It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(0))
            .Verifiable();
            managerMock
            .Setup(m => m.ReleaseLeaseAsync(It.IsAny <IStorageBlockBlob>(), It.IsAny <string>(),
                                            It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(0))
            .Verifiable();
            IBlobReceiptManager receiptManager = managerMock.Object;

            Mock <IBlobTriggerQueueWriter> queueWriterMock = new Mock <IBlobTriggerQueueWriter>(MockBehavior.Strict);

            queueWriterMock
            .Setup(w => w.EnqueueAsync(It.IsAny <BlobTriggerMessage>(), It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult(0));
            IBlobTriggerQueueWriter queueWriter = queueWriterMock.Object;

            ITriggerExecutor <IStorageBlob> product = CreateProductUnderTest(expectedFunctionId, input, eTagReader,
                                                                             receiptManager, queueWriter);

            // Act
            Task <bool> task = product.ExecuteAsync(blob, CancellationToken.None);

            // Assert
            task.WaitUntilCompleted();
            queueWriterMock
            .Verify(
                w => w.EnqueueAsync(It.Is <BlobTriggerMessage>(m =>
                                                               m != null && m.FunctionId == expectedFunctionId && m.BlobType == StorageBlobType.BlockBlob &&
                                                               m.BlobName == blob.Name && m.ContainerName == blob.Container.Name && m.ETag == expectedETag),
                                    It.IsAny <CancellationToken>()),
                Times.Once());
            managerMock.Verify();
            Assert.True(task.Result);
        }
        private static async Task <DateTime?> GetBlobModifiedUtcTimeAsync(IStorageBlob blob, CancellationToken cancellationToken)
        {
            if (!await blob.ExistsAsync(cancellationToken))
            {
                return(null); // no blob, no time.
            }

            var lastModified = blob.Properties.LastModified;

            return(lastModified.HasValue ? (DateTime?)lastModified.Value.UtcDateTime : null);
        }
Beispiel #19
0
        public BlobValueProvider(IStorageBlob blob, object value, Type valueType)
        {
            if (value != null && !valueType.IsAssignableFrom(value.GetType()))
            {
                throw new InvalidOperationException("value is not of the correct type.");
            }

            _blob      = blob;
            _value     = value;
            _valueType = valueType;
        }
            public async Task <IValueProvider> BindAsync(IStorageBlob blob, ValueBindingContext context)
            {
                WatchableCloudBlobStream watchableStream = await WriteBlobArgumentBinding.BindStreamAsync(blob,
                                                                                                          context, _blobWrittenWatcherGetter.Value);

                const int DefaultBufferSize = 1024;

                TextWriter writer = new StreamWriter(watchableStream, Encoding.UTF8, DefaultBufferSize, leaveOpen: true);

                return(new TextWriterValueBinder(blob, watchableStream, writer));
            }
            public async Task<IValueProvider> BindAsync(IStorageBlob blob, ValueBindingContext context)
            {
                WatchableReadStream watchableStream = await ReadBlobArgumentBinding.TryBindStreamAsync(blob, context);
                if (watchableStream == null)
                {
                    return BlobValueProvider.CreateWithNull<Stream>(blob);
                }

                return new BlobWatchableDisposableValueProvider(blob, watchableStream, typeof(Stream),
                    watcher: watchableStream, disposable: watchableStream);
            }
Beispiel #22
0
            public async Task <IValueProvider> BindAsync(IStorageBlob blob, ValueBindingContext context)
            {
                WatchableReadStream watchableStream = await ReadBlobArgumentBinding.TryBindStreamAsync(blob, context);

                if (watchableStream == null)
                {
                    return(BlobValueProvider.CreateWithNull <Stream>(blob));
                }

                return(new BlobWatchableDisposableValueProvider(blob, watchableStream, typeof(Stream),
                                                                watcher: watchableStream, disposable: watchableStream));
            }
        public AzureStorageModelLoader(
            IStorageBlob <StorageBlobOptions> storageBlob,
            ILogger <AzureStorageModelLoader> logger)
        {
            _logger      = logger ?? throw new ArgumentNullException(nameof(logger));
            _storageBlob = storageBlob ?? throw new ArgumentNullException(nameof(storageBlob));

            LoadFunc = (options, cancellationToken) => LoadModelResult(options, cancellationToken);

            _reloadToken = new ReloadToken();
            _stopping    = new CancellationTokenSource();
        }
        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());
                }
        }
        public BlobWatchableDisposableValueProvider(IStorageBlob blob, object value, Type valueType, IWatcher watcher,
            IDisposable disposable)
        {
            if (value != null && !valueType.IsAssignableFrom(value.GetType()))
            {
                throw new InvalidOperationException("value is not of the correct type.");
            }

            _blob = blob;
            _value = value;
            _valueType = valueType;
            _watcher = watcher;
            _disposable = disposable;
        }
        public BlobWatchableDisposableValueProvider(IStorageBlob blob, object value, Type valueType, IWatcher watcher,
                                                    IDisposable disposable)
        {
            if (value != null && !valueType.IsAssignableFrom(value.GetType()))
            {
                throw new InvalidOperationException("value is not of the correct type.");
            }

            _blob       = blob;
            _value      = value;
            _valueType  = valueType;
            _watcher    = watcher;
            _disposable = disposable;
        }
Beispiel #27
0
        public void ExecuteAsync_IfBlobDoesNotExist_ReturnsTrue()
        {
            // Arrange
            IStorageBlob    blob       = CreateBlobReference();
            IBlobPathSource input      = CreateBlobPath(blob);
            IBlobETagReader eTagReader = CreateStubETagReader(null);

            ITriggerExecutor <IStorageBlob> product = CreateProductUnderTest(input, eTagReader);

            // Act
            Task <bool> task = product.ExecuteAsync(blob, CancellationToken.None);

            // Assert
            Assert.True(task.Result);
        }
        public void ExecuteAsync_IfCompletedBlobReceiptExists_ReturnsSuccessfulResult()
        {
            // Arrange
            IStorageBlob        blob           = CreateBlobReference();
            IBlobPathSource     input          = CreateBlobPath(blob);
            IBlobETagReader     eTagReader     = CreateStubETagReader("ETag");
            IBlobReceiptManager receiptManager = CreateCompletedReceiptManager();

            ITriggerExecutor <IStorageBlob> product = CreateProductUnderTest(input, eTagReader, receiptManager);

            // Act
            Task <FunctionResult> task = product.ExecuteAsync(blob, CancellationToken.None);

            // Assert
            Assert.True(task.Result.Succeeded);
        }
        public static async Task<WatchableCloudBlobStream> BindStreamAsync(IStorageBlob blob,
            ValueBindingContext context, IBlobWrittenWatcher blobWrittenWatcher)
        {
            IStorageBlockBlob blockBlob = blob as IStorageBlockBlob;

            if (blockBlob == null)
            {
                throw new InvalidOperationException("Cannot bind a page blob using an out string.");
            }

            BlobCausalityManager.SetWriter(blob.Metadata, context.FunctionInstanceId);

            CloudBlobStream rawStream = await blockBlob.OpenWriteAsync(context.CancellationToken);
            IBlobCommitedAction committedAction = new BlobCommittedAction(blob, blobWrittenWatcher);
            return new WatchableCloudBlobStream(rawStream, committedAction);
        }
Beispiel #30
0
        public async Task <IValueProvider> BindAsync(BindingContext context)
        {
            BlobPath boundPath = _path.Bind(context.BindingData);
            IStorageBlobContainer container = _client.GetContainerReference(boundPath.ContainerName);

            if (_argumentBinding.Access != FileAccess.Read)
            {
                await container.CreateIfNotExistsAsync(context.CancellationToken);
            }

            Type         argumentType = _argumentBinding.ValueType;
            string       blobName     = boundPath.BlobName;
            IStorageBlob blob         = await container.GetBlobReferenceForArgumentTypeAsync(blobName, argumentType, context.CancellationToken);

            return(await BindBlobAsync(blob, context.ValueContext));
        }
Beispiel #31
0
        public AzureStorageMSModelLoader(
            IOptions <MLOptions> contextOptions,
            IStorageBlob <StorageBlobOptions> storageBlob,
            ILogger <AzureStorageMSModelLoader> logger)
        {
            _logger = logger ?? throw new ArgumentNullException(nameof(logger));

            if (contextOptions.Value?.MLContext == null)
            {
                throw new ArgumentNullException(nameof(contextOptions));
            }

            _mlContext   = contextOptions.Value.MLContext;
            _reloadToken = new ModelReloadToken();
            _stopping    = new CancellationTokenSource();
            _storageBlob = storageBlob ?? throw new ArgumentNullException(nameof(storageBlob));
        }
Beispiel #32
0
 public static Task <IStorageBlob> GetBlobReferenceForArgumentTypeAsync(this IStorageBlobContainer container,
                                                                        string blobName, Type argumentType, CancellationToken cancellationToken)
 {
     if (argumentType == typeof(CloudBlockBlob))
     {
         IStorageBlob blob = container.GetBlockBlobReference(blobName);
         return(Task.FromResult(blob));
     }
     else if (argumentType == typeof(CloudPageBlob))
     {
         IStorageBlob blob = container.GetPageBlobReference(blobName);
         return(Task.FromResult(blob));
     }
     else
     {
         return(GetExistingOrNewBlockBlobReferenceAsync(container, blobName, cancellationToken));
     }
 }
        public static async Task <WatchableCloudBlobStream> BindStreamAsync(IStorageBlob blob,
                                                                            ValueBindingContext context, IBlobWrittenWatcher blobWrittenWatcher)
        {
            IStorageBlockBlob blockBlob = blob as IStorageBlockBlob;

            if (blockBlob == null)
            {
                throw new InvalidOperationException("Cannot bind a page blob using an out string.");
            }

            BlobCausalityManager.SetWriter(blob.Metadata, context.FunctionInstanceId);

            CloudBlobStream rawStream = await blockBlob.OpenWriteAsync(context.CancellationToken);

            IBlobCommitedAction committedAction = new BlobCommittedAction(blob, blobWrittenWatcher);

            return(new WatchableCloudBlobStream(rawStream, committedAction));
        }
        private IReadOnlyDictionary <string, object> CreateBindingData(IStorageBlob value)
        {
            Dictionary <string, object> bindingData = new Dictionary <string, object>(StringComparer.OrdinalIgnoreCase);

            bindingData.Add("BlobTrigger", value.GetBlobPath());

            IReadOnlyDictionary <string, object> bindingDataFromPath = _path.CreateBindingData(value.ToBlobPath());

            if (bindingDataFromPath != null)
            {
                foreach (KeyValuePair <string, object> item in bindingDataFromPath)
                {
                    // In case of conflict, binding data from the value type overrides the built-in binding data above.
                    bindingData[item.Key] = item.Value;
                }
            }
            return(bindingData);
        }
 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;
     }
 }
Beispiel #36
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);
        }
        /// <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 async Task<IValueProvider> BindAsync(IStorageBlob blob, ValueBindingContext context)
            {
                WatchableReadStream watchableStream = await ReadBlobArgumentBinding.TryBindStreamAsync(blob, context);
                if (watchableStream == null)
                {
                    return BlobValueProvider.CreateWithNull<string>(blob);
                }

                string value;
                ParameterLog status;

                using (watchableStream)
                using (TextReader reader = ReadBlobArgumentBinding.CreateTextReader(watchableStream))
                {
                    context.CancellationToken.ThrowIfCancellationRequested();
                    value = await reader.ReadToEndAsync();
                    status = watchableStream.GetStatus();
                }

                return new BlobWatchableValueProvider(blob, value, typeof(string), new ImmutableWatcher(status));
            }
        public static async Task<Guid?> GetWriterAsync(IStorageBlob blob, CancellationToken cancellationToken)
        {
            if (!await blob.TryFetchAttributesAsync(cancellationToken))
            {
                return null;
            }

            if (!blob.Metadata.ContainsKey(BlobMetadataKeys.ParentId))
            {
                return null;
            }

            string val = blob.Metadata[BlobMetadataKeys.ParentId];
            Guid result;
            if (Guid.TryParse(val, out result))
            {
                return result;
            }

            return null;
        }
            public async Task<IValueProvider> BindAsync(IStorageBlob blob, ValueBindingContext context)
            {
                WatchableReadStream watchableStream = await ReadBlobArgumentBinding.TryBindStreamAsync(blob, context);
                if (watchableStream == null)
                {
                    return BlobValueProvider.CreateWithNull<byte[]>(blob);
                }

                byte[] value;
                ParameterLog status;

                using (watchableStream)
                using (MemoryStream outputStream = new MemoryStream())
                {
                    const int DefaultBufferSize = 4096;
                    await watchableStream.CopyToAsync(outputStream, DefaultBufferSize);
                    value = outputStream.ToArray();
                    status = watchableStream.GetStatus();
                }

                return new BlobWatchableValueProvider(blob, value, typeof(byte[]), new ImmutableWatcher(status));
            }
        private static async Task<DateTime?> GetBlobModifiedUtcTimeAsync(IStorageBlob blob, CancellationToken cancellationToken)
        {
            if (!await blob.ExistsAsync(cancellationToken))
            {
                return null; // no blob, no time.
            }

            var lastModified = blob.Properties.LastModified;
            return lastModified.HasValue ? (DateTime?)lastModified.Value.UtcDateTime : null;
        }
 public Task<Guid?> GetWriterAsync(IStorageBlob blob, CancellationToken cancellationToken)
 {
     return BlobCausalityManager.GetWriterAsync(blob, cancellationToken);
 }
 public TextWriterValueBinder(IStorageBlob blob, WatchableCloudBlobStream stream, TextWriter value)
 {
     _blob = blob;
     _stream = stream;
     _value = value;
 }
 public WriteStreamValueBinder(IStorageBlob blob, WatchableCloudBlobStream stream)
 {
     _blob = blob;
     _stream = stream;
 }
 public BlobCommittedAction(IStorageBlob blob, IBlobWrittenWatcher blobWrittenWatcher)
 {
     _blob = blob;
     _blobWrittenWatcher = blobWrittenWatcher;
 }
Beispiel #47
0
        private async Task NotifyRegistrationsAsync(IStorageBlob blob, ICollection<IStorageBlob> failedNotifications, CancellationToken cancellationToken)
        {
            IStorageBlobContainer container = blob.Container;
            ContainerScanInfo containerScanInfo;

            // Blob written notifications are host-wide, so filter out things that aren't in the container list.
            if (!_scanInfo.TryGetValue(container, out containerScanInfo))
            {
                return;
            }

            foreach (ITriggerExecutor<IStorageBlob> registration in containerScanInfo.Registrations)
            {
                cancellationToken.ThrowIfCancellationRequested();

                FunctionResult result = await registration.ExecuteAsync(blob, cancellationToken);
                if (!result.Succeeded)
                {
                    // If notification failed, try again on the next iteration.
                    failedNotifications.Add(blob);
                }
            }
        }
 private Task<IValueProvider> BindBlobAsync(IStorageBlob value, ValueBindingContext context)
 {
     return _argumentBinding.BindAsync(value, context);
 }
        private IReadOnlyDictionary<string, object> CreateBindingData(IStorageBlob value)
        {
            Dictionary<string, object> bindingData = new Dictionary<string, object>(StringComparer.OrdinalIgnoreCase);
            bindingData.Add("BlobTrigger", value.GetBlobPath());

            IReadOnlyDictionary<string, object> bindingDataFromPath = _path.CreateBindingData(value.ToBlobPath());

            if (bindingDataFromPath != null)
            {
                foreach (KeyValuePair<string, object> item in bindingDataFromPath)
                {
                    // In case of conflict, binding data from the value type overrides the built-in binding data above.
                    bindingData[item.Key] = item.Value;
                }
            }
            return bindingData;
        }
 private static IBlobPathSource CreateBlobPath(IStorageBlob blob)
 {
     return new FixedBlobPathSource(blob.ToBlobPath());
 }
 public void Notify(IStorageBlob blobWritten)
 {
     ThrowIfDisposed();
     _blobsFoundFromScanOrNotification.Enqueue(blobWritten);
 }
        private async Task NotifyRegistrationsAsync(IStorageBlob blob, CancellationToken cancellationToken)
        {
            IStorageBlobContainer container = blob.Container;

            // Log listening is client-wide and blob written notifications are host-wide, so filter out things that
            // aren't in the container list.
            if (!_registrations.ContainsKey(container))
            {
                return;
            }

            foreach (ITriggerExecutor<IStorageBlob> registration in _registrations[container])
            {
                cancellationToken.ThrowIfCancellationRequested();

                FunctionResult result = await registration.ExecuteAsync(blob, cancellationToken);
                if (!result.Succeeded)
                {
                    // If notification failed, try again on the next iteration.
                    _blobsFoundFromScanOrNotification.Enqueue(blob);
                }
            }
        }
 public void Notify(IStorageBlob blobWritten)
 {
     _blobWrittenNotifications.Enqueue(blobWritten);
 }
 public async Task<IValueProvider> BindAsync(IStorageBlob blob, ValueBindingContext context)
 {
     WatchableCloudBlobStream watchableStream = await WriteBlobArgumentBinding.BindStreamAsync(blob, context, _blobWrittenWatcherGetter.Value);
     return new ByteArrayValueBinder(blob, watchableStream);
 }
 public ByteArrayValueBinder(IStorageBlob blob, WatchableCloudBlobStream stream)
 {
     _blob = blob;
     _stream = stream;
 }