コード例 #1
0
        /// <inheritdoc />
        public async Task BeatAsync(CancellationToken cancellationToken)
        {
            bool isContainerNotFoundException = false;

            try
            {
                await _blob.UploadTextAsync(String.Empty, cancellationToken : cancellationToken);

                return;
            }
            catch (StorageException exception)
            {
                if (exception.IsNotFoundContainerNotFound())
                {
                    isContainerNotFoundException = true;
                }
                else
                {
                    throw;
                }
            }

            Debug.Assert(isContainerNotFoundException, "Blob Container was not found");
            await _blob.Container.CreateIfNotExistsAsync(cancellationToken);

            await _blob.UploadTextAsync(String.Empty, cancellationToken : cancellationToken);
        }
コード例 #2
0
        private async Task <bool> TryCreateAsync(IStorageBlockBlob blob, CancellationToken cancellationToken)
        {
            bool isContainerNotFoundException = false;

            try
            {
                await blob.UploadTextAsync(string.Empty, cancellationToken : cancellationToken);

                return(true);
            }
            catch (StorageException exception)
            {
                if (exception.RequestInformation != null)
                {
                    if (exception.RequestInformation.HttpStatusCode == 404)
                    {
                        isContainerNotFoundException = true;
                    }
                    else if (exception.RequestInformation.HttpStatusCode == 409 ||
                             exception.RequestInformation.HttpStatusCode == 412)
                    {
                        // The blob already exists, or is leased by someone else
                        return(false);
                    }
                    else
                    {
                        throw;
                    }
                }
                else
                {
                    throw;
                }
            }

            Debug.Assert(isContainerNotFoundException);
            await blob.Container.CreateIfNotExistsAsync(cancellationToken);

            try
            {
                await blob.UploadTextAsync(string.Empty, cancellationToken : cancellationToken);

                return(true);
            }
            catch (StorageException exception)
            {
                if (exception.RequestInformation != null &&
                    (exception.RequestInformation.HttpStatusCode == 409 || exception.RequestInformation.HttpStatusCode == 412))
                {
                    // The blob already exists, or is leased by someone else
                    return(false);
                }
                else
                {
                    throw;
                }
            }
        }
コード例 #3
0
        public async Task <bool> TryExecuteAsync(CancellationToken cancellationToken)
        {
            Dictionary <string, ParameterLog> logs = new Dictionary <string, ParameterLog>();

            AddLogs(_watches, logs);
            string content = JsonConvert.SerializeObject(logs, JsonSerialization.Settings);

            try
            {
                if (_lastContent == content)
                {
                    // If it hasn't change, then don't re upload stale content.
                    return(true);
                }

                _lastContent = content;
                await _parameterLogBlob.UploadTextAsync(content, cancellationToken : cancellationToken);

                return(true);
            }
            catch (OperationCanceledException)
            {
                throw;
            }
            catch (Exception e)
            {
                // Not fatal if we can't update parameter status.
                // But at least log what happened for diagnostics in case it's an infrastructure bug.
                string msg = "---- Parameter status update failed ----";
                _logger?.LogError(0, e, msg);
                return(false);
            }
        }
コード例 #4
0
        public static void UploadText(this IStorageBlockBlob blob, string content)
        {
            if (blob == null)
            {
                throw new ArgumentNullException("blob");
            }

            blob.UploadTextAsync(content).GetAwaiter().GetResult();
        }
コード例 #5
0
        /// <inheritdoc />
        public async Task <string> EnqueueAsync(T message, CancellationToken cancellationToken)
        {
            await _blobContainer.CreateIfNotExistsAsync(cancellationToken);

            string            blobName = BlobNames.GetConflictFreeDateTimeBasedBlobName();
            IStorageBlockBlob blob     = _blobContainer.GetBlockBlobReference(blobName);

            message.AddMetadata(blob.Metadata);
            string messageBody = JsonConvert.SerializeObject(message, JsonSerialization.Settings);
            await blob.UploadTextAsync(messageBody, cancellationToken : cancellationToken);

            return(blobName);
        }
コード例 #6
0
        public async Task UpdateLatestScanAsync(string storageAccountName, string containerName, DateTime latestScan)
        {
            string   scanInfoLine;
            ScanInfo scanInfo = new ScanInfo
            {
                LatestScan = latestScan
            };

            using (StringWriter stringWriter = new StringWriter())
            {
                _serializer.Serialize(stringWriter, scanInfo);
                scanInfoLine = stringWriter.ToString();
            }

            try
            {
                IStorageBlockBlob scanInfoBlob = GetScanInfoBlobReference(storageAccountName, containerName);
                await scanInfoBlob.UploadTextAsync(scanInfoLine);
            }
            catch
            {
                // best effort
            }
        }
コード例 #7
0
 private static Task UploadTextAsync(IStorageBlockBlob outputBlob, string contents, CancellationToken cancellationToken)
 {
     return outputBlob.UploadTextAsync(contents, cancellationToken: cancellationToken);
 }
