Пример #1
0
        public static void UploadTextAPM(CloudBlob blob, string text, Encoding encoding, AccessCondition accessCondition = null, BlobRequestOptions options = null, OperationContext operationContext = null)
        {
            byte[] textAsBytes = encoding.GetBytes(text);
            using (MemoryStream stream = new MemoryStream())
            {
                stream.Write(textAsBytes, 0, textAsBytes.Length);
                if (blob.BlobType == BlobType.PageBlob)
                {
                    int lastPageSize = (int)(stream.Length % 512);
                    if (lastPageSize != 0)
                    {
                        byte[] padding = new byte[512 - lastPageSize];
                        stream.Write(padding, 0, padding.Length);
                    }
                }

                stream.Seek(0, SeekOrigin.Begin);
                blob.ServiceClient.DefaultRequestOptions.ParallelOperationThreadCount = 2;

                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    if (blob.BlobType == BlobType.AppendBlob)
                    {
                        CloudAppendBlob blob1 = blob as CloudAppendBlob;

                        IAsyncResult result = blob1.BeginCreateOrReplace(
                            ar => waitHandle.Set(),
                            null);
                        waitHandle.WaitOne();
                        blob1.EndCreateOrReplace(result);

                        result = blob1.BeginAppendBlock(stream, null,
                                                        ar => waitHandle.Set(),
                                                        null);
                        waitHandle.WaitOne();
                        blob1.EndAppendBlock(result);
                    }
                    else if (blob.BlobType == BlobType.PageBlob)
                    {
                        CloudPageBlob pageBlob = blob as CloudPageBlob;
                        IAsyncResult  result   = pageBlob.BeginUploadFromStream(stream, accessCondition, options, operationContext,
                                                                                ar => waitHandle.Set(),
                                                                                null);
                        waitHandle.WaitOne();
                        pageBlob.EndUploadFromStream(result);
                    }
                    else
                    {
                        CloudBlockBlob blockBlob = blob as CloudBlockBlob;
                        IAsyncResult   result    = blockBlob.BeginUploadFromStream(stream, accessCondition, options, operationContext,
                                                                                   ar => waitHandle.Set(),
                                                                                   null);
                        waitHandle.WaitOne();
                        blockBlob.EndUploadFromStream(result);
                    }
                }
            }
        }
Пример #2
0
        public void CloudBlockBlobDownloadToStreamAPMCancel()
        {
            byte[]             buffer    = GetRandomBuffer(1 * 1024 * 1024);
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                CloudBlockBlob blob = container.GetBlockBlobReference("blob1");
                using (MemoryStream originalBlob = new MemoryStream(buffer))
                {
                    using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                    {
                        ICancellableAsyncResult result = blob.BeginUploadFromStream(originalBlob,
                                                                                    ar => waitHandle.Set(),
                                                                                    null);
                        waitHandle.WaitOne();
                        blob.EndUploadFromStream(result);

                        using (MemoryStream downloadedBlob = new MemoryStream())
                        {
                            OperationContext operationContext = new OperationContext();
                            result = blob.BeginDownloadToStream(downloadedBlob, null, null, operationContext,
                                                                ar => waitHandle.Set(),
                                                                null);
                            Thread.Sleep(100);
                            result.Cancel();
                            waitHandle.WaitOne();
                            try
                            {
                                blob.EndDownloadToStream(result);
                            }
                            catch (StorageException ex)
                            {
                                Assert.AreEqual("A task was canceled.", ex.Message);
                                Assert.AreEqual(ex.RequestInformation.HttpStatusCode, 306);
                                Assert.AreEqual(ex.RequestInformation.HttpStatusMessage, null);
                            }
                            TestHelper.AssertNAttempts(operationContext, 1);
                        }
                    }
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
        public void CloudBlockBlobDownloadRangeToStreamAPMRetry()
        {
            byte[]             buffer    = GetRandomBuffer(1 * 1024 * 1024);
            int                offset    = 1024;
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                CloudBlockBlob blob = container.GetBlockBlobReference("blob1");
                using (MemoryStream originalBlob = new MemoryStream(buffer))
                {
                    using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                    {
                        ICancellableAsyncResult result = blob.BeginUploadFromStream(originalBlob,
                                                                                    ar => waitHandle.Set(),
                                                                                    null);
                        waitHandle.WaitOne();
                        blob.EndUploadFromStream(result);
                    }
                }

                using (MemoryStream originalBlob = new MemoryStream())
                {
                    originalBlob.Write(buffer, offset, buffer.Length - offset);

                    using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                    {
                        Exception manglerEx = null;
                        using (HttpMangler proxy = new HttpMangler(false,
                                                                   new[]
                        {
                            TamperBehaviors.TamperNRequestsIf(
                                session => ThreadPool.QueueUserWorkItem(state =>
                            {
                                Thread.Sleep(1000);
                                try
                                {
                                    session.Abort();
                                }
                                catch (Exception e)
                                {
                                    manglerEx = e;
                                }
                            }),
                                2,
                                AzureStorageSelectors.BlobTraffic().IfHostNameContains(container.ServiceClient.Credentials.AccountName))
                        }))
                        {
                            foreach (var options in new[]
                            {
                                new BlobRequestOptions()
                                {
                                    ChecksumOptions =
                                        new ChecksumOptions
                                    {
                                        UseTransactionalMD5 = true,
                                        UseTransactionalCRC64 = false
                                    }
                                },
                                new BlobRequestOptions()
                                {
                                    ChecksumOptions =
                                        new ChecksumOptions
                                    {
                                        UseTransactionalMD5 = false,
                                        UseTransactionalCRC64 = true
                                    }
                                }
                            })
                            {
                                using (MemoryStream downloadedBlob = new MemoryStream())
                                {
                                    OperationContext operationContext = new OperationContext();

                                    ICancellableAsyncResult result = blob.BeginDownloadRangeToStream(downloadedBlob, offset, buffer.Length - offset, null, options, operationContext,
                                                                                                     ar => waitHandle.Set(),
                                                                                                     null);
                                    waitHandle.WaitOne();
                                    blob.EndDownloadToStream(result);
                                    TestHelper.AssertStreamsAreEqual(originalBlob, downloadedBlob);

                                    if (manglerEx != null)
                                    {
                                        throw manglerEx;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }