Пример #1
1
        /// <summary>
        /// Create a block with a random name.
        /// </summary>
        /// <param name="testBlob">The block blob.</param>
        /// <param name="testAccessCondition">The access condition.</param>
        /// <returns>The name of the new block.</returns>
        private async Task<string> BlockCreateAsync(CloudBlockBlob testBlob, AccessCondition testAccessCondition, OperationContext operationContext)
        {
            byte[] buffer = new byte[4 * 1024];
            Random random = new Random();
            random.NextBytes(buffer);
            string blockId = Guid.NewGuid().ToString("N");
            Stream blockData = new MemoryStream(buffer);
            await testBlob.PutBlockAsync(blockId, blockData.AsInputStream(), null /* content MD5 */, testAccessCondition, null /* options */, operationContext);

            return blockId;
        }
        public async Task CloudBlockBlobDownloadBlockListAsync()
        {
            byte[]             buffer      = GetRandomBuffer(1024);
            List <string>      blocks      = GetBlockIdList(3);
            List <string>      extraBlocks = GetBlockIdList(2);
            CloudBlobContainer container   = GetRandomContainerReference();

            try
            {
                await container.CreateAsync();

                CloudBlockBlob blob = container.GetBlockBlobReference("blob1");
                foreach (string block in blocks)
                {
                    using (MemoryStream memoryStream = new MemoryStream(buffer))
                    {
                        await blob.PutBlockAsync(block, memoryStream.AsInputStream(), null);
                    }
                }
                await blob.PutBlockListAsync(blocks);

                foreach (string block in extraBlocks)
                {
                    using (MemoryStream memoryStream = new MemoryStream(buffer))
                    {
                        await blob.PutBlockAsync(block, memoryStream.AsInputStream(), null);
                    }
                }

                CloudBlockBlob blob2 = container.GetBlockBlobReference("blob1");
                await blob2.FetchAttributesAsync();

                Assert.AreEqual(1024 * blocks.Count, blob2.Properties.Length);

                IEnumerable <ListBlockItem> blockList = await blob2.DownloadBlockListAsync();

                foreach (ListBlockItem blockItem in blockList)
                {
                    Assert.IsTrue(blockItem.Committed);
                    Assert.IsTrue(blocks.Remove(blockItem.Name));
                }
                Assert.AreEqual(0, blocks.Count);

                blockList = await blob2.DownloadBlockListAsync(BlockListingFilter.Uncommitted, null, null, null);

                foreach (ListBlockItem blockItem in blockList)
                {
                    Assert.IsFalse(blockItem.Committed);
                    Assert.IsTrue(extraBlocks.Remove(blockItem.Name));
                }
                Assert.AreEqual(0, extraBlocks.Count);
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
        public async Task CloudBlockBlobPutBlockBoundariesAsync()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                await container.CreateAsync();

                CloudBlockBlob blob    = container.GetBlockBlobReference("blob1");
                string         blockId = GetBlockIdList(1).First();

                OperationContext operationContext = new OperationContext();
                byte[]           buffer           = new byte[0];
                using (MemoryStream stream = new MemoryStream(buffer))
                {
                    await TestHelper.ExpectedExceptionAsync(
                        async() => await blob.PutBlockAsync(blockId, stream.AsInputStream(), null, null, null, operationContext),
                        operationContext,
                        "Trying to upload a block with zero bytes should fail",
                        HttpStatusCode.BadRequest);
                }

                buffer = new byte[1];
                using (MemoryStream stream = new MemoryStream(buffer))
                {
                    await blob.PutBlockAsync(blockId, stream.AsInputStream(), null);
                }

                buffer = new byte[4 * 1024 * 1024];
                using (MemoryStream stream = new MemoryStream(buffer))
                {
                    await blob.PutBlockAsync(blockId, stream.AsInputStream(), null);
                }

                buffer = new byte[4 * 1024 * 1024 + 1];
                using (MemoryStream stream = new MemoryStream(buffer))
                {
                    await TestHelper.ExpectedExceptionAsync(
                        async() => await blob.PutBlockAsync(blockId, stream.AsInputStream(), null, null, null, operationContext),
                        operationContext,
                        "Trying to upload a block with more than 4MB should fail",
                        HttpStatusCode.RequestEntityTooLarge);
                }
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
        public async Task CloudBlockBlobUploadAsync()
        {
            byte[]             buffer      = GetRandomBuffer(1024);
            List <string>      blocks      = GetBlockIdList(3);
            List <string>      extraBlocks = GetBlockIdList(2);
            CloudBlobContainer container   = GetRandomContainerReference();

            try
            {
                await container.CreateAsync();

                using (MemoryStream wholeBlob = new MemoryStream())
                {
                    CloudBlockBlob blob = container.GetBlockBlobReference("blob1");

                    foreach (string block in blocks)
                    {
                        using (MemoryStream memoryStream = new MemoryStream(buffer))
                        {
                            await blob.PutBlockAsync(block, memoryStream.AsInputStream(), null);
                        }
                        wholeBlob.Write(buffer, 0, buffer.Length);
                    }
                    foreach (string block in extraBlocks)
                    {
                        using (MemoryStream memoryStream = new MemoryStream(buffer))
                        {
                            await blob.PutBlockAsync(block, memoryStream.AsInputStream(), null);
                        }
                    }

                    await blob.PutBlockListAsync(blocks);

                    CloudBlockBlob blob2 = container.GetBlockBlobReference("blob1");
                    using (MemoryStream downloadedBlob = new MemoryStream())
                    {
                        await blob2.DownloadToStreamAsync(downloadedBlob.AsOutputStream());

                        TestHelper.AssertStreamsAreEqual(wholeBlob, downloadedBlob);
                    }
                }
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
Пример #5
0
        private static async Task CreateForTestAsync(CloudBlockBlob blob, int blockCount, int blockSize, bool commit = true)
        {
            byte[] buffer = GetRandomBuffer(blockSize);
            List<string> blocks = GetBlockIdList(blockCount);

            foreach (string block in blocks)
            {
                using (MemoryStream stream = new MemoryStream(buffer))
                {
                    await blob.PutBlockAsync(block, stream.AsInputStream(), null);
                }
            }

            if (commit)
            {
                await blob.PutBlockListAsync(blocks);
            }
        }
Пример #6
0
        internal static async Task CreateForTestAsync(CloudBlockBlob blob, int blockCount, int blockSize, bool commit = true)
        {
            byte[]        buffer = GetRandomBuffer(blockSize);
            List <string> blocks = GetBlockIdList(blockCount);

            foreach (string block in blocks)
            {
                using (MemoryStream stream = new MemoryStream(buffer))
                {
                    await blob.PutBlockAsync(block, stream, null);
                }
            }

            if (commit)
            {
                await blob.PutBlockListAsync(blocks);
            }
        }
        public async Task CloudBlockBlobMethodsOnPageBlobAsync()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                await container.CreateAsync();

                List <string> blobs = await CreateBlobsAsync(container, 1, BlobType.PageBlob);

                CloudBlockBlob blob      = container.GetBlockBlobReference(blobs.First());
                List <string>  blockList = GetBlockIdList(1);

                OperationContext operationContext = new OperationContext();
                byte[]           buffer           = new byte[1];
                using (MemoryStream stream = new MemoryStream(buffer))
                {
                    await TestHelper.ExpectedExceptionAsync(
                        async() => await blob.PutBlockAsync(blockList.First(), stream.AsInputStream(), null, null, null, operationContext),
                        operationContext,
                        "Block operations should fail on page blobs",
                        HttpStatusCode.Conflict,
                        "InvalidBlobType");
                }

                await TestHelper.ExpectedExceptionAsync(
                    async() => await blob.PutBlockListAsync(blockList, null, null, operationContext),
                    operationContext,
                    "Block operations should fail on page blobs",
                    HttpStatusCode.Conflict,
                    "InvalidBlobType");

                await TestHelper.ExpectedExceptionAsync(
                    async() => await blob.DownloadBlockListAsync(BlockListingFilter.Committed, null, null, operationContext),
                    operationContext,
                    "Block operations should fail on page blobs",
                    HttpStatusCode.Conflict,
                    "InvalidBlobType");
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
Пример #8
0
        private static async Task AppendToBlob(CloudBlockBlob blockBlob, string content, string md5)
        {
            var leaseId = await blockBlob.AcquireLeaseAsync(TimeSpan.FromSeconds(30), null);
            var access = AccessCondition.GenerateLeaseCondition(leaseId);
            var options = new BlobRequestOptions();
            var context = new OperationContext();

            try
            {
                var blockId = Convert.ToBase64String(Guid.NewGuid().ToByteArray());
                using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(content)))
                {
                    stream.Position = 0;
                    await blockBlob.PutBlockAsync(blockId, stream, md5, access, options, context);
                }

                var blockList = await blockBlob.DownloadBlockListAsync(BlockListingFilter.Committed, access, options, context);
                var blockIds = blockList.Select(x => x.Name).ToList();
                blockIds.Add(blockId);
                await blockBlob.PutBlockListAsync(blockIds, access, options, context);
            }
            catch(Exception ex)
            {
                //TODO - logging
            }
            finally
            {
                await blockBlob.ReleaseLeaseAsync(access);
            }
        }
        public async Task CloudBlockBlobBlockReorderingAsync()
        {
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                await container.CreateAsync();

                CloudBlockBlob blob = container.GetBlockBlobReference("blob1");

                List <string> originalBlockIds = GetBlockIdList(10);
                List <string> blockIds         = new List <string>(originalBlockIds);
                List <byte[]> blocks           = new List <byte[]>();
                for (int i = 0; i < blockIds.Count; i++)
                {
                    byte[] buffer = Encoding.UTF8.GetBytes(i.ToString());
                    using (MemoryStream stream = new MemoryStream(buffer))
                    {
                        await blob.PutBlockAsync(blockIds[i], stream.AsInputStream(), null);
                    }
                    blocks.Add(buffer);
                }
                await blob.PutBlockListAsync(blockIds);

                Assert.AreEqual("0123456789", await DownloadTextAsync(blob, Encoding.UTF8));

                blockIds.RemoveAt(0);
                await blob.PutBlockListAsync(blockIds);

                Assert.AreEqual("123456789", await DownloadTextAsync(blob, Encoding.UTF8));

                blockIds.RemoveAt(8);
                await blob.PutBlockListAsync(blockIds);

                Assert.AreEqual("12345678", await DownloadTextAsync(blob, Encoding.UTF8));

                blockIds.RemoveAt(3);
                await blob.PutBlockListAsync(blockIds);

                Assert.AreEqual("1235678", await DownloadTextAsync(blob, Encoding.UTF8));

                using (MemoryStream stream = new MemoryStream(blocks[9]))
                {
                    await blob.PutBlockAsync(originalBlockIds[9], stream.AsInputStream(), null);
                }
                blockIds.Insert(0, originalBlockIds[9]);
                await blob.PutBlockListAsync(blockIds);

                Assert.AreEqual("91235678", await DownloadTextAsync(blob, Encoding.UTF8));

                using (MemoryStream stream = new MemoryStream(blocks[0]))
                {
                    await blob.PutBlockAsync(originalBlockIds[0], stream.AsInputStream(), null);
                }
                blockIds.Add(originalBlockIds[0]);
                await blob.PutBlockListAsync(blockIds);

                Assert.AreEqual("912356780", await DownloadTextAsync(blob, Encoding.UTF8));

                using (MemoryStream stream = new MemoryStream(blocks[4]))
                {
                    await blob.PutBlockAsync(originalBlockIds[4], stream.AsInputStream(), null);
                }
                blockIds.Insert(2, originalBlockIds[4]);
                await blob.PutBlockListAsync(blockIds);

                Assert.AreEqual("9142356780", await DownloadTextAsync(blob, Encoding.UTF8));

                blockIds.Insert(0, originalBlockIds[0]);
                await blob.PutBlockListAsync(blockIds);

                Assert.AreEqual("09142356780", await DownloadTextAsync(blob, Encoding.UTF8));
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
Пример #10
0
        /// <summary>
        /// Uploads the current chunk to the storage
        /// </summary>
        /// <param name="model"></param>
        /// <param name="chunk"></param>
        /// <param name="id"></param>
        /// <param name="blobBlock"></param>
        /// <returns></returns>
        private async Task<JsonResult> UploadCurrentChunk(CloudFile model, byte[] chunk, int id, CloudBlockBlob blobBlock)
        {
            using (var chunkStream = new MemoryStream(chunk))
            {
                var blockId = Convert.ToBase64String(Encoding.UTF8.GetBytes(
                        string.Format(CultureInfo.InvariantCulture, "{0:D4}", id)));
                try
                {
                    //model.BlockBlob.PutBlockAsync(
                    //    blockId,
                    //    chunkStream, null, null,
                    //    new BlobRequestOptions()
                    //    {
                    //        RetryPolicy = new LinearRetry(TimeSpan.FromSeconds(10), 3)
                    //    },
                    //    null);

                    await blobBlock.PutBlockAsync(
                        blockId,
                        chunkStream, null, null,
                        new BlobRequestOptions()
                        {
                            RetryPolicy = new LinearRetry(TimeSpan.FromSeconds(10), 3)
                        },
                        null);

                    return null;
                }
                catch (StorageException e)
                {
                    HttpContext.Session.Remove("CurrentFile");
                    model.IsUploadCompleted = true;
                    model.UploadStatusMessage = "Failed to Upload file. Exception - " + e.Message;
                    return Json(new { error = true, isLastBlock = false, message = model.UploadStatusMessage });
                }
            }
        }