示例#1
0
 public static string DownloadTextAPM(CloudFile file, Encoding encoding, AccessCondition accessCondition = null, FileRequestOptions options = null, OperationContext operationContext = null)
 {
     using (MemoryStream stream = new MemoryStream())
     {
         using (AutoResetEvent waitHandle = new AutoResetEvent(false))
         {
             IAsyncResult result = file.BeginDownloadToStream(stream, accessCondition, options, operationContext, ar => waitHandle.Set(), null);
             waitHandle.WaitOne();
             file.EndDownloadToStream(result);
             return(encoding.GetString(stream.ToArray()));
         }
     }
 }
        public void FileUseTransactionalMD5GetTestAPM()
        {
            FileRequestOptions optionsWithNoMD5 = new FileRequestOptions()
            {
                UseTransactionalMD5 = false,
            };
            FileRequestOptions optionsWithMD5 = new FileRequestOptions()
            {
                UseTransactionalMD5 = true,
            };

            byte[] buffer = GetRandomBuffer(3 * 1024 * 1024);
            MD5    hasher = MD5.Create();
            string md5    = Convert.ToBase64String(hasher.ComputeHash(buffer));

            string           lastCheckMD5          = null;
            int              checkCount            = 0;
            OperationContext opContextWithMD5Check = new OperationContext();

            opContextWithMD5Check.ResponseReceived += (_, args) =>
            {
                if (long.Parse(HttpResponseParsers.GetContentLength(args.Response)) >= buffer.Length)
                {
                    lastCheckMD5 = HttpResponseParsers.GetContentMD5(args.Response);
                    checkCount++;
                }
            };

            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.Create();

                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    IAsyncResult result;
                    CloudFile    file = share.GetRootDirectoryReference().GetFileReference("file2");
                    using (Stream fileStream = file.OpenWrite(buffer.Length * 2))
                    {
                        fileStream.Write(buffer, 0, buffer.Length);
                        fileStream.Write(buffer, 0, buffer.Length);
                    }

                    checkCount = 0;
                    using (Stream stream = new MemoryStream())
                    {
                        result = file.BeginDownloadToStream(stream, null, optionsWithNoMD5, opContextWithMD5Check,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        file.EndDownloadRangeToStream(result);
                        Assert.IsNull(lastCheckMD5);

                        result = file.BeginDownloadToStream(stream, null, optionsWithMD5, opContextWithMD5Check,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        StorageException storageEx = TestHelper.ExpectedException <StorageException>(
                            () => file.EndDownloadRangeToStream(result),
                            "File will not have MD5 set by default; with UseTransactional, download should fail");

                        result = file.BeginDownloadRangeToStream(stream, buffer.Length, buffer.Length, null, optionsWithNoMD5, opContextWithMD5Check,
                                                                 ar => waitHandle.Set(),
                                                                 null);
                        waitHandle.WaitOne();
                        file.EndDownloadRangeToStream(result);
                        Assert.IsNull(lastCheckMD5);

                        result = file.BeginDownloadRangeToStream(stream, buffer.Length, buffer.Length, null, optionsWithMD5, opContextWithMD5Check,
                                                                 ar => waitHandle.Set(),
                                                                 null);
                        waitHandle.WaitOne();
                        file.EndDownloadRangeToStream(result);
                        Assert.AreEqual(md5, lastCheckMD5);

                        result = file.BeginDownloadRangeToStream(stream, 1024, 4 * 1024 * 1024 + 1, null, optionsWithNoMD5, opContextWithMD5Check,
                                                                 ar => waitHandle.Set(),
                                                                 null);
                        waitHandle.WaitOne();
                        file.EndDownloadRangeToStream(result);
                        Assert.IsNull(lastCheckMD5);

                        result = file.BeginDownloadRangeToStream(stream, 1024, 4 * 1024 * 1024 + 1, null, optionsWithMD5, opContextWithMD5Check,
                                                                 ar => waitHandle.Set(),
                                                                 null);
                        waitHandle.WaitOne();
                        storageEx = TestHelper.ExpectedException <StorageException>(
                            () => file.EndDownloadRangeToStream(result),
                            "Downloading more than 4MB with transactional MD5 should not be supported");
                        Assert.IsInstanceOfType(storageEx.InnerException, typeof(ArgumentOutOfRangeException));

                        result = file.BeginOpenRead(null, optionsWithMD5, opContextWithMD5Check,
                                                    ar => waitHandle.Set(),
                                                    null);
                        waitHandle.WaitOne();
                        using (Stream fileStream = file.EndOpenRead(result))
                        {
                            fileStream.CopyTo(stream);
                            Assert.IsNotNull(lastCheckMD5);
                        }

                        result = file.BeginOpenRead(null, optionsWithNoMD5, opContextWithMD5Check,
                                                    ar => waitHandle.Set(),
                                                    null);
                        waitHandle.WaitOne();
                        using (Stream fileStream = file.EndOpenRead(result))
                        {
                            fileStream.CopyTo(stream);
                            Assert.IsNull(lastCheckMD5);
                        }
                    }
                    Assert.AreEqual(9, checkCount);
                }
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
        public void FileDisableContentMD5ValidationTestAPM()
        {
            FileRequestOptions optionsWithNoMD5 = new FileRequestOptions()
            {
                DisableContentMD5Validation = true,
                StoreFileContentMD5         = true,
            };
            FileRequestOptions optionsWithMD5 = new FileRequestOptions()
            {
                DisableContentMD5Validation = false,
                StoreFileContentMD5         = true,
            };

            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.Create();

                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    IAsyncResult result;
                    CloudFile    file = share.GetRootDirectoryReference().GetFileReference("file2");
                    using (Stream stream = new MemoryStream())
                    {
                        file.UploadFromStream(stream, null, optionsWithMD5);
                    }

                    using (Stream stream = new MemoryStream())
                    {
                        result = file.BeginDownloadToStream(stream, null, optionsWithMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        file.EndDownloadToStream(result);
                        result = file.BeginDownloadToStream(stream, null, optionsWithNoMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        file.EndDownloadToStream(result);

                        file.Properties.ContentMD5 = "MDAwMDAwMDA=";
                        file.SetProperties();

                        result = file.BeginDownloadToStream(stream, null, optionsWithMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        TestHelper.ExpectedException(
                            () => file.EndDownloadToStream(result),
                            "Downloading a file with invalid MD5 should fail",
                            HttpStatusCode.OK);
                        result = file.BeginDownloadToStream(stream, null, optionsWithNoMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        file.EndDownloadToStream(result);
                    }
                }
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
 public static string DownloadTextAPM(CloudFile file, Encoding encoding, AccessCondition accessCondition = null, FileRequestOptions options = null, OperationContext operationContext = null)
 {
     using (MemoryStream stream = new MemoryStream())
     {
         using (AutoResetEvent waitHandle = new AutoResetEvent(false))
         {
             IAsyncResult result = file.BeginDownloadToStream(stream, accessCondition, options, operationContext, ar => waitHandle.Set(), null);
             waitHandle.WaitOne();
             file.EndDownloadToStream(result);
             return encoding.GetString(stream.ToArray());
         }
     }
 }
示例#5
0
        public void CloudFileDownloadToStreamAPMRetry()
        {
            byte[]         buffer = GetRandomBuffer(1 * 1024 * 1024);
            CloudFileShare share  = GetRandomShareReference();

            try
            {
                share.Create();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file1");
                using (MemoryStream originalFile = new MemoryStream(buffer))
                {
                    using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                    {
                        ICancellableAsyncResult result = file.BeginUploadFromStream(originalFile,
                                                                                    ar => waitHandle.Set(),
                                                                                    null);
                        waitHandle.WaitOne();
                        file.EndUploadFromStream(result);

                        using (MemoryStream downloadedFile = new MemoryStream())
                        {
                            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.FileTraffic().IfHostNameContains(share.ServiceClient.Credentials.AccountName))
                            }))
                            {
                                OperationContext operationContext = new OperationContext();
                                result = file.BeginDownloadToStream(downloadedFile, null, null, operationContext,
                                                                    ar => waitHandle.Set(),
                                                                    null);
                                waitHandle.WaitOne();
                                file.EndDownloadToStream(result);
                                TestHelper.AssertStreamsAreEqual(originalFile, downloadedFile);
                            }

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