public async Task BlockBlobReadStreamBasicTestAsync()
        {
            byte[] buffer = GetRandomBuffer(5 * 1024 * 1024);
            CloudBlobContainer container = GetRandomContainerReference();
            try
            {
                await container.CreateAsync();

                CloudBlockBlob blob = container.GetBlockBlobReference("blob1");
                using (MemoryStream wholeBlob = new MemoryStream(buffer))
                {
                    await blob.UploadFromStreamAsync(wholeBlob.AsInputStream());
                }

                using (MemoryStream wholeBlob = new MemoryStream(buffer))
                {
                    using (IInputStream blobStream = await blob.OpenReadAsync())
                    {
                        await TestHelper.AssertStreamsAreEqualAsync(wholeBlob.AsInputStream(), blobStream);
                    }
                }
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
        public async Task PageBlobReadStreamBasicTestAsync()
        {
            byte[] buffer = GetRandomBuffer(5 * 1024 * 1024);
            CloudBlobContainer container = GetRandomContainerReference();
            try
            {
                await container.CreateAsync();

                CloudPageBlob blob = container.GetPageBlobReference("blob1");
                using (MemoryStream wholeBlob = new MemoryStream(buffer))
                {
                    await blob.UploadFromStreamAsync(wholeBlob.AsInputStream());
                }

                using (MemoryStream wholeBlob = new MemoryStream(buffer))
                {
                    wholeBlob.Seek(0, SeekOrigin.End);
                    IRandomAccessStreamWithContentType readStream = await blob.OpenReadAsync();
                    using (Stream blobStream = readStream.AsStreamForRead())
                    {
                        blobStream.Seek(0, SeekOrigin.End);
                        TestHelper.AssertStreamsAreEqual(wholeBlob, blobStream);
                    }
                }
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
        public async Task FileReadStreamBasicTestAsync()
        {
            byte[] buffer = GetRandomBuffer(5 * 1024 * 1024);
            CloudFileShare share = GetRandomShareReference();
            try
            {
                await share.CreateAsync();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                using (MemoryStream wholeFile = new MemoryStream(buffer))
                {
                    await file.UploadFromStreamAsync(wholeFile.AsInputStream());
                }

                using (MemoryStream wholeFile = new MemoryStream(buffer))
                {
                    IRandomAccessStreamWithContentType readStream = await file.OpenReadAsync();
                    using (Stream fileStream = readStream.AsStreamForRead())
                    {
                        TestHelper.AssertStreamsAreEqual(wholeFile, fileStream);
                    }
                }
            }
            finally
            {
                share.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
 public async Task BlobSeekTestAsync()
 {
     byte[] buffer = GetRandomBuffer(2 * 1024);
     CloudBlobContainer container = GetRandomContainerReference();
     try
     {
         await container.CreateAsync();
         CloudPageBlob blob = container.GetPageBlobReference("blob1");
         using (MemoryStream srcStream = new MemoryStream(buffer))
         {
             await blob.UploadFromStreamAsync(srcStream.AsInputStream(), null, null, null);
             using (IInputStream blobStream = await blob.OpenReadAsync())
             {
                 Stream blobStreamForRead = blobStream.AsStreamForRead();
                 blobStreamForRead.Seek(2048, 0);
                 byte[] buff = new byte[100];
                 int numRead = await blobStreamForRead.ReadAsync(buff, 0, 100);
                 Assert.AreEqual(numRead, 0);
             }
         }
     }
     finally
     {
         container.DeleteIfExistsAsync().AsTask().Wait();
     }
 }
 public async Task FileSeekTestAsync()
 {
     byte[] buffer = GetRandomBuffer(2 * 1024);
     CloudFileShare share = GetRandomShareReference();
     try
     {
         await share.CreateAsync();
         CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
         using (MemoryStream srcStream = new MemoryStream(buffer))
         {
             await file.UploadFromStreamAsync(srcStream.AsInputStream(), null, null, null);
             using (var fileStream = await file.OpenReadAsync())
             {
                 Stream fileStreamForRead = fileStream.AsStreamForRead();
                 fileStreamForRead.Seek(2048, 0);
                 byte[] buff = new byte[100];
                 int numRead = await fileStreamForRead.ReadAsync(buff, 0, 100);
                 Assert.AreEqual(numRead, 0);
             }
         }
     }
     finally
     {
         share.DeleteIfExistsAsync().AsTask().Wait();
     }
 }
        private static async Task <IHttpContent> GetCompressedContent(IHttpContent originalContent)
        {
            var ms = new System.IO.MemoryStream();

            try
            {
                await CompressOriginalContentStream(originalContent, ms).ConfigureAwait(false);

                ms.Seek(0, System.IO.SeekOrigin.Begin);

                var compressedContent = new Windows.Web.Http.HttpStreamContent(ms.AsInputStream());
                originalContent.CopyHeadersTo(compressedContent);
                compressedContent.Headers.ContentEncoding.Clear();
                compressedContent.Headers.ContentEncoding.Add(new Windows.Web.Http.Headers.HttpContentCodingHeaderValue("gzip"));
                compressedContent.Headers.ContentLength = (ulong)ms.Length;

                originalContent.Dispose();

                return(compressedContent);
            }
            catch
            {
                ms?.Dispose();
                throw;
            }
        }
        public async Task PageBlobDownloadToStreamRangeTestAsync()
        {
            byte[] buffer = GetRandomBuffer(2 * 1024);

            CloudPageBlob blob = this.testContainer.GetPageBlobReference("blob1");
            using (MemoryStream wholeBlob = new MemoryStream(buffer))
            {
                await blob.UploadFromStreamAsync(wholeBlob.AsInputStream());

                byte[] testBuffer = new byte[1024];
                MemoryStream blobStream = new MemoryStream(testBuffer);
                Exception ex = await TestHelper.ExpectedExceptionAsync<Exception>(
                    async () => await blob.DownloadRangeToStreamAsync(blobStream.AsOutputStream(), 0, 0),
                    "Requesting 0 bytes when downloading range should not work");
                Assert.IsInstanceOfType(ex.InnerException.InnerException, typeof(ArgumentOutOfRangeException));
                await blob.DownloadRangeToStreamAsync(blobStream.AsOutputStream(), 0, 1024);
                Assert.AreEqual(blobStream.Position, 1024);
                TestHelper.AssertStreamsAreEqualAtIndex(blobStream, wholeBlob, 0, 0, 1024);

                CloudPageBlob blob2 = this.testContainer.GetPageBlobReference("blob1");
                MemoryStream blobStream2 = new MemoryStream(testBuffer);
                ex = await TestHelper.ExpectedExceptionAsync<Exception>(
                    async () => await blob2.DownloadRangeToStreamAsync(blobStream.AsOutputStream(), 1024, 0),
                    "Requesting 0 bytes when downloading range should not work");
                Assert.IsInstanceOfType(ex.InnerException.InnerException, typeof(ArgumentOutOfRangeException));
                await blob2.DownloadRangeToStreamAsync(blobStream2.AsOutputStream(), 1024, 1024);
                TestHelper.AssertStreamsAreEqualAtIndex(blobStream2, wholeBlob, 0, 1024, 1024);

                AssertAreEqual(blob, blob2);
            }
        }
        public async Task FileReadLockToETagTestAsync()
        {
            byte[] outBuffer = new byte[1 * 1024 * 1024];
            byte[] buffer = GetRandomBuffer(2 * outBuffer.Length);
            CloudFileShare share = GetRandomShareReference();
            try
            {
                await share.CreateAsync();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                file.StreamMinimumReadSizeInBytes = outBuffer.Length;
                using (MemoryStream wholeFile = new MemoryStream(buffer))
                {
                    await file.UploadFromStreamAsync(wholeFile.AsInputStream());
                }

                OperationContext opContext = new OperationContext();
                using (IRandomAccessStreamWithContentType fileStream = await file.OpenReadAsync(null, null, opContext))
                {
                    Stream fileStreamForRead = fileStream.AsStreamForRead();
                    await fileStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length);
                    await file.SetMetadataAsync();
                    await ExpectedExceptionAsync(
                        async () => await fileStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length),
                        opContext,
                        "File read stream should fail if file is modified during read",
                        HttpStatusCode.PreconditionFailed);
                }

                opContext = new OperationContext();
                using (IRandomAccessStreamWithContentType fileStream = await file.OpenReadAsync(null, null, opContext))
                {
                    Stream fileStreamForRead = fileStream.AsStreamForRead();
                    long length = fileStreamForRead.Length;
                    await file.SetMetadataAsync();
                    await ExpectedExceptionAsync(
                        async () => await fileStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length),
                        opContext,
                        "File read stream should fail if file is modified during read",
                        HttpStatusCode.PreconditionFailed);
                }

                /*
                opContext = new OperationContext();
                AccessCondition accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(DateTimeOffset.Now.Subtract(TimeSpan.FromHours(1)));
                await file.SetMetadataAsync();
                await TestHelper.ExpectedExceptionAsync(
                    async () => await file.OpenReadAsync(accessCondition, null, opContext),
                    opContext,
                    "File read stream should fail if file is modified during read",
                    HttpStatusCode.PreconditionFailed);
                 */
            }
            finally
            {
                share.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
 public static async Task UploadTextAsync(CloudFile file, string text, Encoding encoding, AccessCondition accessCondition = null, FileRequestOptions options = null, OperationContext operationContext = null)
 {
     byte[] textAsBytes = encoding.GetBytes(text);
     using (MemoryStream stream = new MemoryStream())
     {
         stream.Write(textAsBytes, 0, textAsBytes.Length);
         stream.Seek(0, SeekOrigin.Begin);
         file.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount = 2;
         await file.UploadFromStreamAsync(stream.AsInputStream(), accessCondition, options, operationContext);
     }
 }
示例#10
0
 private Windows.Foundation.IAsyncOperationWithProgress <HttpResponseMessage, HttpProgress> ResponseFromObject(object response)
 {
     System.Runtime.Serialization.Json.DataContractJsonSerializer s = new System.Runtime.Serialization.Json.DataContractJsonSerializer(response.GetType());
     System.IO.MemoryStream ms = new System.IO.MemoryStream();
     s.WriteObject(ms, response);
     ms.Seek(0, SeekOrigin.Begin);
     return(Operation <HttpResponseMessage> .FromResult(new HttpResponseMessage()
     {
         Content = new HttpStreamContent(ms.AsInputStream())
     }));
 }
示例#11
0
        public async Task SaveAsync()
        {
            this.SaveApplicationState();
            var file = await ApplicationData.Current.LocalFolder.CreateFileAsync(SessionStateFileName, CreationCollisionOption.ReplaceExisting);
            var bytes = this.SerializeState(this.states);
            using (var sessionData = new MemoryStream(bytes))
            {
                using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    sessionData.Seek(0, SeekOrigin.Begin);
                    var provider = new DataProtectionProvider("LOCAL=user");

                    await provider.ProtectStreamAsync(sessionData.AsInputStream(), fileStream);
                    await fileStream.FlushAsync();
                }
            }
        }
        public async Task FileStoreContentMD5TestAsync()
        {
            FileRequestOptions optionsWithNoMD5 = new FileRequestOptions()
            {
                StoreFileContentMD5 = false,
            };
            FileRequestOptions optionsWithMD5 = new FileRequestOptions()
            {
                StoreFileContentMD5 = true,
            };

            CloudFileShare share = GetRandomShareReference();
            try
            {
                await share.CreateAsync();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file4");
                using (Stream stream = new MemoryStream())
                {
                    await file.UploadFromStreamAsync(stream.AsInputStream(), null, optionsWithMD5, null);
                }
                await file.FetchAttributesAsync();
                Assert.IsNotNull(file.Properties.ContentMD5);

                file = share.GetRootDirectoryReference().GetFileReference("file5");
                using (Stream stream = new MemoryStream())
                {
                    await file.UploadFromStreamAsync(stream.AsInputStream(), null, optionsWithNoMD5, null);
                }
                await file.FetchAttributesAsync();
                Assert.IsNull(file.Properties.ContentMD5);

                file = share.GetRootDirectoryReference().GetFileReference("file6");
                using (Stream stream = new MemoryStream())
                {
                    await file.UploadFromStreamAsync(stream.AsInputStream());
                }
                await file.FetchAttributesAsync();
                Assert.IsNull(file.Properties.ContentMD5);
            }
            finally
            {
                share.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
        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);
            }
        }
示例#14
0
 public static async Task UploadTextAsync(ICloudBlob blob, string text, Encoding encoding, AccessCondition accessCondition = null, BlobRequestOptions options = null, OperationContext operationContext = null)
 {
     byte[] textAsBytes = encoding.GetBytes(text);
     using (MemoryStream stream = new MemoryStream())
     {
         await stream.WriteAsync(textAsBytes, 0, textAsBytes.Length);
         if (blob.BlobType == BlobType.PageBlob)
         {
             int lastPageSize = (int)(stream.Length % 512);
             if (lastPageSize != 0)
             {
                 byte[] padding = new byte[512 - lastPageSize];
                 await stream.WriteAsync(padding, 0, padding.Length);
             }
         }
         stream.Seek(0, SeekOrigin.Begin);
         await blob.UploadFromStreamAsync(stream.AsInputStream(), accessCondition, options, operationContext);
     }
 }
        public async Task FileDownloadToStreamRangeTestAsync()
        {
            byte[] buffer = GetRandomBuffer(2 * 1024);
            CloudFileShare share = GetRandomShareReference();
            try
            {
                await share.CreateAsync();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                using (MemoryStream wholeFile = new MemoryStream(buffer))
                {
                    await file.UploadFromStreamAsync(wholeFile.AsInputStream());

                    byte[] testBuffer = new byte[1024];
                    MemoryStream fileStream = new MemoryStream(testBuffer);
                    Exception ex = await TestHelper.ExpectedExceptionAsync<Exception>(
                        async () => await file.DownloadRangeToStreamAsync(fileStream.AsOutputStream(), 0, 0),
                        "Requesting 0 bytes when downloading range should not work");
                    Assert.IsInstanceOfType(ex.InnerException.InnerException, typeof(ArgumentOutOfRangeException));
                    await file.DownloadRangeToStreamAsync(fileStream.AsOutputStream(), 0, 1024);
                    Assert.AreEqual(fileStream.Position, 1024);
                    TestHelper.AssertStreamsAreEqualAtIndex(fileStream, wholeFile, 0, 0, 1024);

                    CloudFile file2 = share.GetRootDirectoryReference().GetFileReference("file1");
                    MemoryStream fileStream2 = new MemoryStream(testBuffer);
                    ex = await TestHelper.ExpectedExceptionAsync<Exception>(
                        async () => await file2.DownloadRangeToStreamAsync(fileStream.AsOutputStream(), 1024, 0),
                        "Requesting 0 bytes when downloading range should not work");
                    Assert.IsInstanceOfType(ex.InnerException.InnerException, typeof(ArgumentOutOfRangeException));
                    await file2.DownloadRangeToStreamAsync(fileStream2.AsOutputStream(), 1024, 1024);
                    TestHelper.AssertStreamsAreEqualAtIndex(fileStream2, wholeFile, 0, 1024, 1024);

                    AssertAreEqual(file, file2);
                }
            }
            finally
            {
                share.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
        public async Task CloudBlockBlobDownloadToStreamCancelAsync()
        {
            byte[] buffer = GetRandomBuffer(1 * 1024 * 1024);
            CloudBlobContainer container = GetRandomContainerReference();
            try
            {
                await container.CreateAsync();

                CloudBlockBlob blob = container.GetBlockBlobReference("blob1");
                using (MemoryStream originalBlob = new MemoryStream(buffer))
                {
                    await blob.UploadFromStreamAsync(originalBlob.AsInputStream());

                    using (MemoryStream downloadedBlob = new MemoryStream())
                    {
                        OperationContext operationContext = new OperationContext();
                        IAsyncAction action = blob.DownloadToStreamAsync(downloadedBlob.AsOutputStream(), null, null, operationContext);
                        await Task.Delay(100);
                        action.Cancel();
                        try
                        {
                            await action;
                        }
                        catch (Exception)
                        {
                            Assert.AreEqual(operationContext.LastResult.Exception.Message, "A task was canceled.");
                            Assert.AreEqual(operationContext.LastResult.HttpStatusCode, 306);
                            //Assert.AreEqual(operationContext.LastResult.HttpStatusMessage, "Unused");
                        }
                        TestHelper.AssertNAttempts(operationContext, 1);
                    }
                }
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
示例#17
0
        /// <summary>
        /// Save the current <see cref="SessionState"/>. Any <see cref="Frame"/> instances
        /// registered with <see cref="RegisterFrame"/> will also preserve their current
        /// navigation stack, which in turn gives their active <see cref="Page"/> an opportunity
        /// to save its state.
        /// </summary>
        /// <returns>An asynchronous task that reflects when session state has been saved.</returns>
        public async Task SaveAsync()
        {
            try
            {
                // Save the navigation state for all registered frames
                foreach (var weakFrameReference in _registeredFrames)
                {
                    IFrameFacade frame;
                    if (weakFrameReference.TryGetTarget(out frame))
                    {
                        SaveFrameNavigationState(frame);
                    }
                }

                // Serialize the session state synchronously to avoid asynchronous access to shared
                // state
                MemoryStream sessionData = new MemoryStream();
                DataContractSerializer serializer = new DataContractSerializer(typeof(Dictionary<string, object>), _knownTypes);
                serializer.WriteObject(sessionData, _sessionState);

                // Get an output stream for the SessionState file and write the state asynchronously
                StorageFile file = await ApplicationData.Current.LocalFolder.CreateFileAsync(Constants.SessionStateFileName, CreationCollisionOption.ReplaceExisting);
                using (var fileStream = await file.OpenAsync(FileAccessMode.ReadWrite))
                {
                    sessionData.Seek(0, SeekOrigin.Begin);
                    var provider = new DataProtectionProvider("LOCAL=user");

                    // Encrypt the session data and write it to disk.
                    await provider.ProtectStreamAsync(sessionData.AsInputStream(), fileStream);
                    await fileStream.FlushAsync();
                }
            }
            catch (Exception e)
            {
                throw new SessionStateServiceException(e);
            }
        }
        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);

                // Check with 0 length
                blocks = GetBlockIdList(0);
                await blob.PutBlockListAsync(blocks);

                await blob.DownloadBlockListAsync();
                Assert.AreEqual(0, blob.Properties.Length);
            }
            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 (MemoryOutputStream downloadedBlob = new MemoryOutputStream())
                    {
                        await blob2.DownloadToStreamAsync(downloadedBlob);
                        TestHelper.AssertStreamsAreEqual(wholeBlob, downloadedBlob.UnderlyingStream);
                    }
                }
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
        public async Task CloudBlockBlobUploadDownloadNoDataAsync()
        {
            CloudBlobContainer container = GetRandomContainerReference();
            try
            {
                await container.CreateAsync();

                CloudBlockBlob blob = container.GetBlockBlobReference("blob");
                await TestHelper.ExpectedExceptionAsync<ArgumentNullException>(
                    async () => await blob.UploadFromStreamAsync(null),
                    "Uploading from a null stream should fail");

                using (MemoryStream stream = new MemoryStream())
                {
                    await blob.UploadFromStreamAsync(stream.AsInputStream());
                }

                await TestHelper.ExpectedExceptionAsync<ArgumentNullException>(
                    async () => await blob.DownloadToStreamAsync(null),
                    "Downloading to a null stream should fail");

                using (MemoryStream stream = new MemoryStream())
                {
                    await blob.DownloadToStreamAsync(stream.AsOutputStream());
                    Assert.AreEqual(0, stream.Length);
                }
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
        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();
            }
        }
        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();
            }
        }
        public async Task CloudBlockBlobPutBlockAsync()
        {
            byte[] buffer = GetRandomBuffer(4 * 1024 * 1024);
#if ASPNET_K
            MD5 md5 = MD5.Create();
            string contentMD5 = Convert.ToBase64String(md5.ComputeHash(buffer));
#else
            CryptographicHash hasher = HashAlgorithmProvider.OpenAlgorithm("MD5").CreateHash();
            hasher.Append(buffer.AsBuffer());
            string contentMD5 = CryptographicBuffer.EncodeToBase64String(hasher.GetValueAndReset());
#endif

            CloudBlobContainer container = GetRandomContainerReference();
            try
            {
                await container.CreateAsync();

                CloudBlockBlob blob = container.GetBlockBlobReference("blob1");
                List<string> blockList = GetBlockIdList(2);

                using (MemoryStream resultingData = new MemoryStream())
                {
                    using (MemoryStream memoryStream = new MemoryStream(buffer))
                    {
                        memoryStream.Seek(0, SeekOrigin.Begin);
                        await blob.PutBlockAsync(blockList[0], memoryStream.AsInputStream(), contentMD5);
                        resultingData.Write(buffer, 0, buffer.Length);

                        int offset = buffer.Length - 1024;
                        memoryStream.Seek(offset, SeekOrigin.Begin);
                        OperationContext opContext = new OperationContext();
                        await TestHelper.ExpectedExceptionAsync(
                            async () => await blob.PutBlockAsync(blockList[1], memoryStream.AsInputStream(), contentMD5, null, null, opContext),
                            opContext,
                            "Invalid MD5 should fail with mismatch",
                            HttpStatusCode.BadRequest,
                            "Md5Mismatch");

                        memoryStream.Seek(offset, SeekOrigin.Begin);
                        await blob.PutBlockAsync(blockList[1], memoryStream.AsInputStream(), null);
                        resultingData.Write(buffer, offset, buffer.Length - offset);
                    }

                    await blob.PutBlockListAsync(blockList);

                    using (MemoryStream blobData = new MemoryStream())
                    {
                        await blob.DownloadToStreamAsync(blobData.AsOutputStream());
                        Assert.AreEqual(resultingData.Length, blobData.Length);
                        Assert.IsTrue(blobData.ToArray().SequenceEqual(resultingData.ToArray()));
                    }
                }
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
        public async Task PageBlobReadLockToETagTestAsync()
        {
            byte[] outBuffer = new byte[1 * 1024 * 1024];
            byte[] buffer = GetRandomBuffer(2 * outBuffer.Length);
            CloudBlobContainer container = GetRandomContainerReference();
            try
            {
                await container.CreateAsync();

                CloudPageBlob blob = container.GetPageBlobReference("blob1");
                blob.StreamMinimumReadSizeInBytes = outBuffer.Length;
                using (MemoryStream wholeBlob = new MemoryStream(buffer))
                {
                    await blob.UploadFromStreamAsync(wholeBlob.AsInputStream());
                }

                OperationContext opContext = new OperationContext();
                using (IRandomAccessStreamWithContentType blobStream = await blob.OpenReadAsync(null, null, opContext))
                {
                    Stream blobStreamForRead = blobStream.AsStreamForRead();
                    await blobStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length);
                    await blob.SetMetadataAsync();
                    await TestHelper.ExpectedExceptionAsync(
                        async () => await blobStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length),
                        opContext,
                        "Blob read stream should fail if blob is modified during read",
                        HttpStatusCode.PreconditionFailed);
                }

                opContext = new OperationContext();
                using (IRandomAccessStreamWithContentType blobStream = await blob.OpenReadAsync(null, null, opContext))
                {
                    Stream blobStreamForRead = blobStream.AsStreamForRead();
                    long length = blobStreamForRead.Length;
                    await blob.SetMetadataAsync();
                    await TestHelper.ExpectedExceptionAsync(
                        async () => await blobStreamForRead.ReadAsync(outBuffer, 0, outBuffer.Length),
                        opContext,
                        "Blob read stream should fail if blob is modified during read",
                        HttpStatusCode.PreconditionFailed);
                }

                opContext = new OperationContext();
                AccessCondition accessCondition = AccessCondition.GenerateIfNotModifiedSinceCondition(DateTimeOffset.Now.Subtract(TimeSpan.FromHours(1)));
                await blob.SetMetadataAsync();
                await TestHelper.ExpectedExceptionAsync(
                    async () => await blob.OpenReadAsync(accessCondition, null, opContext),
                    opContext,
                    "Blob read stream should fail if blob is modified during read",
                    HttpStatusCode.PreconditionFailed);
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
        public async Task FileReadStreamSeekTestAsync()
        {
            byte[] buffer = GetRandomBuffer(3 * 1024 * 1024);
            CloudFileShare share = GetRandomShareReference();
            try
            {
                await share.CreateAsync();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                file.StreamMinimumReadSizeInBytes = 2 * 1024 * 1024;
                using (MemoryStream wholeFile = new MemoryStream(buffer))
                {
                    await file.UploadFromStreamAsync(wholeFile.AsInputStream());
                }

                OperationContext opContext = new OperationContext();
                using (IRandomAccessStreamWithContentType fileStream = await file.OpenReadAsync(null, null, opContext))
                {
                    int attempts = await FileReadStreamSeekTestAsync(fileStream, file.StreamMinimumReadSizeInBytes, buffer);
                    TestHelper.AssertNAttempts(opContext, attempts);
                }
            }
            finally
            {
                share.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
        public async Task CloudBlobUploadTimeoutAsync()
        {
            CloudBlobContainer container = DefaultBlobClient.GetContainerReference(Guid.NewGuid().ToString("N"));
            byte[] buffer = BlobTestBase.GetRandomBuffer(4 * 1024 * 1024);

            try
            {
                await container.CreateAsync();

                CloudBlockBlob blob = container.GetBlockBlobReference("blob1");
                BlobRequestOptions requestOptions = new BlobRequestOptions()
                {
                    MaximumExecutionTime = TimeSpan.FromSeconds(1),
                    RetryPolicy = new NoRetry()
                };

                using (MemoryStream ms = new MemoryStream(buffer))
                {
                    await blob.UploadFromStreamAsync(ms.AsInputStream(), null, requestOptions, null);
                }

                Assert.Fail();
            }
            catch (Exception e)
            {
                Assert.AreEqual(WindowsAzureErrorCode.HttpRequestTimeout, e.HResult);
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
        public async Task CloudBlockBlobParallelUploadFromStreamRequestOptionsAsync()
        {
            CloudBlobContainer container = GetRandomContainerReference();
            await container.CreateAsync();

            try
            {
                const int Size = 20 * 1024 * 1024;
                byte[] buffer = GetRandomBuffer(Size);

                CloudBlockBlob blob = container.GetBlockBlobReference("blob1");
                blob.StreamWriteSizeInBytes = 1 * 1024 * 1024;

                CloudBlockBlob blob2 = container.GetBlockBlobReference("blob2");
                blob2.StreamWriteSizeInBytes = 1 * 1024 * 1024;

                CloudBlockBlob blob3 = container.GetBlockBlobReference("blob3");
                blob3.StreamWriteSizeInBytes = 1 * 1024 * 1024;

                using (MemoryStream originalBlobStream = new MemoryStream())
                {
                    originalBlobStream.Write(buffer, 0, buffer.Length);

                    Stream sourceStream = new MemoryStream(buffer);
                    sourceStream.Seek(0, SeekOrigin.Begin);

                    using (sourceStream)
                    {
                        BlobRequestOptions options = new BlobRequestOptions()
                        {
                            StoreBlobContentMD5 = true,
                            SingleBlobUploadThresholdInBytes = Size,
                            ParallelOperationThreadCount = 2
                        };
                        OperationContext context = new OperationContext();
                        await blob.UploadFromStreamAsync(sourceStream.AsInputStream(), null /* accessCondition */, options, context);

                        // Number or requests should be at least 21 since StreamWriteSizeInBytes is 1 MB
                        Assert.IsTrue(context.RequestResults.Count >= 21);

                        sourceStream.Seek(0, SeekOrigin.Begin);
                        options = new BlobRequestOptions()
                        {
                            StoreBlobContentMD5 = true,
                            SingleBlobUploadThresholdInBytes = Size / 2,
                            ParallelOperationThreadCount = 1
                        };
                        context = new OperationContext();
                        await blob2.UploadFromStreamAsync(sourceStream.AsInputStream(), null /* accessCondition */, options, context);

                        // Number or requests should be at least 21 since StreamWriteSizeInBytes is 1 MB
                        Assert.IsTrue(context.RequestResults.Count >= 21);

                        sourceStream.Seek(0, SeekOrigin.Begin);
                        options = new BlobRequestOptions()
                        {
                            StoreBlobContentMD5 = true,
                            SingleBlobUploadThresholdInBytes = Size,
                            ParallelOperationThreadCount = 1
                        };
                        context = new OperationContext();
                        await blob3.UploadFromStreamAsync(sourceStream.AsInputStream(), null /* accessCondition */, options, context);

                        // Number or requests should 1, or 2 if there is a retry
                        Assert.IsTrue(context.RequestResults.Count <= 2);
                    }
                }
            }
            finally
            {
                container.DeleteAsync().AsTask().Wait();
            }
        }
        public async Task PageBlobReadStreamSeekTestAsync()
        {
            byte[] buffer = GetRandomBuffer(3 * 1024 * 1024);
            CloudBlobContainer container = GetRandomContainerReference();
            try
            {
                await container.CreateAsync();

                CloudPageBlob blob = container.GetPageBlobReference("blob1");
                blob.StreamMinimumReadSizeInBytes = 2 * 1024 * 1024;
                using (MemoryStream wholeBlob = new MemoryStream(buffer))
                {
                    await blob.UploadFromStreamAsync(wholeBlob.AsInputStream());
                }

                OperationContext opContext = new OperationContext();
                using (IRandomAccessStreamWithContentType blobStream = await blob.OpenReadAsync(null, null, opContext))
                {
                    int attempts = await BlobReadStreamSeekTestAsync(blobStream, blob.StreamMinimumReadSizeInBytes, buffer);
                    TestHelper.AssertNAttempts(opContext, attempts);
                }
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
        public async Task CloudBlockBlobUploadFromStreamInvalidOptionsAsync()
        {
            BlobRequestOptions options = new BlobRequestOptions()
            {
                UseTransactionalMD5 = true,
                StoreBlobContentMD5 = false,
            };

            CloudBlobContainer container = GetRandomContainerReference();
            await container.CreateAsync();
            try
            {
                CloudBlockBlob blob = container.GetBlockBlobReference("blob1");
                using (MemoryStream stream = new MemoryStream())
                {
                    await TestHelper.ExpectedExceptionAsync<ArgumentException>(
                        async () => await blob.UploadFromStreamAsync(stream.AsInputStream(), null, options, null),
                        "Single put blob with mismatching MD5 options should fail immediately");
                }
            }
            finally
            {
                container.DeleteAsync().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();
            }
        }
示例#31
0
        public static IInputStream CreateMemoryStreamAsInputStream()
        {
            MemoryStream memStream = CreateMemoryStream();

            return(memStream.AsInputStream());
        }
        public async Task CloudBlockBlobSnapshotAsync()
        {
            CloudBlobContainer container = GetRandomContainerReference();
            try
            {
                await container.CreateAsync();

                MemoryStream originalData = new MemoryStream(GetRandomBuffer(1024));
                CloudBlockBlob blob = container.GetBlockBlobReference("blob1");
                await blob.UploadFromStreamAsync(originalData.AsInputStream());

                Assert.IsFalse(blob.IsSnapshot);
                Assert.IsNull(blob.SnapshotTime, "Root blob has SnapshotTime set");
                Assert.IsFalse(blob.SnapshotQualifiedUri.Query.Contains("snapshot"));
                Assert.AreEqual(blob.Uri, blob.SnapshotQualifiedUri);

                CloudBlockBlob snapshot1 = await blob.CreateSnapshotAsync();
                Assert.AreEqual(blob.Properties.ETag, snapshot1.Properties.ETag);
                Assert.AreEqual(blob.Properties.LastModified, snapshot1.Properties.LastModified);
                Assert.IsTrue(snapshot1.IsSnapshot);
                Assert.IsNotNull(snapshot1.SnapshotTime, "Snapshot does not have SnapshotTime set");
                Assert.AreEqual(blob.Uri, snapshot1.Uri);
                Assert.AreNotEqual(blob.SnapshotQualifiedUri, snapshot1.SnapshotQualifiedUri);
                Assert.AreNotEqual(snapshot1.Uri, snapshot1.SnapshotQualifiedUri);
                Assert.IsTrue(snapshot1.SnapshotQualifiedUri.Query.Contains("snapshot"));

                CloudBlockBlob snapshot2 = await blob.CreateSnapshotAsync();
                Assert.IsTrue(snapshot2.SnapshotTime.Value > snapshot1.SnapshotTime.Value);

                await snapshot1.FetchAttributesAsync();
                await snapshot2.FetchAttributesAsync();
                await blob.FetchAttributesAsync();
                AssertAreEqual(snapshot1.Properties, blob.Properties);

                CloudBlockBlob snapshot1Clone = new CloudBlockBlob(new Uri(blob.Uri + "?snapshot=" + snapshot1.SnapshotTime.Value.ToString("O")), blob.ServiceClient.Credentials);
                Assert.IsNotNull(snapshot1Clone.SnapshotTime, "Snapshot clone does not have SnapshotTime set");
                Assert.AreEqual(snapshot1.SnapshotTime.Value, snapshot1Clone.SnapshotTime.Value);
                await snapshot1Clone.FetchAttributesAsync();
                AssertAreEqual(snapshot1.Properties, snapshot1Clone.Properties);

                CloudBlockBlob snapshotCopy = container.GetBlockBlobReference("blob2");
                await snapshotCopy.StartCopyFromBlobAsync(TestHelper.Defiddler(snapshot1.Uri));
                await WaitForCopyAsync(snapshotCopy);
                Assert.AreEqual(CopyStatus.Success, snapshotCopy.CopyState.Status);

                await TestHelper.ExpectedExceptionAsync<InvalidOperationException>(
                    async () => await snapshot1.OpenWriteAsync(),
                    "Trying to write to a blob snapshot should fail");

                using (Stream snapshotStream = (await snapshot1.OpenReadAsync()).AsStreamForRead())
                {
                    snapshotStream.Seek(0, SeekOrigin.End);
                    TestHelper.AssertStreamsAreEqual(originalData, snapshotStream);
                }

                await blob.PutBlockListAsync(new List<string>());
                await blob.FetchAttributesAsync();

                using (Stream snapshotStream = (await snapshot1.OpenReadAsync()).AsStreamForRead())
                {
                    snapshotStream.Seek(0, SeekOrigin.End);
                    TestHelper.AssertStreamsAreEqual(originalData, snapshotStream);
                }

                BlobResultSegment resultSegment = await container.ListBlobsSegmentedAsync(null, true, BlobListingDetails.All, null, null, null, null);
                List<IListBlobItem> blobs = resultSegment.Results.ToList();
                Assert.AreEqual(4, blobs.Count);
                AssertAreEqual(snapshot1, (ICloudBlob)blobs[0]);
                AssertAreEqual(snapshot2, (ICloudBlob)blobs[1]);
                AssertAreEqual(blob, (ICloudBlob)blobs[2]);
                AssertAreEqual(snapshotCopy, (ICloudBlob)blobs[3]);
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }
        public async Task CloudBlobUploadCancellationAsync()
        {
            CloudBlobContainer container = DefaultBlobClient.GetContainerReference(Guid.NewGuid().ToString("N"));
            byte[] buffer = BlobTestBase.GetRandomBuffer(4 * 1024 * 1024);

            try
            {
                await container.CreateAsync();

                CloudBlockBlob blob = container.GetBlockBlobReference("blob1");
                BlobRequestOptions requestOptions = new BlobRequestOptions()
                {
                    RetryPolicy = new NoRetry()
                };

                CancellationTokenSource cts = new CancellationTokenSource();
                CancellationToken token = cts.Token; 

                new Task(() =>
                {
                    new System.Threading.ManualResetEvent(false).WaitOne(500);
                    cts.Cancel(false);
                }).Start();

                using (MemoryStream ms = new MemoryStream(buffer))
                {
                    blob.UploadFromStreamAsync(ms.AsInputStream(), null, requestOptions, null).AsTask(token).Wait();
                }

                Assert.Fail();
            }
            catch (AggregateException e)
            {
                TaskCanceledException ex = new TaskCanceledException();
                Assert.AreEqual(ex.HResult, e.InnerException.HResult);
            }
            finally
            {
                container.DeleteIfExistsAsync().AsTask().Wait();
            }
        }