コード例 #8
0
 private static Task UploadTextAsync(IStorageBlockBlob outputBlob, string contents, CancellationToken cancellationToken)
 {
     return(outputBlob.UploadTextAsync(contents, cancellationToken: cancellationToken));
 }
コード例 #9
0
        private static async Task<bool> TryInitializeIdAsync(IStorageBlockBlob blob, Guid hostId,
            CancellationToken cancellationToken)
        {
            string text = hostId.ToString("N");
            AccessCondition accessCondition = new AccessCondition { IfNoneMatchETag = "*" };
            bool failedWithContainerNotFoundException = false;

            try
            {
                await blob.UploadTextAsync(text,
                    encoding: null,
                    accessCondition: accessCondition,
                    options: null,
                    operationContext: null,
                    cancellationToken: cancellationToken);
                return true;
            }
            catch (StorageException exception)
            {
                if (exception.IsPreconditionFailed() || exception.IsConflict())
                {
                    return false;
                }
                else if (exception.IsNotFoundContainerNotFound())
                {
                    failedWithContainerNotFoundException = true;
                }
                else
                {
                    throw;
                }
            }

            Debug.Assert(failedWithContainerNotFoundException);

            await blob.Container.CreateIfNotExistsAsync(cancellationToken);

            try
            {
                await blob.UploadTextAsync(text,
                    encoding: null,
                    accessCondition: accessCondition,
                    options: null,
                    operationContext: null,
                    cancellationToken: cancellationToken);
                return true;
            }
            catch (StorageException retryException)
            {
                if (retryException.IsPreconditionFailed() || retryException.IsConflict())
                {
                    return false;
                }
                else
                {
                    throw;
                }
            }
        }
コード例 #10
0
        private async Task<bool> TryCreateAsync(IStorageBlockBlob blob, CancellationToken cancellationToken)
        {
            AccessCondition accessCondition = new AccessCondition { IfNoneMatchETag = "*" };
            bool isContainerNotFoundException = false;

            try
            {
                await blob.UploadTextAsync(String.Empty,
                    encoding: null,
                    accessCondition: accessCondition,
                    options: null,
                    operationContext: null,
                    cancellationToken: cancellationToken);
                return true;
            }
            catch (StorageException exception)
            {
                if (exception.IsNotFoundContainerNotFound())
                {
                    isContainerNotFoundException = true;
                }
                else if (exception.IsConflictBlobAlreadyExists())
                {
                    return false;
                }
                else if (exception.IsPreconditionFailedLeaseIdMissing())
                {
                    return false;
                }
                else
                {
                    throw;
                }
            }

            Debug.Assert(isContainerNotFoundException);
            await blob.Container.CreateIfNotExistsAsync(cancellationToken);

            try
            {
                await blob.UploadTextAsync(String.Empty,
                    encoding: null,
                    accessCondition: accessCondition,
                    options: null,
                    operationContext: null,
                    cancellationToken: cancellationToken);
                return true;
            }
            catch (StorageException exception)
            {
                if (exception.IsConflictBlobAlreadyExists())
                {
                    return false;
                }
                else if (exception.IsPreconditionFailedLeaseIdMissing())
                {
                    return false;
                }
                else
                {
                    throw;
                }
            }
        }
コード例 #11
0
ファイル: BlobReceiptManager.cs プロジェクト: sravan251/Azure
        public async Task <bool> TryCreateAsync(IStorageBlockBlob blob, CancellationToken cancellationToken)
        {
            BlobReceipt.Incomplete.ToMetadata(blob.Metadata);
            AccessCondition accessCondition = new AccessCondition {
                IfNoneMatchETag = "*"
            };
            bool isContainerNotFoundException = false;

            try
            {
                await blob.UploadTextAsync(String.Empty,
                                           encoding : null,
                                           accessCondition : accessCondition,
                                           options : null,
                                           operationContext : null,
                                           cancellationToken : cancellationToken);

                return(true);
            }
            catch (StorageException exception)
            {
                if (exception.IsNotFoundContainerNotFound())
                {
                    isContainerNotFoundException = true;
                }
                else if (exception.IsConflictBlobAlreadyExists())
                {
                    return(false);
                }
                else if (exception.IsPreconditionFailedLeaseIdMissing())
                {
                    return(false);
                }
                else
                {
                    throw;
                }
            }

            Debug.Assert(isContainerNotFoundException);
            await blob.Container.CreateIfNotExistsAsync(cancellationToken);

            try
            {
                await blob.UploadTextAsync(String.Empty,
                                           encoding : null,
                                           accessCondition : accessCondition,
                                           options : null,
                                           operationContext : null,
                                           cancellationToken : cancellationToken);

                return(true);
            }
            catch (StorageException exception)
            {
                if (exception.IsConflictBlobAlreadyExists())
                {
                    return(false);
                }
                else if (exception.IsPreconditionFailedLeaseIdMissing())
                {
                    return(false);
                }
                else
                {
                    throw;
                }
            }
        }
