Exemplo n.º 1
0
        public async Task <long> ReadAsync(CancellationToken token)
        {
            try
            {
                // blob exists will actually fetch attributes but suppress error on 404
                var exists = await _blob.ExistsAsync(token);

                if (!exists)
                {
                    return(0);
                }
                var s      = _blob.Metadata[CloudSetup.CheckpointMetadataName];
                var result = long.Parse(s);
                Ensure.ZeroOrGreater("result", result);
                return(result);
            }
            catch (StorageException ex)
            {
                // if forbidden, then we might have an expired SAS token
                if (ex.RequestInformation != null && ex.RequestInformation.HttpStatusCode == 403)
                {
                    throw new ForbiddenException("Can't read blob", ex);
                }
                throw;
            }
        }
        public AzurePageBlobStream(string connString, string databaseName)
        {
            CloudPageBlob blob = GetBlobReference(connString, databaseName);

            if (!blob.ExistsAsync().Result)
            {
                if (WriteDebugLogs)
                {
                    Console.WriteLine("Creating new page blob file " + databaseName);
                }
                blob.CreateAsync(DefaultStreamSize).Wait();
                blob.SetPremiumBlobTierAsync(DefaultBlobTier).Wait();
            }
            Blob = blob;

            for (var i = 0; i < NumberOfLocks; i++)
            {
                Locks[i] = new object();
            }
            for (var i = 0; i < Math.Max(Environment.ProcessorCount * 2, 10); i++)
            {
                Buffers.Add(new byte[PageSize * Pages]);
            }
            if (Length != 0)
            {
                ReadAhead(0);
            }
        }
Exemplo n.º 3
0
        private async Task InitStream(string accountName, string accountKey, string containerName, bool isPublic, bool deleteIfExists)
        {
            _container = Utils.InitContainer(accountName, accountKey, containerName, isPublic);

            // Get a reference to the page blob that will be created.
            _pageBlob = _container.GetPageBlobReference(_streamName);

            BlobRequestOptions options = new BlobRequestOptions();

            options.MaximumExecutionTime = MaxExecutionTime;

            bool exists = await Utils.WrapInRetry(_logger, async() =>
                                                  { return(await _pageBlob.ExistsAsync(options, null)); });

            if (exists)
            {
                if (deleteIfExists)
                {
                    await Utils.WrapInRetry(_logger, async() => { await _pageBlob.DeleteAsync(DeleteSnapshotsOption.None, null, options, null); });
                }
                else
                {
                    throw new ApplicationException("Trying to open existing page blob " + _streamName);
                }
            }

            await Utils.WrapInRetry(_logger, async() => { await _pageBlob.CreateAsync(_streamSize, null, options, null); });

            await Utils.WrapInRetry(_logger, async() =>
            {
                _pageBlob.Metadata["writePosition"] = 0.ToString();
                await _pageBlob.SetMetadataAsync(null, options, null);
                await _pageBlob.SetPropertiesAsync(null, options, null);
            });
        }
Exemplo n.º 4
0
        protected async Task <EnsureBlobExistsResult> EnsureBlobExistsAsync(CloudPageBlob pageBlob, int byteCountToWrite, CancellationToken?cancellationToken = null)
        {
            if (this.VerifiedThatBlobExists)
            {
                return(EnsureBlobExistsResult.AlreadyExisted);
            }

            if (cancellationToken == null)
            {
                cancellationToken = CancellationToken.None;
            }

            await this.EnsureContainerExistsAsync();

            bool blobExists = await pageBlob.ExistsAsync(this.RequestOptions, this.OperationContext, cancellationToken.Value);

            EnsureBlobExistsResult result = blobExists
                ? EnsureBlobExistsResult.AlreadyExisted
                : EnsureBlobExistsResult.Created;

            if (!blobExists)
            {
                long initialByteLength = CalculateBlobLengthThroughEndOfPage(byteCountToWrite);
                await pageBlob.CreateAsync(initialByteLength, CreateAccessCondition(pageBlob), this.RequestOptions, this.OperationContext);
            }

            this.VerifiedThatBlobExists = true;
            return(result);
        }
Exemplo n.º 5
0
        public override void SetParameters(object parameters)
        {
            var jParameters = parameters as JObject;

            if (jParameters == null)
            {
                throw new ArgumentException("Expecting JObject parameters");
            }

            var settings = jParameters.ToObject <PageBlobSettings>();
            var client   = new CloudBlobClient(
                new Uri(string.Format("https://{0}.blob.core.windows.net/", settings.AccountName)),
                new StorageCredentials(settings.AccountName, settings.AccountKey));

            var container = client.GetContainerReference(settings.ContainerName.ToLower());

            container.CreateIfNotExistsAsync().Wait();
            cloudBlob = container.GetPageBlobReference(settings.BlobName);
            if (!cloudBlob.ExistsAsync().Result)
            {
                cloudBlob.CreateAsync(settings.BlobSizeGB * 1024L * 1024L * 1024L).Wait();
            }

            cloudBlob.FetchAttributesAsync().Wait();
        }
