Beispiel #1
0
        public void GetBlockListScenarioTest(string containerName, string blobName, BlockListingFilter typesOfBlocks, string leaseId, HttpStatusCode?expectedError, params string[] expectedBlocks)
        {
            HttpWebRequest request = BlobTests.GetBlockListRequest(BlobContext, containerName, blobName, typesOfBlocks, AccessCondition.GenerateLeaseCondition(leaseId));

            Assert.IsTrue(request != null, "Failed to create HttpWebRequest");
            if (BlobContext.Credentials != null)
            {
                BlobTests.SignRequest(request, BlobContext);
            }
            HttpWebResponse response = BlobTestUtils.GetResponse(request, BlobContext);

            try
            {
                BlobTests.GetBlockListResponse(response, BlobContext, expectedError);
                GetBlockListResponse getBlockListResponse = new GetBlockListResponse(response.GetResponseStream());
                int i = 0;
                foreach (ListBlockItem item in getBlockListResponse.Blocks)
                {
                    if (expectedBlocks == null)
                    {
                        Assert.Fail("Should not have blocks.");
                    }
                    Assert.IsTrue(i < expectedBlocks.Length, "Unexpected block: " + item.Name);
                    Assert.AreEqual <string>(expectedBlocks[i++], item.Name, "Incorrect block.");
                }
                if (expectedBlocks != null && i < expectedBlocks.Length)
                {
                    Assert.Fail("Missing block: " + expectedBlocks[i] + "(and " + (expectedBlocks.Length - i - 1) + " more).");
                }
            }
            finally
            {
                response.Close();
            }
        }
        /// <summary>
        /// Returns an enumerable collection of the blob's blocks, using the specified block list filter.
        /// </summary>
        /// <param name="blockListingFilter">One of the enumeration values that indicates whether to return
        /// committed blocks, uncommitted blocks, or both.</param>
        /// <param name="options">An object that specifies any additional options for the request.</param>
        /// <returns>An enumerable collection of objects implementing <see cref="ListBlockItem"/>.</returns>
        public IEnumerable <ListBlockItem> DownloadBlockList(BlockListingFilter blockListingFilter, BlobRequestOptions options)
        {
            var fullModifier = BlobRequestOptions.CreateFullModifier(this.ServiceClient, options);

            return(TaskImplHelper.ExecuteImplWithRetry <IEnumerable <ListBlockItem> >(
                       (result) =>
            {
                return this.GetDownloadBlockList(blockListingFilter, fullModifier, result);
            },
                       fullModifier.RetryPolicy));
        }
        /// <summary>
        /// Begins an asynchronous operation to return an enumerable collection of the blob's blocks,
        /// using the specified block list filter.
        /// </summary>
        /// <param name="blockListingFilter">One of the enumeration values that indicates whether to return
        /// committed blocks, uncommitted blocks, or both.</param>
        /// <param name="options">An object that specifies any additional options for the request.</param>
        /// <param name="callback">The callback delegate that will receive notification when the asynchronous operation completes.</param>
        /// <param name="state">A user-defined object that will be passed to the callback delegate.</param>
        /// <returns>An <see cref="IAsyncResult"/> that references the asynchronous operation.</returns>
        public IAsyncResult BeginDownloadBlockList(BlockListingFilter blockListingFilter, BlobRequestOptions options, AsyncCallback callback, object state)
        {
            var fullModifier = BlobRequestOptions.CreateFullModifier(this.ServiceClient, options);

            return(TaskImplHelper.BeginImplWithRetry <IEnumerable <ListBlockItem> >(
                       (result) =>
            {
                return this.GetDownloadBlockList(blockListingFilter, fullModifier, result);
            },
                       fullModifier.RetryPolicy,
                       callback,
                       state));
        }
        /// <summary>
        ///     Returns an enumerable collection of the blob's blocks, using the specified block list filter asynchronously.
        /// </summary>
        /// <param name="blockBlob">Cloud block blob.</param>
        /// <param name="blockListingFilter">
        ///     One of the enumeration values that indicates whether to return
        ///     committed blocks, uncommitted blocks, or both.
        /// </param>
        /// <param name="accessCondition">
        ///     An <see cref="T:Microsoft.WindowsAzure.Storage.AccessCondition" /> object that represents the access conditions for the blob. If <c>null</c>, no condition is used.
        /// </param>
        /// <param name="cancellationToken">Cancellation token.</param>
        /// <returns>
        ///     An enumerable collection of objects implementing <see cref="T:Microsoft.WindowsAzure.Storage.Blob.ListBlockItem" />.
        /// </returns>
        public static Task<IEnumerable<ListBlockItem>> DownloadBlockListAsync(
            this CloudBlockBlob blockBlob,
            BlockListingFilter blockListingFilter = BlockListingFilter.Committed,
            AccessCondition accessCondition = null,
            CancellationToken cancellationToken = default (CancellationToken))
        {
            ICancellableAsyncResult asyncResult = blockBlob.BeginDownloadBlockList(blockListingFilter, accessCondition, null, null, null, null);
            CancellationTokenRegistration registration = cancellationToken.Register(p => asyncResult.Cancel(), null);

            return Task<IEnumerable<ListBlockItem>>.Factory.FromAsync(
                asyncResult,
                result =>
                    {
                        registration.Dispose();
                        return blockBlob.EndDownloadBlockList(result);
                    });
        }
