public void BlobWriteWhenOpenRead()
        {
            byte[]             buffer    = GetRandomBuffer(2 * 1024);
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();

                CloudPageBlob blob = container.GetPageBlobReference("blob1");
                using (MemoryStream srcStream = new MemoryStream(buffer))
                {
                    blob.UploadFromStream(srcStream);
                    Stream blobStream = blob.OpenRead();

                    byte[] testBuffer = new byte[2048];
                    TestHelper.ExpectedException <NotSupportedException>(() => blobStream.Write(testBuffer, 0, 2048),
                                                                         "Try writing to a stream opened for read");
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
        public async Task BlobOpenReadWithCancelTest()
        {
            CancellationTokenSource cts = new CancellationTokenSource();

            byte[]             buffer    = new byte[4 * 1024 * 1024];
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();
                CloudPageBlob blob = container.GetPageBlobReference("blob1");
                using (MemoryStream srcStream = new MemoryStream(buffer))
                {
                    blob.UploadFromStream(srcStream);
                }

                Stream dstStream = await blob.OpenReadAsync(null, null, null, cts.Token);

                cts.Cancel();

                try
                {
                    Assert.IsTrue(cts.Token.IsCancellationRequested);

                    int bytesRead = 0;

                    do
                    {
                        bytesRead = await dstStream.ReadAsync(buffer, 0, buffer.Length, cts.Token);
                    }while (bytesRead > 0);

                    Assert.Fail("Expected exception not raised");
                }
                catch (StorageException e)
                {
                    Assert.AreEqual("A task was canceled.", e.Message);
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
        public void BlobOpenReadTest()
        {
            byte[]             buffer    = GetRandomBuffer(2 * 1024);
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();
                CloudPageBlob blob = container.GetPageBlobReference("blob1");
                using (MemoryStream srcStream = new MemoryStream(buffer))
                {
                    blob.UploadFromStream(srcStream);

                    Stream dstStream = blob.OpenRead();
                    TestHelper.AssertStreamsAreEqual(srcStream, dstStream);
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }
Esempio n. 4
0
        public static void UploadText(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;

                if (blob.BlobType == BlobType.AppendBlob)
                {
                    CloudAppendBlob blob1 = blob as CloudAppendBlob;
                    blob1.CreateOrReplace();
                    blob1.AppendBlock(stream, null);
                }
                else if (blob.BlobType == BlobType.PageBlob)
                {
                    CloudPageBlob pageBlob = blob as CloudPageBlob;
                    pageBlob.UploadFromStream(stream, accessCondition, options, operationContext);
                }
                else
                {
                    CloudBlockBlob blockBlob = blob as CloudBlockBlob;
                    blockBlob.UploadFromStream(stream, accessCondition, options, operationContext);
                }
            }
        }
        public void BlobSeekTest()
        {
            byte[]             buffer    = GetRandomBuffer(2 * 1024);
            CloudBlobContainer container = GetRandomContainerReference();

            try
            {
                container.Create();
                CloudPageBlob blob = container.GetPageBlobReference("blob1");
                using (MemoryStream srcStream = new MemoryStream(buffer))
                {
                    blob.UploadFromStream(srcStream);
                    Stream blobStream = blob.OpenRead();
                    blobStream.Seek(2048, 0);
                    byte[] buff    = new byte[100];
                    int    numRead = blobStream.Read(buff, 0, 100);
                    Assert.AreEqual(numRead, 0);
                }
            }
            finally
            {
                container.DeleteIfExists();
            }
        }