Пример #1
0
        public static void UploadTextAPM(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;
                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    IAsyncResult result = file.BeginUploadFromStream(stream, accessCondition, options, operationContext,
                                                                     ar => waitHandle.Set(),
                                                                     null);
                    waitHandle.WaitOne();
                    file.EndUploadFromStream(result);
                }
            }
        }
Пример #2
0
        public void FileStoreContentMD5TestAPM()
        {
            FileRequestOptions optionsWithNoMD5 = new FileRequestOptions()
            {
                StoreFileContentMD5 = false,
            };
            FileRequestOptions optionsWithMD5 = new FileRequestOptions()
            {
                StoreFileContentMD5 = true,
            };

            CloudFileShare share = GetRandomShareReference();

            try
            {
                share.Create();

                using (AutoResetEvent waitHandle = new AutoResetEvent(false))
                {
                    IAsyncResult result;
                    CloudFile    file = share.GetRootDirectoryReference().GetFileReference("file4");
                    using (Stream stream = new MemoryStream())
                    {
                        result = file.BeginUploadFromStream(stream, null, optionsWithMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        file.EndUploadFromStream(result);
                    }
                    file.FetchAttributes();
                    Assert.IsNotNull(file.Properties.ContentMD5);

                    file = share.GetRootDirectoryReference().GetFileReference("file5");
                    using (Stream stream = new MemoryStream())
                    {
                        result = file.BeginUploadFromStream(stream, null, optionsWithNoMD5, null,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        file.EndUploadFromStream(result);
                    }
                    file.FetchAttributes();
                    Assert.IsNull(file.Properties.ContentMD5);

                    file = share.GetRootDirectoryReference().GetFileReference("file6");
                    using (Stream stream = new MemoryStream())
                    {
                        result = file.BeginUploadFromStream(stream,
                                                            ar => waitHandle.Set(),
                                                            null);
                        waitHandle.WaitOne();
                        file.EndUploadFromStream(result);
                    }
                    file.FetchAttributes();
                    Assert.IsNull(file.Properties.ContentMD5);
                }
            }
            finally
            {
                share.DeleteIfExists();
            }
        }
        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();
            }
        }