Beispiel #5
0
        public void UploadBlock(Stream blockStream, string containerName, string fileName, int blockIndex, int blocksCount, string contentType)
        {
            string         blockId = Convert.ToBase64String(BitConverter.GetBytes(blockIndex));
            CloudBlockBlob blob    = GetBlockBlob(containerName, null, fileName);

            blob.PutBlockAsync(blockId, blockStream, null).Wait();
            if (blockIndex == blocksCount - 1)
            {
                BlockListingFilter          blockType = BlockListingFilter.Uncommitted;
                IEnumerable <ListBlockItem> blockList = blob.DownloadBlockListAsync(blockType, null, null, null).Result;
                List <string> blockIds = new List <string>();
                foreach (ListBlockItem blockItem in blockList)
                {
                    blockIds.Add(blockItem.Name);
                }
                blob.PutBlockListAsync(blockIds).Wait();
                blob.Properties.ContentType = contentType;
                blob.SetPropertiesAsync().Wait();
            }
        }
        /// <summary>
        /// Gets the download block list.
        /// </summary>
        /// <param name="typesOfBlocks">The types of blocks.</param>
        /// <param name="options">An object that specifies any additional options for the request.</param>
        /// <param name="setResult">The result report delegate.</param>
        /// <returns>A <see cref="TaskSequence"/> that gets the download block list.</returns>
        internal TaskSequence GetDownloadBlockList(BlockListingFilter typesOfBlocks, BlobRequestOptions options, Action <IEnumerable <ListBlockItem> > setResult)
        {
            var request = ProtocolHelper.GetWebRequest(this.ServiceClient, options, (timeout) => BlobRequest.GetBlockList(this.TransformedAddress, timeout, this.SnapshotTime, typesOfBlocks, null));

            this.ServiceClient.Credentials.SignRequest(request);

            // Retrieve the stream
            var requestStreamTask = request.GetResponseAsyncWithTimeout(this.ServiceClient, options.Timeout);

            yield return(requestStreamTask);

            // Copy the data
            using (var response = requestStreamTask.Result as HttpWebResponse)
            {
                using (var responseStream = response.GetResponseStream())
                {
                    var blockListResponse = new GetBlockListResponse(responseStream);

                    setResult(ParseResponse(blockListResponse));
                }

                this.ParseSizeAndLastModified(response);
            }
        }
 public ICancellableAsyncResult BeginDownloadBlockList(BlockListingFilter blockListingFilter, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext, AsyncCallback callback, object state)
 {
     BlobRequestOptions modifiedOptions = BlobRequestOptions.ApplyDefaults(options, BlobType.BlockBlob, this.ServiceClient);
     return Executor.BeginExecuteAsync(
         this.GetBlockListImpl(blockListingFilter, accessCondition, modifiedOptions),
         modifiedOptions.RetryPolicy,
         operationContext,
         callback,
         state);
 }
        /// <summary>
        /// Constructs a web request to return the list of blocks for a block blob.
        /// </summary>
        /// <param name="uri">The absolute URI to the blob.</param>
        /// <param name="timeout">The server timeout interval.</param>
        /// <param name="snapshot">The snapshot timestamp, if the blob is a snapshot.</param>
        /// <param name="typesOfBlocks">The types of blocks to include in the list: committed, uncommitted, or both.</param>
        /// <param name="accessCondition">The access condition to apply to the request.</param>
        /// <returns>A web request to use to perform the operation.</returns>
        public static HttpRequestMessage GetBlockList(Uri uri, int?timeout, DateTimeOffset?snapshot, BlockListingFilter typesOfBlocks, AccessCondition accessCondition, HttpContent content, OperationContext operationContext)
        {
            UriQueryBuilder builder = new UriQueryBuilder();

            builder.Add(Constants.QueryConstants.Component, "blocklist");
            builder.Add("blocklisttype", typesOfBlocks.ToString());
            BlobHttpRequestMessageFactory.AddSnapshot(builder, snapshot);

            HttpRequestMessage request = HttpRequestMessageFactory.CreateRequestMessage(HttpMethod.Get, uri, timeout, builder, content, operationContext);

            request.ApplyAccessCondition(accessCondition);
            return(request);
        }