Exemplo n.º 6
0
        protected virtual async Task <InternalDownloadToBufferResult> DownloadToBufferAsync(int byteOffset, int bytesToRead, CancellationToken?cancellationToken)
        {
            if (cancellationToken == null)
            {
                cancellationToken = CancellationToken.None;
            }

            this.EnsureInitialized();

            CloudPageBlob pageBlob = this.BlobContainer.GetPageBlobReference(this.BlobPath);

            int pageOffset           = CalculatePageOffset(byteOffset);
            int byteOffsetWithinPage = byteOffset % ByteCountInPage;
            int pageBytesToRead      = CalculatePageBytesToRead(byteOffsetWithinPage, bytesToRead);
            int totalBytesRead       = 0;

            var readBuffer = new byte[pageBytesToRead];

            bool blobExists =
                await pageBlob.ExistsAsync(this.RequestOptions, this.OperationContext, cancellationToken.Value);

            if (blobExists)
            {
                using (Stream stream = await pageBlob.OpenReadAsync(CreateAccessCondition(pageBlob),
                                                                    this.RequestOptions, this.OperationContext, cancellationToken.Value))
                {
                    stream.Seek(pageOffset, SeekOrigin.Begin);

                    while (totalBytesRead < pageBytesToRead)
                    {
                        int bytesRead = await stream.ReadAsync(readBuffer, 0, pageBytesToRead,
                                                               cancellationToken.Value);

                        if (bytesRead == 0)
                        {
                            break;
                        }

                        totalBytesRead += bytesRead;
                    }
                }
            }

            int requestedBytesRead = bytesToRead < totalBytesRead ? bytesToRead : totalBytesRead;

            return(new InternalDownloadToBufferResult(readBuffer, byteOffsetWithinPage, requestedBytesRead));
        }
Exemplo n.º 7
0
        private async Task CreateBlobAsync(CancellationToken token)
        {
            try
            {
                await leaseBlob.Container.CreateIfNotExistsAsync();

                if (!await leaseBlob.ExistsAsync())
                {
                    await leaseBlob.CreateAsync(0);
                }
            }
            catch (StorageException storageException)
            {
                logger.LogError($"Error creating a mutex blob. Details: {storageException}");
                throw;
            }
        }
Exemplo n.º 8
0
        /// <summary>
        /// Checks if the page blob exists.
        /// </summary>
        /// <param name="cancellationToken">Optional cancellation token</param>
        /// <returns>true if the page blob exists, false if it does not exist.</returns>
        public virtual async Task <bool> ExistsAsync(CancellationToken?cancellationToken = null)
        {
            if (cancellationToken == null)
            {
                cancellationToken = CancellationToken.None;
            }

            this.EnsureInitialized();
            bool containerExists = await this.BlobContainer.ExistsAsync(this.RequestOptions, this.OperationContext,
                                                                        cancellationToken.Value);

            if (!containerExists)
            {
                return(false);
            }

            CloudPageBlob pageBlob = this.BlobContainer.GetPageBlobReference(this.BlobPath);

            return(await pageBlob.ExistsAsync(this.RequestOptions, this.OperationContext, cancellationToken.Value));
        }
Exemplo n.º 9
0
        async Task CreateBlobAsync(CancellationToken token)
        {
            await _leaseBlob.Container.CreateIfNotExistsAsync(token);

            if (!await _leaseBlob.ExistsAsync(token))
            {
                try {
                    await _leaseBlob.CreateAsync(_size, token);
                }
                catch (StorageException e) {
                    if (e.InnerException is WebException)
                    {
                        var webException = e.InnerException as WebException;
                        var response     = webException.Response as HttpWebResponse;

                        if (response == null || response.StatusCode != HttpStatusCode.PreconditionFailed)
                        {
                            throw;
                        }
                    }
                }
            }
        }
Exemplo n.º 10
0
        /// <summary>
        /// Returns the full contents of the page blob as a <see cref="String"/>.
        /// </summary>
        /// <param name="cancellationToken">Optional cancellation token</param>
        /// <returns>The full contents of the page blob as a <see cref="String"/>.
        /// Important: This <see cref="Stream"/> is intended as Read-Only and DOES support Seek, Position, and Length operations.</returns>
        public virtual async Task <Stream> DownloadToStreamAsync(CancellationToken?cancellationToken = null)
        {
            if (cancellationToken == null)
            {
                cancellationToken = CancellationToken.None;
            }

            this.EnsureInitialized();

            CloudPageBlob pageBlob = this.BlobContainer.GetPageBlobReference(this.BlobPath);

            bool blobExists =
                await pageBlob.ExistsAsync(this.RequestOptions, this.OperationContext, cancellationToken.Value);

            if (blobExists)
            {
                Stream downloadPageBlobStream = await pageBlob.OpenReadAsync(CreateAccessCondition(pageBlob),
                                                                             this.RequestOptions, this.OperationContext, cancellationToken.Value);

                return(new NullCharacterInFinalPageTruncatingStream(downloadPageBlobStream));
            }

            return(new MemoryStream(new byte[0], false));
        }
        private async Task CreateBlobAsync(CancellationToken token)
        {
            logger.InfoFormat("Creating container {0} if it does not exist.", leaseBlob.Container.Name);
            await leaseBlob.Container.CreateIfNotExistsAsync(token);

            if (!await leaseBlob.ExistsAsync(token))
            {
                try
                {
                    logger.InfoFormat("Creating blob {0}.", leaseBlob.Name);
                    await leaseBlob.CreateAsync(0, token);
                }
                catch (StorageException e)
                {
                    if (e.InnerException is WebException webException)
                    {
                        if (!(webException.Response is HttpWebResponse response) || response.StatusCode != HttpStatusCode.PreconditionFailed)
                        {
                            throw;
                        }
                    }
                }
            }
        }
Exemplo n.º 12
0
 /// <inheritdoc />
 public Task <bool> ExistsAsync(CancellationToken cancellationToken)
 {
     return(_sdk.ExistsAsync(options: null, operationContext: null, cancellationToken: cancellationToken));
 }
Exemplo n.º 13
0
 /// <inheritdoc />
 public Task <bool> ExistsAsync(CancellationToken cancellationToken)
 {
     return(_sdk.ExistsAsync(cancellationToken));
 }
Exemplo n.º 14
0
 public static bool Exists(this CloudPageBlob blob)
 {
     return(blob.ExistsAsync().Result);
 }