Пример #1
0
 public static string DownloadText(CloudFile file, Encoding encoding, AccessCondition accessCondition = null, FileRequestOptions options = null, OperationContext operationContext = null)
 {
     using (MemoryStream stream = new MemoryStream())
     {
         file.DownloadToStream(stream, accessCondition, options, operationContext);
         return(encoding.GetString(stream.ToArray()));
     }
 }
Пример #2
0
        public void FileUseTransactionalMD5GetTest()
        {
            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();

                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())
                {
                    file.DownloadToStream(stream, null, optionsWithNoMD5, opContextWithMD5Check);
                    Assert.IsNull(lastCheckMD5);

                    StorageException storageEx = TestHelper.ExpectedException <StorageException>(
                        () => file.DownloadToStream(stream, null, optionsWithMD5, opContextWithMD5Check),
                        "File will not have MD5 set by default; with UseTransactional, download should fail");

                    file.DownloadRangeToStream(stream, buffer.Length, buffer.Length, null, optionsWithNoMD5, opContextWithMD5Check);
                    Assert.IsNull(lastCheckMD5);

                    file.DownloadRangeToStream(stream, buffer.Length, buffer.Length, null, optionsWithMD5, opContextWithMD5Check);
                    Assert.AreEqual(md5, lastCheckMD5);

                    file.DownloadRangeToStream(stream, 1024, 4 * 1024 * 1024 + 1, null, optionsWithNoMD5, opContextWithMD5Check);
                    Assert.IsNull(lastCheckMD5);

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

                    using (Stream fileStream = file.OpenRead(null, optionsWithMD5, opContextWithMD5Check))
                    {
                        fileStream.CopyTo(stream);
                        Assert.IsNotNull(lastCheckMD5);
                    }

                    using (Stream fileStream = file.OpenRead(null, optionsWithNoMD5, opContextWithMD5Check))
                    {
                        fileStream.CopyTo(stream);
                        Assert.IsNull(lastCheckMD5);
                    }
                }
                Assert.AreEqual(9, checkCount);
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
Пример #3
0
        public void FileDisableContentMD5ValidationTest()
        {
            byte[] buffer = new byte[1024];
            Random random = new Random();

            random.NextBytes(buffer);

            FileRequestOptions optionsWithNoMD5 = new FileRequestOptions()
            {
                DisableContentMD5Validation = true,
                StoreFileContentMD5         = true,
            };
            FileRequestOptions optionsWithMD5 = new FileRequestOptions()
            {
                DisableContentMD5Validation = false,
                StoreFileContentMD5         = true,
            };

            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.Create();

                CloudFile file = share.GetRootDirectoryReference().GetFileReference("file2");
                using (Stream stream = new MemoryStream(buffer))
                {
                    file.UploadFromStream(stream, null, optionsWithMD5);
                }

                using (Stream stream = new MemoryStream())
                {
                    file.DownloadToStream(stream, null, optionsWithMD5);
                    file.DownloadToStream(stream, null, optionsWithNoMD5);

                    using (Stream fileStream = file.OpenRead(null, optionsWithMD5))
                    {
                        int read;
                        do
                        {
                            read = fileStream.Read(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }

                    using (Stream fileStream = file.OpenRead(null, optionsWithNoMD5))
                    {
                        int read;
                        do
                        {
                            read = fileStream.Read(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }

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

                    TestHelper.ExpectedException(
                        () => file.DownloadToStream(stream, null, optionsWithMD5),
                        "Downloading a file with invalid MD5 should fail",
                        HttpStatusCode.OK);
                    file.DownloadToStream(stream, null, optionsWithNoMD5);

                    using (Stream fileStream = file.OpenRead(null, optionsWithMD5))
                    {
                        TestHelper.ExpectedException <IOException>(
                            () =>
                        {
                            int read;
                            do
                            {
                                read = fileStream.Read(buffer, 0, buffer.Length);
                            }while (read > 0);
                        },
                            "Downloading a file with invalid MD5 should fail");
                    }

                    using (Stream fileStream = file.OpenRead(null, optionsWithNoMD5))
                    {
                        int read;
                        do
                        {
                            read = fileStream.Read(buffer, 0, buffer.Length);
                        }while (read > 0);
                    }
                }
            }
            finally
            {
                share.DeleteIfExists();
            }
        }