Beispiel #9
0
 public override Task <IEnumerable <ListBlockItem> > DownloadBlockListAsync(BlockListingFilter blockListingFilter, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
 {
     throw new NotImplementedException();
 }
 /// <summary>
 /// Begins an asynchronous operation to return an enumerable collection of the blob's blocks,
 /// using the specified block list filter.
 /// </summary>
 /// <param name="blockListingFilter">One of the enumeration values that indicates whether to return
 /// committed blocks, uncommitted blocks, or both.</param>
 /// <param name="callback">The callback delegate that will receive notification when the asynchronous operation completes.</param>
 /// <param name="state">A user-defined object that will be passed to the callback delegate.</param>
 /// <returns>An <see cref="IAsyncResult"/> that references the asynchronous operation.</returns>
 public IAsyncResult BeginDownloadBlockList(BlockListingFilter blockListingFilter, AsyncCallback callback, object state)
 {
     return(this.BeginDownloadBlockList(blockListingFilter, null, callback, state));
 }
 /// <summary>
 /// Returns an enumerable collection of the blob's blocks, using the specified block list filter.
 /// </summary>
 /// <param name="blockListingFilter">One of the enumeration values that indicates whether to return
 /// committed blocks, uncommitted blocks, or both.</param>
 /// <returns>An enumerable collection of objects implementing <see cref="ListBlockItem"/>.</returns>
 public IEnumerable <ListBlockItem> DownloadBlockList(BlockListingFilter blockListingFilter)
 {
     return(this.DownloadBlockList(blockListingFilter, null));
 }
 public virtual Task <IEnumerable <ListBlockItem> > DownloadBlockListAsync(BlockListingFilter blockListingFilter, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
 {
     return(this.failoverExecutor.ExecuteAsync(x => x.DownloadBlockListAsync(blockListingFilter, accessCondition, options, operationContext)));
 }
        /// <summary>
        /// Constructs a web request to return the list of blocks for a block blob.
        /// </summary>
        /// <param name="uri">A <see cref="System.Uri"/> specifying the absolute URI to the blob.</param>
        /// <param name="timeout">An integer specifying the server timeout interval.</param>
        /// <param name="snapshot">A <see cref="DateTimeOffset"/> specifying the snapshot timestamp, if the blob is a snapshot.</param>
        /// <param name="typesOfBlocks">A <see cref="BlockListingFilter"/> enumeration value.</param>
        /// <param name="accessCondition">An <see cref="AccessCondition"/> object that represents the condition that must be met in order for the request to proceed.</param>
        /// <param name="useVersionHeader">A boolean value indicating whether to set the <i>x-ms-version</i> HTTP header.</param>
        /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
        /// <returns>A <see cref="System.Net.HttpWebRequest"/> object.</returns>
        public static HttpWebRequest GetBlockList(Uri uri, int? timeout, DateTimeOffset? snapshot, BlockListingFilter typesOfBlocks, AccessCondition accessCondition, bool useVersionHeader, OperationContext operationContext)
        {
            UriQueryBuilder builder = new UriQueryBuilder();
            builder.Add(Constants.QueryConstants.Component, "blocklist");
            builder.Add("blocklisttype", typesOfBlocks.ToString());
            BlobHttpWebRequestFactory.AddSnapshot(builder, snapshot);

            HttpWebRequest request = HttpWebRequestFactory.CreateWebRequest(WebRequestMethods.Http.Get, uri, timeout, builder, useVersionHeader, operationContext);
            request.ApplyAccessCondition(accessCondition);
            return request;
        }
 /// <summary>
 /// Constructs a web request to return the list of blocks for a block blob.
 /// </summary>
 /// <param name="uri">A <see cref="System.Uri"/> specifying the absolute URI to the blob.</param>
 /// <param name="timeout">An integer specifying the server timeout interval.</param>
 /// <param name="snapshot">A <see cref="DateTimeOffset"/> specifying the snapshot timestamp, if the blob is a snapshot.</param>
 /// <param name="typesOfBlocks">A <see cref="BlockListingFilter"/> enumeration value.</param>
 /// <param name="accessCondition">An <see cref="AccessCondition"/> object that represents the condition that must be met in order for the request to proceed.</param>
 /// <param name="operationContext">An <see cref="OperationContext"/> object that represents the context for the current operation.</param>
 /// <returns>A <see cref="System.Net.HttpWebRequest"/> object.</returns>
 public static HttpWebRequest GetBlockList(Uri uri, int? timeout, DateTimeOffset? snapshot, BlockListingFilter typesOfBlocks, AccessCondition accessCondition, OperationContext operationContext)
 {
     return BlobHttpWebRequestFactory.GetBlockList(uri, timeout, snapshot, typesOfBlocks, accessCondition, true /* useVersionHeader */, operationContext);
 }
 public virtual Task<IEnumerable<ListBlockItem>> DownloadBlockListAsync(BlockListingFilter blockListingFilter, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
 {
     BlobRequestOptions modifiedOptions = BlobRequestOptions.ApplyDefaults(options, BlobType.BlockBlob, this.ServiceClient);
     return Task.Run(async () => await Executor.ExecuteAsync(
         this.GetBlockListImpl(blockListingFilter, accessCondition, modifiedOptions),
         modifiedOptions.RetryPolicy,
         operationContext,
         cancellationToken), cancellationToken);
 }
        /// <summary>
        /// Gets the download block list.
        /// </summary>
        /// <param name="typesOfBlocks">The types of blocks.</param>
        /// <param name="accessCondition">An <see cref="AccessCondition"/> object that represents the access conditions for the blob. If <c>null</c>, no condition is used.</param>
        /// <param name="options">A <see cref="BlobRequestOptions"/> object that specifies additional options for the request.</param>
        /// <returns>A <see cref="TaskSequence"/> that gets the download block list.</returns>
        internal RESTCommand<IEnumerable<ListBlockItem>> GetBlockListImpl(BlockListingFilter typesOfBlocks, AccessCondition accessCondition, BlobRequestOptions options)
        {
            RESTCommand<IEnumerable<ListBlockItem>> getCmd = new RESTCommand<IEnumerable<ListBlockItem>>(this.ServiceClient.Credentials, this.attributes.StorageUri);

            options.ApplyToStorageCommand(getCmd);
            getCmd.CommandLocationMode = CommandLocationMode.PrimaryOrSecondary;
            getCmd.RetrieveResponseStream = true;
            getCmd.BuildRequest = (cmd, uri, builder, cnt, serverTimeout, ctx) => BlobHttpRequestMessageFactory.GetBlockList(uri, serverTimeout, this.SnapshotTime, typesOfBlocks, accessCondition, cnt, ctx, this.ServiceClient.GetCanonicalizer(), this.ServiceClient.Credentials);
            getCmd.PreProcessResponse = (cmd, resp, ex, ctx) => HttpResponseParsers.ProcessExpectedStatusCodeNoException(HttpStatusCode.OK, resp, null /* retVal */, cmd, ex);
            getCmd.PostProcessResponse = (cmd, resp, ctx) =>
            {
                CloudBlob.UpdateETagLMTLengthAndSequenceNumber(this.attributes, resp, true);
                return Task.Factory.StartNew(() =>
                {
                    GetBlockListResponse responseParser = new GetBlockListResponse(cmd.ResponseStream);
                    IEnumerable<ListBlockItem> blocks = new List<ListBlockItem>(responseParser.Blocks);
                    return blocks;
                });
            };

            return getCmd;
        }
        public List<ListBlockItem> AssertBlockListsAreEqual(string containerName, string blobName, BlockListingFilter blockType, List<Basic.Azure.Storage.Communications.BlobService.ParsedBlockListBlockId> gottenBlocks)
        {
            var client = _storageAccount.CreateCloudBlobClient();
            var container = client.GetContainerReference(containerName);
            if (!container.Exists())
                Assert.Fail("AssertBlockExists: The container '{0}' does not exist", containerName);

            var blob = container.GetBlockBlobReference(blobName);
            var blockList = blob.DownloadBlockList(blockType).ToList();

            Assert.AreEqual(blockList.Count, gottenBlocks.Count);
            for (var i = 0; i < gottenBlocks.Count; i++)
            {
                var expectedBlock = blockList[i];
                var gottenBlock = gottenBlocks[i];
                Assert.AreEqual(expectedBlock.Name, gottenBlock.Name);
                Assert.AreEqual(expectedBlock.Length, gottenBlock.Size);
            }

            return blockList;
        }
        public ListBlockItem AssertBlockExists(string containerName, string blobName, string blockId, BlockListingFilter blockType = BlockListingFilter.All)
        {
            var client = _storageAccount.CreateCloudBlobClient();
            var container = client.GetContainerReference(containerName);
            if (!container.Exists())
                Assert.Fail("AssertBlockExists: The container '{0}' does not exist", containerName);

            var blob = container.GetBlockBlobReference(blobName);
            var blockList = blob.DownloadBlockList(blockType);
            var block = blockList.FirstOrDefault(item => item.Name == blockId);

            if (block == null)
                Assert.Fail("AssertBlockExists: The block of id '{0}' does not exist", blockId);

            return block;
        }
        /// <summary>
        /// Constructs a web request to return the list of blocks for a block blob.
        /// </summary>
        /// <param name="uri">The absolute URI to the blob.</param>
        /// <param name="timeout">The server timeout interval.</param>
        /// <param name="snapshot">The snapshot timestamp, if the blob is a snapshot.</param>
        /// <param name="typesOfBlocks">The types of blocks to include in the list: committed, uncommitted, or both.</param>
        /// <param name="accessCondition">The access condition to apply to the request.</param>
        /// <returns>A web request to use to perform the operation.</returns>
        public static HttpRequestMessage GetBlockList(Uri uri, int? timeout, DateTimeOffset? snapshot, BlockListingFilter typesOfBlocks, AccessCondition accessCondition, HttpContent content, OperationContext operationContext)
        {
            UriQueryBuilder builder = new UriQueryBuilder();
            builder.Add(Constants.QueryConstants.Component, "blocklist");
            builder.Add("blocklisttype", typesOfBlocks.ToString());
            BlobHttpRequestMessageFactory.AddSnapshot(builder, snapshot);

            HttpRequestMessage request = HttpRequestMessageFactory.CreateRequestMessage(HttpMethod.Get, uri, timeout, builder, content, operationContext);
            request.ApplyAccessCondition(accessCondition);
            return request;
        }
 public Task<IEnumerable<ListBlockItem>> DownloadBlockListAsync(BlockListingFilter blockListingFilter, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
 {
     return AsyncExtensions.TaskFromApm(this.BeginDownloadBlockList, this.EndDownloadBlockList, blockListingFilter, accessCondition, options, operationContext, cancellationToken);
 }
        protected List <ListBlockItem> AssertBlockListsAreEqual(string containerName, string blobName, BlockListingFilter blockType, List <ParsedBlockListBlockId> gottenBlocks)
        {
            var client    = StorageAccount.CreateCloudBlobClient();
            var container = client.GetContainerReference(containerName);

            if (!container.Exists())
            {
                Assert.Fail("AssertBlockExists: The container '{0}' does not exist", containerName);
            }

            var blob      = container.GetBlockBlobReference(blobName);
            var blockList = blob.DownloadBlockList(blockType).ToList();

            Assert.AreEqual(blockList.Count, gottenBlocks.Count);
            for (var i = 0; i < gottenBlocks.Count; i++)
            {
                var expectedBlock = blockList[i];
                var gottenBlock   = gottenBlocks[i];
                Assert.AreEqual(expectedBlock.Name, gottenBlock.Name);
                Assert.AreEqual(expectedBlock.Length, gottenBlock.Size);
            }

            return(blockList);
        }
 public virtual Task <IEnumerable <ListBlockItem> > DownloadBlockListAsync(BlockListingFilter blockListingFilter, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext, CancellationToken cancellationToken)
 {
     throw new System.NotImplementedException();
 }
 public static HttpWebRequest GetBlockListRequest(BlobContext context, string containerName, string blobName, BlockListingFilter typesOfBlocks, AccessCondition accessCondition)
 {
     Uri uri = BlobClientTests.ConstructUri(context.Address, containerName, blobName);
     OperationContext opContext = new OperationContext();
     HttpWebRequest request = BlobHttpWebRequestFactory.GetBlockList(uri, context.Timeout, null /* snapshot */, typesOfBlocks, accessCondition, opContext);
     Assert.IsNotNull(request);
     Assert.IsNotNull(request.Method);
     Assert.AreEqual("GET", request.Method);
     BlobTestUtils.RangeHeader(request, null);
     BlobTestUtils.LeaseIdHeader(request, null);
     return request;
 }
 internal RESTCommand <IEnumerable <ListBlockItem> > GetBlockListImpl(BlockListingFilter typesOfBlocks, AccessCondition accessCondition, BlobRequestOptions options)
 {
     throw new System.NotImplementedException();
 }
Beispiel #25
0
        public static HttpRequestMessage GetBlockListRequest(BlobContext context, string containerName, string blobName, BlockListingFilter typesOfBlocks, AccessCondition accessCondition)
        {
            Uri uri = BlobClientTests.ConstructUri(context.Address, containerName, blobName);
            OperationContext   opContext = new OperationContext();
            HttpRequestMessage request   = BlobHttpRequestMessageFactory.GetBlockList(uri, context.Timeout, null /* snapshot */, typesOfBlocks, accessCondition, null,
                                                                                      opContext,
                                                                                      SharedKeyCanonicalizer.Instance,
                                                                                      context.Credentials);

            Assert.IsNotNull(request);
            Assert.IsNotNull(request.Method);
            Assert.AreEqual(HttpMethod.Get, request.Method);
            BlobTestUtils.RangeHeader(request, null);
            BlobTestUtils.LeaseIdHeader(request, null);
            return(request);
        }
        /// <summary>
        /// Constructs a web request to return the list of blocks for a block blob.
        /// </summary>
        /// <param name="uri">The absolute URI to the blob.</param>
        /// <param name="timeout">The server timeout interval.</param>
        /// <param name="snapshot">The snapshot timestamp, if the blob is a snapshot.</param>
        /// <param name="typesOfBlocks">The types of blocks to include in the list: committed, uncommitted, or both.</param>
        /// <param name="leaseId">The lease ID for the blob, if it has an active lease.</param>
        /// <returns>A web request to use to perform the operation.</returns>
        public static HttpWebRequest GetBlockList(Uri uri, int timeout, DateTime?snapshot, BlockListingFilter typesOfBlocks, string leaseId)
        {
            UriQueryBuilder builder = new UriQueryBuilder();

            builder.Add(Constants.QueryConstants.Component, "blocklist");

            builder.Add("blocklisttype", typesOfBlocks.ToString());

            BlobRequest.AddSnapshot(builder, snapshot);

            HttpWebRequest request = CreateWebRequest(uri, timeout, builder);

            request.Method = "GET";

            Request.AddLeaseId(request, leaseId);

            return(request);
        }
 public IAsyncOperation<IEnumerable<ListBlockItem>> DownloadBlockListAsync(BlockListingFilter blockListingFilter, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
 {
     BlobRequestOptions modifiedOptions = BlobRequestOptions.ApplyDefaults(options, BlobType.BlockBlob, this.ServiceClient);
     return AsyncInfo.Run(async (token) => await Executor.ExecuteAsync(
         this.GetBlockListImpl(blockListingFilter, accessCondition, modifiedOptions),
         modifiedOptions.RetryPolicy,
         operationContext,
         token));
 }
        /// <summary>
        /// Constructs a web request to return the list of blocks for a block blob.
        /// </summary>
        /// <param name="uri">The absolute URI to the blob.</param>
        /// <param name="timeout">The server timeout interval.</param>
        /// <param name="snapshot">The snapshot timestamp, if the blob is a snapshot.</param>
        /// <param name="typesOfBlocks">The types of blocks to include in the list: committed, uncommitted, or both.</param>
        /// <param name="leaseId">The lease ID for the blob, if it has an active lease.</param>
        /// <returns>A web request to use to perform the operation.</returns>
        public static HttpWebRequest GetBlockList(Uri uri, int timeout, DateTime? snapshot, BlockListingFilter typesOfBlocks, string leaseId)
        {
            UriQueryBuilder builder = new UriQueryBuilder();
            builder.Add(Constants.QueryConstants.Component, "blocklist");

            builder.Add("blocklisttype", typesOfBlocks.ToString());
        
            BlobRequest.AddSnapshot(builder, snapshot);

            HttpWebRequest request = CreateWebRequest(uri, timeout, builder);

            request.Method = "GET";

            Request.AddLeaseId(request, leaseId);

            return request;
        }
 /// <summary>
 /// Begins an asynchronous operation to return an enumerable collection of the blob's blocks, 
 /// using the specified block list filter.
 /// </summary>
 /// <param name="blockListingFilter">One of the enumeration values that indicates whether to return 
 /// committed blocks, uncommitted blocks, or both.</param>
 /// <param name="callback">The callback delegate that will receive notification when the asynchronous operation completes.</param>
 /// <param name="state">A user-defined object that will be passed to the callback delegate.</param>
 /// <returns>An <see cref="IAsyncResult"/> that references the asynchronous operation.</returns>
 public IAsyncResult BeginDownloadBlockList(BlockListingFilter blockListingFilter, AsyncCallback callback, object state)
 {
     return this.BeginDownloadBlockList(blockListingFilter, null, callback, state);
 }
 public static IEnumerable <ListBlockItem> DownloadBlockList(this CloudBlockBlob blob, BlockListingFilter blockListingFilter = BlockListingFilter.Committed, AccessCondition accessCondition = null, BlobRequestOptions options = null, OperationContext operationContext = null)
 {
     return(blob.DownloadBlockListAsync(blockListingFilter, accessCondition, options, operationContext).GetAwaiter().GetResult());
 }
        /// <summary>
        /// Gets the download block list.
        /// </summary>
        /// <param name="typesOfBlocks">The types of blocks.</param>
        /// <param name="accessCondition">An <see cref="AccessCondition"/> object that represents the access conditions for the blob. If <c>null</c>, no condition is used.</param>
        /// <param name="options">A <see cref="BlobRequestOptions"/> object that specifies any additional options for the request.</param>
        /// <returns>A <see cref="TaskSequence"/> that gets the download block list.</returns>
        internal RESTCommand<IEnumerable<ListBlockItem>> GetBlockListImpl(BlockListingFilter typesOfBlocks, AccessCondition accessCondition, BlobRequestOptions options)
        {
            RESTCommand<IEnumerable<ListBlockItem>> getCmd = new RESTCommand<IEnumerable<ListBlockItem>>(this.ServiceClient.Credentials, this.Uri);

            getCmd.ApplyRequestOptions(options);
            getCmd.RetrieveResponseStream = true;
            getCmd.Handler = this.ServiceClient.AuthenticationHandler;
            getCmd.BuildClient = HttpClientFactory.BuildHttpClient;
            getCmd.BuildRequest = (cmd, cnt, ctx) => BlobHttpRequestMessageFactory.GetBlockList(cmd.Uri, cmd.ServerTimeoutInSeconds, this.SnapshotTime, typesOfBlocks, accessCondition, cnt, ctx);
            getCmd.PreProcessResponse = (cmd, resp, ex, ctx) => HttpResponseParsers.ProcessExpectedStatusCodeNoException(HttpStatusCode.OK, resp, null /* retVal */, cmd, ex, ctx);
            getCmd.PostProcessResponse = (cmd, resp, ex, ctx) =>
            {
                CloudBlobSharedImpl.ParseSizeAndLastModified(this.attributes, resp);
                return Task.Factory.StartNew(() =>
                {
                    GetBlockListResponse responseParser = new GetBlockListResponse(cmd.ResponseStream);
                    IEnumerable<ListBlockItem> blocks = new List<ListBlockItem>(responseParser.Blocks);
                    return blocks;
                });
            };

            return getCmd;
        }
        /// <summary>
        /// Begins an asynchronous operation to return an enumerable collection of the blob's blocks, 
        /// using the specified block list filter.
        /// </summary>
        /// <param name="blockListingFilter">One of the enumeration values that indicates whether to return 
        /// committed blocks, uncommitted blocks, or both.</param>
        /// <param name="options">An object that specifies any additional options for the request.</param>
        /// <param name="callback">The callback delegate that will receive notification when the asynchronous operation completes.</param>
        /// <param name="state">A user-defined object that will be passed to the callback delegate.</param>
        /// <returns>An <see cref="IAsyncResult"/> that references the asynchronous operation.</returns>
        public IAsyncResult BeginDownloadBlockList(BlockListingFilter blockListingFilter, BlobRequestOptions options, AsyncCallback callback, object state)
        {
            var fullModifier = BlobRequestOptions.CreateFullModifier(this.ServiceClient, options);

            return TaskImplHelper.BeginImplWithRetry<IEnumerable<ListBlockItem>>(
                (result) =>
                    {
                        return this.GetDownloadBlockList(blockListingFilter, fullModifier, result);
                    },
                fullModifier.RetryPolicy,
                callback,
                state);
        }
 public void GetBlockListScenarioTest(string containerName, string blobName, BlockListingFilter typesOfBlocks, string leaseId, HttpStatusCode? expectedError, params string[] expectedBlocks)
 {
     HttpWebRequest request = BlobTests.GetBlockListRequest(BlobContext, containerName, blobName, typesOfBlocks, AccessCondition.GenerateLeaseCondition(leaseId));
     Assert.IsTrue(request != null, "Failed to create HttpWebRequest");
     if (BlobContext.Credentials != null)
     {
         BlobTests.SignRequest(request, BlobContext);
     }
     HttpWebResponse response = BlobTestUtils.GetResponse(request, BlobContext);
     try
     {
         BlobTests.GetBlockListResponse(response, BlobContext, expectedError);
         GetBlockListResponse getBlockListResponse = new GetBlockListResponse(response.GetResponseStream());
         int i = 0;
         foreach (ListBlockItem item in getBlockListResponse.Blocks)
         {
             if (expectedBlocks == null)
             {
                 Assert.Fail("Should not have blocks.");
             }
             Assert.IsTrue(i < expectedBlocks.Length, "Unexpected block: " + item.Name);
             Assert.AreEqual<string>(expectedBlocks[i++], item.Name, "Incorrect block.");
         }
         if (expectedBlocks != null && i < expectedBlocks.Length)
         {
             Assert.Fail("Missing block: " + expectedBlocks[i] + "(and " + (expectedBlocks.Length - i - 1) + " more).");
         }
     }
     finally
     {
         response.Close();
     }
 }
 /// <summary>
 /// Returns an enumerable collection of the blob's blocks, using the specified block list filter.
 /// </summary>
 /// <param name="blockListingFilter">One of the enumeration values that indicates whether to return 
 /// committed blocks, uncommitted blocks, or both.</param>
 /// <returns>An enumerable collection of objects implementing <see cref="ListBlockItem"/>.</returns>
 public IEnumerable<ListBlockItem> DownloadBlockList(BlockListingFilter blockListingFilter)
 {
     return this.DownloadBlockList(blockListingFilter, null);
 }
        /// <summary>
        /// Returns an enumerable collection of the blob's blocks, using the specified block list filter.
        /// </summary>
        /// <param name="blockListingFilter">One of the enumeration values that indicates whether to return 
        /// committed blocks, uncommitted blocks, or both.</param>
        /// <param name="options">An object that specifies any additional options for the request.</param>
        /// <returns>An enumerable collection of objects implementing <see cref="ListBlockItem"/>.</returns>
        public IEnumerable<ListBlockItem> DownloadBlockList(BlockListingFilter blockListingFilter, BlobRequestOptions options)
        {
            var fullModifier = BlobRequestOptions.CreateFullModifier(this.ServiceClient, options);

            return TaskImplHelper.ExecuteImplWithRetry<IEnumerable<ListBlockItem>>(
                (result) =>
                    {
                        return this.GetDownloadBlockList(blockListingFilter, fullModifier, result);
                    },
                fullModifier.RetryPolicy);
        }
        /// <summary>
        /// Gets the download block list.
        /// </summary>
        /// <param name="typesOfBlocks">The types of blocks.</param>
        /// <param name="options">An object that specifies any additional options for the request.</param>
        /// <param name="setResult">The result report delegate.</param>
        /// <returns>A <see cref="TaskSequence"/> that gets the download block list.</returns>
        internal TaskSequence GetDownloadBlockList(BlockListingFilter typesOfBlocks, BlobRequestOptions options, Action<IEnumerable<ListBlockItem>> setResult)
        {
            var request = ProtocolHelper.GetWebRequest(this.ServiceClient, options, (timeout) => BlobRequest.GetBlockList(this.TransformedAddress, timeout, this.SnapshotTime, typesOfBlocks, null));
            this.ServiceClient.Credentials.SignRequest(request);

            // Retrieve the stream
            var requestStreamTask = request.GetResponseAsyncWithTimeout(this.ServiceClient, options.Timeout);
            yield return requestStreamTask;

            // Copy the data
            using (var response = requestStreamTask.Result as HttpWebResponse)
            {
                using (var responseStream = response.GetResponseStream())
                {
                    var blockListResponse = new GetBlockListResponse(responseStream);

                    setResult(ParseResponse(blockListResponse));
                }

                this.ParseSizeAndLastModified(response);
            }
        }
 public IEnumerable<ListBlockItem> DownloadBlockList(BlockListingFilter blockListingFilter = BlockListingFilter.Committed, AccessCondition accessCondition = null, BlobRequestOptions options = null, OperationContext operationContext = null)
 {
     BlobRequestOptions modifiedOptions = BlobRequestOptions.ApplyDefaults(options, BlobType.BlockBlob, this.ServiceClient);
     return Executor.ExecuteSync(
         this.GetBlockListImpl(blockListingFilter, accessCondition, modifiedOptions),
         modifiedOptions.RetryPolicy,
         operationContext);
 }
Beispiel #38
0
        public async Task GetBlockListScenarioTest(string containerName, string blobName, BlockListingFilter typesOfBlocks, string leaseId, HttpStatusCode?expectedError, params string[] expectedBlocks)
        {
            HttpRequestMessage request = BlobTests.GetBlockListRequest(BlobContext, containerName, blobName, typesOfBlocks, AccessCondition.GenerateLeaseCondition(leaseId));

            Assert.IsTrue(request != null, "Failed to create HttpRequestMessage");
            using (HttpResponseMessage response = await BlobTestUtils.GetResponse(request, BlobContext))
            {
                BlobTests.GetBlockListResponse(response, BlobContext, expectedError);
                IEnumerable <ListBlockItem> getBlockListResponse = await GetBlockListResponse.ParseAsync(HttpResponseParsers.GetResponseStream(response), CancellationToken.None);

                int i = 0;
                foreach (ListBlockItem item in getBlockListResponse)
                {
                    if (expectedBlocks == null)
                    {
                        Assert.Fail("Should not have blocks.");
                    }
                    Assert.IsTrue(i < expectedBlocks.Length, "Unexpected block: " + item.Name);
                    Assert.AreEqual <string>(expectedBlocks[i++], item.Name, "Incorrect block.");
                }
                if (expectedBlocks != null && i < expectedBlocks.Length)
                {
                    Assert.Fail("Missing block: " + expectedBlocks[i] + "(and " + (expectedBlocks.Length - i - 1) + " more).");
                }
            }
        }
 public Task<IEnumerable<ListBlockItem>> DownloadBlockListAsync(BlockListingFilter blockListingFilter, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)
 {
     return this.DownloadBlockListAsync(blockListingFilter, accessCondition, options, operationContext, CancellationToken.None);
 }
Beispiel #40
0
        public static HttpWebRequest GetBlockListRequest(BlobContext context, string containerName, string blobName, BlockListingFilter typesOfBlocks, AccessCondition accessCondition)
        {
            Uri uri = BlobClientTests.ConstructUri(context.Address, containerName, blobName);
            OperationContext opContext = new OperationContext();
            HttpWebRequest   request   = BlobHttpWebRequestFactory.GetBlockList(uri, context.Timeout, null /* snapshot */, typesOfBlocks, accessCondition, opContext);

            Assert.IsNotNull(request);
            Assert.IsNotNull(request.Method);
            Assert.AreEqual("GET", request.Method);
            BlobTestUtils.RangeHeader(request, null);
            BlobTestUtils.LeaseIdHeader(request, null);
            return(request);
        }
        /// <summary>
        /// Gets the download block list.
        /// </summary>
        /// <param name="typesOfBlocks">The types of blocks.</param>
        /// <param name="accessCondition">An <see cref="AccessCondition"/> object that represents the access conditions for the blob. If <c>null</c>, no condition is used.</param>
        /// <param name="options">A <see cref="BlobRequestOptions"/> object that specifies any additional options for the request.</param>
        /// <returns>A <see cref="RESTCommand{T}"/> that gets the download block list.</returns>
        internal RESTCommand<IEnumerable<ListBlockItem>> GetBlockListImpl(BlockListingFilter typesOfBlocks, AccessCondition accessCondition, BlobRequestOptions options)
        {
            RESTCommand<IEnumerable<ListBlockItem>> getCmd = new RESTCommand<IEnumerable<ListBlockItem>>(this.ServiceClient.Credentials, this.Uri);

            getCmd.ApplyRequestOptions(options);
            getCmd.RetrieveResponseStream = true;
            getCmd.BuildRequestDelegate = (uri, builder, serverTimeout, ctx) => BlobHttpWebRequestFactory.GetBlockList(uri, serverTimeout, this.SnapshotTime, typesOfBlocks, accessCondition, ctx);
            getCmd.SignRequest = this.ServiceClient.AuthenticationHandler.SignRequest;
            getCmd.PreProcessResponse = (cmd, resp, ex, ctx) => HttpResponseParsers.ProcessExpectedStatusCodeNoException(HttpStatusCode.OK, resp, null /* retVal */, cmd, ex);
            getCmd.PostProcessResponse = (cmd, resp, ctx) =>
            {
                CloudBlobSharedImpl.UpdateETagLMTAndSequenceNumber(this.attributes, resp);
                GetBlockListResponse responseParser = new GetBlockListResponse(cmd.ResponseStream);
                IEnumerable<ListBlockItem> blocks = new List<ListBlockItem>(responseParser.Blocks);
                return blocks;
            };

            return getCmd;
        }
        protected Microsoft.WindowsAzure.Storage.Blob.ListBlockItem AssertBlockExists(string containerName, string blobName, string blockId, BlockListingFilter blockType = BlockListingFilter.All)
        {
            var client    = StorageAccount.CreateCloudBlobClient();
            var container = client.GetContainerReference(containerName);

            if (!container.Exists())
            {
                Assert.Fail("AssertBlockExists: The container '{0}' does not exist", containerName);
            }

            var blob      = container.GetBlockBlobReference(blobName);
            var blockList = blob.DownloadBlockList(blockType);
            var block     = blockList.FirstOrDefault(item => item.Name == blockId);

            if (block == null)
            {
                Assert.Fail("AssertBlockExists: The block of id '{0}' does not exist", blockId);
            }

            return(block);
        }