コード例 #12
0
        private static async Task <bool> TryCreateAsync(IStorageBlockBlob blob, CancellationToken cancellationToken)
        {
            bool isContainerNotFoundException = false;

            try
            {
                await blob.UploadTextAsync(string.Empty, cancellationToken : cancellationToken);

                return(true);
            }
            catch (StorageException exception)
            {
                if (exception.RequestInformation != null)
                {
                    if (exception.RequestInformation.HttpStatusCode == 404)
                    {
                        isContainerNotFoundException = true;
                    }
                    else if (exception.RequestInformation.HttpStatusCode == 409 ||
                             exception.RequestInformation.HttpStatusCode == 412)
                    {
                        // The blob already exists, or is leased by someone else
                        return(false);
                    }
                    else
                    {
                        throw;
                    }
                }
                else
                {
                    throw;
                }
            }

            Debug.Assert(isContainerNotFoundException);

            var container = blob.Container;

            try
            {
                await container.CreateIfNotExistsAsync(cancellationToken);
            }
            catch (StorageException exc)
                when(exc.RequestInformation.HttpStatusCode == 409 && string.Compare("ContainerBeingDeleted", exc.RequestInformation.ExtendedErrorInformation?.ErrorCode) == 0)
                {
                    throw new StorageException("The host container is pending deletion and currently inaccessible.");
                }

            try
            {
                await blob.UploadTextAsync(string.Empty, cancellationToken : cancellationToken);

                return(true);
            }
            catch (StorageException exception)
            {
                if (exception.RequestInformation != null &&
                    (exception.RequestInformation.HttpStatusCode == 409 || exception.RequestInformation.HttpStatusCode == 412))
                {
                    // The blob already exists, or is leased by someone else
                    return(false);
                }
                else
                {
                    throw;
                }
            }
        }
コード例 #13
0
        private async Task<bool> TryCreateAsync(IStorageBlockBlob blob, CancellationToken cancellationToken)
        {
            bool isContainerNotFoundException = false;

            try
            {
                await blob.UploadTextAsync(string.Empty, cancellationToken: cancellationToken);
                return true;
            }
            catch (StorageException exception)
            {
                if (exception.RequestInformation != null)
                {
                    if (exception.RequestInformation.HttpStatusCode == 404)
                    {
                        isContainerNotFoundException = true;
                    }
                    else if (exception.RequestInformation.HttpStatusCode == 409 || 
                             exception.RequestInformation.HttpStatusCode == 412)
                    {
                        // The blob already exists, or is leased by someone else
                        return false;
                    }
                    else
                    {
                        throw;
                    }
                }
                else
                {
                    throw;
                }
            }

            Debug.Assert(isContainerNotFoundException);
            await blob.Container.CreateIfNotExistsAsync(cancellationToken);

            try
            {
                await blob.UploadTextAsync(string.Empty, cancellationToken: cancellationToken);
                return true;
            }
            catch (StorageException exception)
            {
                if (exception.RequestInformation != null &&
                    (exception.RequestInformation.HttpStatusCode == 409 || exception.RequestInformation.HttpStatusCode == 412))
                {
                    // The blob already exists, or is leased by someone else
                    return false;
                }
                else
                {
                    throw;
                }
            }
        }
コード例 #14
0
        private static async Task <bool> TryInitializeIdAsync(IStorageBlockBlob blob, Guid hostId,
                                                              CancellationToken cancellationToken)
        {
            string          text            = hostId.ToString("N");
            AccessCondition accessCondition = new AccessCondition {
                IfNoneMatchETag = "*"
            };
            bool failedWithContainerNotFoundException = false;

            try
            {
                await blob.UploadTextAsync(text,
                                           encoding : null,
                                           accessCondition : accessCondition,
                                           options : null,
                                           operationContext : null,
                                           cancellationToken : cancellationToken);

                return(true);
            }
            catch (StorageException exception)
            {
                if (exception.IsPreconditionFailed() || exception.IsConflict())
                {
                    return(false);
                }
                else if (exception.IsNotFoundContainerNotFound())
                {
                    failedWithContainerNotFoundException = true;
                }
                else
                {
                    throw;
                }
            }

            Debug.Assert(failedWithContainerNotFoundException);

            await blob.Container.CreateIfNotExistsAsync(cancellationToken);

            try
            {
                await blob.UploadTextAsync(text,
                                           encoding : null,
                                           accessCondition : accessCondition,
                                           options : null,
                                           operationContext : null,
                                           cancellationToken : cancellationToken);

                return(true);
            }
            catch (StorageException retryException)
            {
                if (retryException.IsPreconditionFailed() || retryException.IsConflict())
                {
                    return(false);
                }
                else
                {
                    throw;
                }
            }
        }